commit 2e13c87a9d236f7e994e60ae0264a3372f1f2926 Author: Dunestorm Date: Thu Aug 25 00:27:03 2022 +0200 Initial Commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b836266 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2ccb4ac --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/pyGame.iml b/.idea/pyGame.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/.idea/pyGame.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Background.png b/Background.png new file mode 100644 index 0000000..90bca8b Binary files /dev/null and b/Background.png differ diff --git a/PaddleA.png b/PaddleA.png new file mode 100644 index 0000000..c581d14 Binary files /dev/null and b/PaddleA.png differ diff --git a/PaddleB.png b/PaddleB.png new file mode 100644 index 0000000..28a838e Binary files /dev/null and b/PaddleB.png differ diff --git a/Sprites.afdesign b/Sprites.afdesign new file mode 100644 index 0000000..c55abf4 Binary files /dev/null and b/Sprites.afdesign differ diff --git a/background.py b/background.py new file mode 100644 index 0000000..ed330ef --- /dev/null +++ b/background.py @@ -0,0 +1,7 @@ +import pygame + +_image = pygame.image.load('Background.png') + + +def draw(surface: pygame.surface): + surface.blit(_image.convert(), (0, 0)) diff --git a/colors.py b/colors.py new file mode 100644 index 0000000..71be675 --- /dev/null +++ b/colors.py @@ -0,0 +1,2 @@ +White = (255, 255, 255) +Black = (0, 0, 0) diff --git a/main.py b/main.py new file mode 100644 index 0000000..43b033f --- /dev/null +++ b/main.py @@ -0,0 +1,35 @@ +import pygame +import sys +from pygame.locals import * + +import osd +from player import Player +import background + +surface = pygame.display.set_mode((800, 600)) +clock = pygame.time.Clock() + +player = Player(surface, x=20) +player2 = Player(surface, x=760) +player2.use_alternate_sprite() +player2.reverse_player() + +while True: + for event in pygame.event.get(): + if event.type == QUIT: + pygame.quit() + sys.exit() + + background.draw(surface) + player.control() + player.draw() + + player2.y = player.y + player2.draw() + + osd.draw_fps(surface, clock) + osd.draw_player_pos(surface, player.y) + + # Update scene + clock.tick(120) + pygame.display.update() diff --git a/osd.py b/osd.py new file mode 100644 index 0000000..69f511f --- /dev/null +++ b/osd.py @@ -0,0 +1,16 @@ +import pygame +import colors as Colors + +pygame.init() +pygame.font.init() +_font = pygame.font.SysFont('Calibre', 24) + + +def draw_fps(surface: pygame.surface, clock: pygame.time.Clock): + _text_fps = _font.render(f"FPS: {round(clock.get_fps(), 0)}", True, Colors.White) + surface.blit(_text_fps, (5, 5)) + + +def draw_player_pos(surface: pygame.surface, pos_y): + _text_fps = _font.render(f"Y: {pos_y}", True, Colors.White) + surface.blit(_text_fps, (5, 25)) diff --git a/player.py b/player.py new file mode 100644 index 0000000..f1ee809 --- /dev/null +++ b/player.py @@ -0,0 +1,54 @@ +import pygame + + +class Player: + """ + Creates new instance of player or NPC. + + :param surface: Surface to draw player onto + :param x: Horizontal position of player + :return: Returns nothing + """ + + def __init__(self, surface: pygame.surface, x): + self._surface = surface + self._image = pygame.image.load('PaddleA.png').convert_alpha() + self._rect = self._image.get_rect() + self.x = x + self.y = round(((self._surface.get_height() / 2) - self._rect.height / 2) / 10, 0) * 10 + self.vec = 5 + self.player = None + self.dist_bot = 0 + self.dist_top = 0 + + def use_alternate_sprite(self): + """ Changes out default sprite for alternate colorized version. """ + self._image = pygame.image.load('PaddleB.png').convert_alpha() + + def reverse_player(self): + """ Rotates player sprite 180°. """ + + self._image = pygame.transform.rotate(self._image, 180) + + def draw(self): + """ Draw player to screen and update positions relative to screen area. """ + + # self.player = pygame.draw.circle(self._surface, + # Colors.White, + # (self.x, self.y), + # 25) + + self.player = self._surface.blit(self._image, (self.x, self.y)) + # Calculate player position relative to screen area + self.dist_bot = (self._surface.get_height() - self._rect.y - self.y) - self._rect.height + self.dist_top = 0 + self._rect.y + self.y + + def control(self): + """ Handover physical controls to player. """ + + keys = pygame.key.get_pressed() + # print(f"{self.dist_top} {self.dist_bot}") + if keys[pygame.K_w] and self.dist_top > 0: + self.y -= self.vec + if keys[pygame.K_s] and self.dist_bot > 0: + self.y += self.vec