Created
May 22, 2020 22:32
-
-
Save ectrimble20/1fded1d0af31888acb1442509db7f260 to your computer and use it in GitHub Desktop.
Space Invaders Clone - helping a reddit user fix some drawing issues.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" create an empty pygame window by creating a class to represent the game""" | |
import sys | |
import pygame | |
from settings import Settings | |
from ship import Ship | |
class AlienInvasion: | |
"""Overall class to mange game assets and behavior.""" | |
def __init__(self): | |
"""initialize the game, and create game resources.""" | |
pygame.init() | |
# create the game screen with the size of 1200x800 from settings.py then assign it to an attribute to call | |
# throughout the program | |
self.settings = Settings() | |
# draw the background image | |
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height)) | |
pygame.display.set_caption("Alien Invasion") | |
self.ship = Ship(self) | |
self.clock = pygame.time.Clock() # add clock here in the main program controller | |
self.dt = 0.016 # add the delta time here, we can init with 0.016 as it is 60/1000 (60 fps) | |
def run_game(self): | |
"""Start the main loop for the game""" | |
# redraw the screen during each pass thought the loop and Set the background color from settings.py. | |
self.screen.blit(self.settings.bg_img, (0, 0)) | |
while True: | |
self._check_events() | |
self.ship.update(self.dt) | |
self._update_screen() | |
def _check_events(self): | |
"""respond to key presses and mouse events.""" | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
# if the exit button on screen is clicked close the program | |
sys.exit() | |
elif event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_RIGHT: | |
# if right arrow is pressed move right. | |
self.ship.moving_right = True | |
elif event.key == pygame.K_LEFT: | |
# if left arrow is pressed move left | |
self.ship.moving_left = True | |
elif event.type == pygame.KEYUP: | |
if event.key == pygame.K_RIGHT: | |
self.ship.moving_right = False | |
elif event.key == pygame.K_LEFT: | |
self.ship.moving_left = False | |
def _update_screen(self): | |
# draw the ship | |
self.ship.blitme() | |
# make the most recently drawn screen visible. | |
pygame.display.flip() | |
self.dt = self.clock.tick(60) / 1000 # update the time delta, you can do this here or at the end of run_game's loop | |
if __name__ == '__main__': | |
# make a game instance, and run the game. | |
ai = AlienInvasion() | |
ai.run_game() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
class Settings: | |
"""A class to store all settings for Alien Invasion""" | |
def __init__(self): | |
"""Initialize the games's settings.""" | |
# Screen settings | |
self.screen_width = 1200 | |
self.screen_height = 800 | |
self.bg_color = (230, 230, 230) | |
self.bg_img = pygame.image.load("path/to/your/image.png") # fix this image | |
self.bg_color = (230, 230, 230) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
from settings import Settings | |
class Ship: | |
"""A class to mange the ship.""" | |
def __init__(self, ai_game): | |
"""Initialize the ship and set its starting position.""" | |
self.screen = ai_game.screen | |
self.screen_rect = ai_game.screen.get_rect() | |
self.settings = Settings() | |
# Load the ship image and get its rect. | |
self.image = pygame.image.load('path/to/image.png') # fix this path | |
self.rect = self.image.get_rect() | |
self.speed = 100 # we add a speed property here, we'll multi by time delta for smooth FPS independent movement | |
# create a flag to check if there is movement | |
self.moving_right = False | |
self.moving_left = False | |
# Start each new ship at the bottom center of the screen. | |
self.rect.midbottom = self.screen_rect.midbottom | |
self.abs_x = float(self.rect.x) # make a floating point absolute X position property here to track exact movement | |
self._last_draw_rect = self.rect.copy() # we also want to init our last drawing point | |
def update(self, dt): | |
"""Update the ship's position based on the movement flag.""" | |
if self.moving_right: | |
self.abs_x += self.speed * dt # this is where the time delta step is calculated | |
if self.moving_left: | |
self.abs_x -= self.speed * dt # and here for the other direction | |
self._last_draw_rect = self.rect.copy() # before we move the rect, let's log it's last position | |
self.rect.x = int(self.abs_x) # now adjust the rect to the absolute position | |
def blitme(self): | |
"""Draw the ship at its current location.""" | |
# below, instead of drawing to the rect position, we want to draw to the last rect position to fully overwrite | |
# the previous frames draw data | |
self.screen.blit(self.settings.bg_img, self._last_draw_rect, self._last_draw_rect) | |
# now we can draw the ship as though the screen was empty | |
self.screen.blit(self.image, self.rect) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment