class SpaceRocks: MIN_ASTEROID_DISTANCE = 250 def __init__(self): self._init_pygame() self.screen = pygame.display.set_mode((800, 600)) self.background = load_sprite("space", False) self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 64) self.message = "" self.asteroids = [] self.bullets = [] #self.asteroids = [Asteroid(get_random_position(self.screen)) for _ in range(6)] self.spaceship = Spaceship((400, 300), self.bullets.append) for _ in range(2): while True: position = get_random_position(self.screen) if position.distance_to( self.spaceship.position) > self.MIN_ASTEROID_DISTANCE: break self.asteroids.append(Asteroid(position, self.asteroids.append)) def main_loop(self): while True: self._handle_input() self._process_game_logic() self._draw() def _init_pygame(self): pygame.init() pygame.display.set_caption("Space Rocks") def _handle_input(self): for event in pygame.event.get(): if event.type == pygame.QUIT or \ (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): quit() elif (self.spaceship and event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE): self.spaceship.shoot() is_key_pressed = pygame.key.get_pressed() if self.spaceship: if is_key_pressed[pygame.K_RIGHT]: self.spaceship.rotate(clockwise=True) elif is_key_pressed[pygame.K_LEFT]: self.spaceship.rotate(clockwise=False) if is_key_pressed[pygame.K_UP]: self.spaceship.accelerate() def _process_game_logic(self): for game_object in self._get_game_objects(): game_object.move(self.screen) #self.asteroid.move() if self.spaceship: for asteroid in self.asteroids: if asteroid.collides_with(self.spaceship): self.spaceship = None self.message = "You lost!" break for bullet in self.bullets[:]: for asteroid in self.asteroids[:]: if asteroid.collides_with(bullet): self.asteroids.remove(asteroid) self.bullets.remove(bullet) asteroid.split() break for bullet in self.bullets[:]: if not self.screen.get_rect().collidepoint(bullet.position): self.bullets.remove(bullet) if not self.asteroids and self.spaceship: self.message = "You won!" def _draw(self): self.screen.blit(self.background, (0, 0)) #self.asteroid.draw(self.screen) for game_object in self._get_game_objects(): game_object.draw(self.screen) if self.message: print_text(self.screen, self.message, self.font) pygame.display.flip() self.clock.tick(60) def _get_game_objects(self): game_objects = [*self.asteroids, *self.bullets] if self.spaceship: game_objects.append(self.spaceship) return game_objects
class Meteoroids: MIN_METEROID_DIS = 300 WIDTH = 800 HEIGHT = 600 def __init__(self): self.score = 0 self._init_pygame() self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT)) self.background = load_sprite("space1", False) self.background_game_music = load_sound("game_play_music") self.game_over_music = load_sound("game_over_music") self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 64) self.message = "" self.meteroids = [] self.bullets = [] self.spaceship = Spaceship((400, 300), self.bullets.append) for _ in range(4): while True: pos = get_random_position(self.screen) if pos.distance_to(self.spaceship.position) > self.MIN_METEROID_DIS: break self.meteroids.append(Meteroid(pos, self.meteroids.append)) def _get_game_objects(self): game_objs = [*self.meteroids, *self.bullets] if self.spaceship: game_objs.append(self.spaceship) return game_objs def _welcome_screen(self): welcome_message = """ PIZZA DOG """ intro_music = load_sound("welcome_screen_music") intro_music.play() self.screen.blit(self.background, (0, 0)) draw_text(self.screen, welcome_message, self.font, self.WIDTH/2, self.HEIGHT/3) draw_text(self.screen, "Press [ENTER] To Begin", self.font, self.WIDTH/2, (self.HEIGHT/2)+40) intro = True while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: quit() elif event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: intro_music.stop() intro = False pygame.display.flip() def game_loop(self): #intro_loop self._welcome_screen() self.background_game_music.play() while True: self._handle_input() self._process_game_logic() self._draw() def _init_pygame(self): pygame.init() pygame.display.set_caption("Meteriods") def _handle_input(self): for event in pygame.event.get(): if event.type == pygame.QUIT or ( event.type == pygame.KEYDOWN and event.key == pygame.K_q ): quit() elif( self.spaceship and event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE ): self.spaceship.shoot() is_key_pressed = pygame.key.get_pressed() if self.spaceship: if is_key_pressed[pygame.K_RIGHT]: self.spaceship.rotate(clockwise=True) elif is_key_pressed[pygame.K_LEFT]: self.spaceship.rotate(clockwise=False) if is_key_pressed[pygame.K_UP]: self.spaceship.accelerate() elif is_key_pressed[pygame.K_DOWN]: self.spaceship.reverse() def _process_game_logic(self): for game_object in self._get_game_objects(): game_object.move(self.screen) if self.spaceship: for meteroid in self.meteroids: if meteroid.collides_with(self.spaceship): self.spaceship.explosion_sound.play() self.background_game_music.stop() self.game_over_music.play() self.spaceship = None self.message = "Game Over... Final Score: " + str(self.score) break for bullet in self.bullets[:]: for meteroid in self.meteroids[:]: if meteroid.collides_with(bullet): meteroid_size = meteroid.size if meteroid_size == 3: self.score += 3 elif meteroid_size == 2: self.score += 2 else: self.score += 1 meteroid.explosion_sound.play() self.meteroids.remove(meteroid) self.bullets.remove(bullet) meteroid.split() break for bullet in self.bullets[:]: if not self.screen.get_rect().collidepoint(bullet.position): self.bullets.remove(bullet) if not self.meteroids and self.spaceship: self.message = "You Won!" def _display_score(self): current_score = self.font.render("Score: " + str(self.score), 1, Color("white")) self.screen.blit(current_score, (self.WIDTH - 10 - current_score.get_width(), 10)) def _draw(self): self.screen.blit(self.background, (0, 0)) self._display_score() for game_object in self._get_game_objects(): game_object.draw(self.screen) if self.message: print_text(self.screen, self.message, self.font) pygame.display.flip() self.clock.tick(60)
class AsteroidClash: MIN_ASTEROID_DISTANCE = 250 # Initializing def __init__(self) -> None: self._init_pygame() self.screen = pygame.display.set_mode((800, 600)) self.background = load_sprite(name='space', with_alpha=False) self.bullets = [] self.asteroids = [] self.spaceship = Spaceship((400, 300), self.bullets.append) for _ in range(6): while True: position = get_random_position(self.screen) if (position.distance_to(self.spaceship.position) > self.MIN_ASTEROID_DISTANCE): break self.asteroids.append(Asteroid(position, self.asteroids.append)) self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 64) self.message = '' # Defining Main Loop for the game def main_loop(self) -> None: while True: self._handle_input() self._process_game_logic() self._draw() # Initializing Pygame def _init_pygame(self) -> None: pygame.init() pygame.display.set_caption('Asteroid Clash') # Defining function for handling input def _handle_input(self) -> None: for event in pygame.event.get(): # Exiting the game if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): quit() elif (self.spaceship and event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE): self.spaceship.shoot() # Handling Inputs is_key_pressed = pygame.key.get_pressed() if self.spaceship: if is_key_pressed[pygame.K_RIGHT]: self.spaceship.rotate(clockwise=True) elif is_key_pressed[pygame.K_LEFT]: self.spaceship.rotate(clockwise=False) elif is_key_pressed[pygame.K_UP]: self.spaceship.accelerate() # Defining function for getting all game objects def _get_game_objects(self): game_objects = [*self.asteroids] game_objects = [*self.asteroids, *self.bullets] if self.spaceship: game_objects.append(self.spaceship) return game_objects # Defining function for processing game logic def _process_game_logic(self) -> None: for game_object in self._get_game_objects(): game_object.move(self.screen) if self.spaceship: for asteroid in self.asteroids: if asteroid.collides_with(self.spaceship): self.spaceship = None self.message = 'YOU LOST!' break for bullet in self.bullets[:]: if not self.screen.get_rect().collidepoint(bullet.position): self.bullets.remove(bullet) for bullet in self.bullets[:]: for asteroid in self.asteroids[:]: if asteroid.collides_with(bullet): self.asteroids.remove(asteroid) self.bullets.remove(bullet) asteroid.split() break if not self.asteroids and self.spaceship: self.message = 'YOU WON!' # Defining function for drawing elements on screen def _draw(self) -> None: self.screen.blit(self.background, (0, 0)) for game_object in self._get_game_objects(): game_object.draw(self.screen) if self.message: print_text(self.screen, self.message, self.font) pygame.display.flip() self.clock.tick(60)
class SpaceRocks: MIN_ASTEROID_DISTANCE = 250 def __init__(self): self._init_pygame() # creates a display surface. Images in Pygame are represented by surfaces. # Here are a few things to know about them: # x Surfaces can be drawn on one another, allowing you to create complex # scenes from simple pictures. # # x There’s one special surface in each Pygame project. That surface represents # the screen and is the one that will eventually be displayed to players. # All other surfaces have to be drawn on this one at some point. # Otherwise, they won’t be shown. # # x To create the display surface, your program uses pygame.display.set_mode(). # The only argument you pass to this method is the size of the screen, # represented by a tuple of two values: width and height. # In this case, Pygame will create a screen with a width of 800 pixels # and a height of 600 pixels. self.screen = pygame.display.set_mode((800, 600)) self.background = load_sprite("space", False) self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 64) self.message = "" self.asteroids = [] self.bullets = [] self.spaceship = Spaceship((400, 300), self.bullets.append) for _ in range(6): while True: position = get_random_position(self.screen) if (position.distance_to(self.spaceship.position) > self.MIN_ASTEROID_DISTANCE): break self.asteroids.append(Asteroid(position, self.asteroids.append)) # self.asteroid = GameObject( # (400, 300), load_sprite("asteroid"), (1, 0) # ) def main_loop(self): while True: self._handle_input() self._process_game_logic() self._draw() def _init_pygame(self): # This single line of code is responsible for setting up # the amazing features of Pygame. # Every time you work with Pygame, you should call pygame.init() # at the beginning of your program to make sure # that the framework will work correctly. pygame.init() pygame.display.set_caption("Space Rocks") def _handle_input(self): # It happens when someone requests the program to end, # either by clicking Close or by pressing Alt+F4 # on Windows and Linux or Cmd+W on macOS. Or Esc for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): quit() elif (self.spaceship and event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE): self.spaceship.shoot() is_key_pressed = pygame.key.get_pressed() if self.spaceship: if is_key_pressed[pygame.K_RIGHT]: self.spaceship.rotate(clockwise=True) elif is_key_pressed[pygame.K_LEFT]: self.spaceship.rotate(clockwise=False) if is_key_pressed[pygame.K_UP]: self.spaceship.accelerate() elif is_key_pressed[pygame.K_DOWN]: self.spaceship.slow_down() def _get_game_objects(self): game_objects = [*self.asteroids, *self.bullets] if self.spaceship: game_objects.append(self.spaceship) return game_objects def _process_game_logic(self): for game_object in self._get_game_objects(): game_object.move(self.screen) if self.spaceship: for asteroid in self.asteroids: if asteroid.collides_with(self.spaceship): self.spaceship = None self.message = "You lost" break for bullet in self.bullets[:]: for asteroid in self.asteroids[:]: if asteroid.collides_with(bullet): self.asteroids.remove(asteroid) self.bullets.remove(bullet) asteroid.split() break for bullet in self.bullets[:]: if not self.screen.get_rect().collidepoint(bullet.position): self.bullets.remove(bullet) if not self.asteroids and self.spaceship: self.message = "You won" def _draw(self): # fills the screen with a color using screen.fill(). The method takes a tuple with three values, # representing three base colors: red, green, and blue. Each color value ranges between 0 and 255, # representing its intensity. In this example, a tuple of (0, 0, 255) means # that the color will consist only of blue, with no traces of red or green. # self.screen.fill((0, 0, 255)) # To display one surface on another in Pygame, you need to call blit() # on the surface you want to draw on. This method takes two arguments: # x The surface that you want to draw # x The point where you want to draw it self.screen.blit(self.background, (0, 0)) for game_object in self._get_game_objects(): game_object.draw(self.screen) # self.asteroid.draw(self.screen) # updates the content of the screen using pygame.display.flip(). Because your game will # eventually display moving objects, you’ll call this method every frame to update # the display. Because of this, you need to fill your screen with color every frame, # as the method will clear the contents generated during the previous frame. if self.message: print_text(self.screen, self.message, self.font) pygame.display.flip() self.clock.tick(60)
class SpaceRocks: MIN_ASTEROID_DISTANCE = 250 def __init__(self): self._init_pygame() self.screen = screenSize(1000, 700) self.background = load_sprite("space", False) self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 64) self.message = "" self.destroyed_small = 0 self.score = 0 self.scoretext = self.font.render("Score = " + str(self.score), 1, (255, 0, 0)) file = 'C://Users//ActionICT//PycharmProjects//Asteroids//assets//music//jlbrock44_-_Stars_Below_Us.mp3' pygame.init() pygame.mixer.init() pygame.mixer.music.load(file) pygame.mixer.music.play( -1) # If the loops is -1 then the music will repeat indefinitely. self.asteroids = [] self.bullets = [] self.spaceship = Spaceship((400, 300), self.bullets.append) for _ in range(6): while True: position = get_random_position(self.screen) if (position.distance_to(self.spaceship.position) > self.MIN_ASTEROID_DISTANCE): break self.asteroids.append(Asteroid(position, self.asteroids.append)) def main_loop(self): while True: self._handle_input() self._process_game_logic() self._draw() def _init_pygame(self): pygame.init() pygame.display.set_caption("Space Rocks") def _handle_input(self): for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): quit() elif (self.spaceship and event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE): self.spaceship.shoot() is_key_pressed = pygame.key.get_pressed() if self.spaceship: if is_key_pressed[pygame.K_RIGHT]: self.spaceship.rotate(clockwise=True) elif is_key_pressed[pygame.K_LEFT]: self.spaceship.rotate(clockwise=False) if is_key_pressed[pygame.K_UP]: self.spaceship.accelerate() def _process_game_logic(self): for game_object in self._get_game_objects(): game_object.move(self.screen) if self.spaceship: for asteroid in self.asteroids: if asteroid.collides_with(self.spaceship): self.spaceship = None #Sound('C:/Users/ActionICT/PycharmProjects/Asteroids/assets/sounds/Blastwave_FX_BankSafeExplosion_HV.37.mp3').play() self.message = FinalScreen.PROTOTYPE_FINAL_DISPLAY.format( FinalScreen.LOST_MESSAGE, FinalScreen.MESSAGE_ESC_OR_CONTINUE) + ' ' + str( self.score) break for bullet in self.bullets[:]: for asteroid in self.asteroids[:]: if asteroid.collides_with(bullet): if asteroid.size == 1: Sound( 'C:/Users/ActionICT/PycharmProjects/Asteroids/assets/sounds/firework_explosion_001.mp3' ).play() self.destroyed_small += 1 if self.destroyed_small == 4: while True: position = get_random_position(self.screen) if (position.distance_to( self.spaceship.position) > self.MIN_ASTEROID_DISTANCE): break self.destroyed_small = 0 self.asteroids.append( Asteroid(position, self.asteroids.append)) elif asteroid.size == 3: Sound( 'C:/Users/ActionICT/PycharmProjects/Asteroids/assets/sounds/zapsplat_explosion_large.mp3' ).play() else: Sound( 'C:/Users/ActionICT/PycharmProjects/Asteroids/assets/sounds/boom.mp3' ).play() self.asteroids.remove(asteroid) self.bullets.remove(bullet) asteroid.split() self.score += 10 break for bullet in self.bullets[:]: if not self.screen.get_rect().collidepoint(bullet.position): self.bullets.remove(bullet) if not self.asteroids and self.spaceship: self.message = FinalScreen.PROTOTYPE_FINAL_DISPLAY.format( FinalScreen.WON_MESSAGE, FinalScreen.MESSAGE_ESC_OR_CONTINUE) def _draw(self): self.screen.blit(self.background, (0, 0)) # self.spaceship.draw(self.screen) # self.asteroid.draw(self.screen) for game_object in self._get_game_objects(): game_object.draw(self.screen) self.screen.blit(self.scoretext, (500, 10)) self.scoretext = self.font.render("Score = " + str(self.score), 1, (255, 0, 0)) pygame.display.flip() if self.message: test_label = makeLabel(self.message, 60, 400, 300, fontColour="red", font='juiceitc', background='clear') moveLabel(test_label, test_label.rect.topleft[0] - test_label.rect.width / 2, test_label.rect.topleft[1] - test_label.rect.height / 2) self.screen.fill("black") showLabel(test_label) pygame.mixer.music.stop() while True: for event in pygame.event.get(): if event.type == pygame.QUIT or ( event.type == pygame.KEYDOWN and (event.key == pygame.K_ESCAPE)): quit() elif (event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN): hideLabel(test_label) self.__init__() self.main_loop() self.clock.tick(60) def _get_game_objects(self): game_objects = [*self.asteroids, *self.bullets] if self.spaceship: game_objects.append(self.spaceship) return game_objects
class SpaceRocks: # Class variables MIN_ASTEROID_DISTANCE = 250 # Constructor for instance of our gameplay handler classs def __init__(self): # Call a private initialization method self._init_pygame() self.screen = pygame.display.set_mode((800, 600)) self.background = load_sprite("space", False) self.clock = pygame.time.Clock() # Ensure stable FPS self.font = pygame.font.Font(None, 64) self.message = "" self.asteroids = [] self.bullets = [] self.spaceship = Spaceship((400, 300), self.bullets.append) for _ in range(6): while True: position = get_random_position(self.screen) if (position.distance_to(self.spaceship.position) > self.MIN_ASTEROID_DISTANCE): break self.asteroids.append(Asteroid(position, self.asteroids.append)) # Main game loop happening in three parts: handle inputs, # process game logic, and then draw to the screen def main_loop(self): while True: self._handle_input() self._process_game_logic() self._draw() # Private initialization method def _init_pygame(self): pygame.init() pygame.display.set_caption("Space Rocks") # Handle inputs, using the arrow keys and space bar def _handle_input(self): # Get the events from pygame module for event in pygame.event.get(): # Allow user to quit the game if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): quit() elif (self.spaceship and event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE): self.spaceship.shoot() # Get dictionary of pressed keys is_key_pressed = pygame.key.get_pressed() # If the spaceship isn't destroyed if self.spaceship: # Rotate the ship object appropriately if is_key_pressed[pygame.K_RIGHT]: self.spaceship.rotate(clockwise=True) elif is_key_pressed[pygame.K_LEFT]: self.spaceship.rotate(clockwise=False) # Accelerate the ship if is_key_pressed[pygame.K_UP]: self.spaceship.accelerate() # handle the game logic, account for collisions, etc. def _process_game_logic(self): for game_object in self._get_game_objects(): game_object.move(self.screen) # Check for collisions of spaceship with asteroids if self.spaceship: for asteroid in self.asteroids: if asteroid.collides_with(self.spaceship): self.spaceship = None self.message = "You lost!" break # Check for collisions of bullets with asteroids for bullet in self.bullets[:]: for asteroid in self.asteroids[:]: if asteroid.collides_with(bullet): self.asteroids.remove(asteroid) self.bullets.remove(bullet) asteroid.split() break # Remove bullets which are off the screen for bullet in self.bullets[:]: if not self.screen.get_rect().collidepoint(bullet.position): self.bullets.remove(bullet) if not self.asteroids and self.spaceship: self.message = "You won!" # Update the game drawing def _draw(self): self.screen.blit(self.background, (0, 0)) for game_object in self._get_game_objects(): game_object.draw(self.screen) if self.message: print_text(self.screen, self.message, self.font) pygame.display.flip() self.clock.tick(60) # Set the frame rate to 60 FPS # Helper method to get all the game objects def _get_game_objects(self): game_objects = [*self.asteroids, *self.bullets] if self.spaceship: game_objects.append(self.spaceship) return game_objects
class SpaceRocks: MIN_ASTEROID_DISTANCE = 250 def __init__(self): self._init_pygame() # Adjust Screen Size self.screen = pygame.display.set_mode((800, 600)) # Customize Game Background self.background = load_sprite("industrial-background", False) self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 64) self.message = "" self.asteroids = [] self.bullets = [] # Adjust player starting position: Spaceship((x, y)) # Center of screen: (400, 300) # Far left center of screen: (20, 300) self.spaceship = Spaceship((100, 490), self.bullets.append) # Adjust number fo initial spawned asteroids: for _ in range(x) for _ in range(6): while True: position = get_random_position(self.screen) if (position.distance_to(self.spaceship.position) > self.MIN_ASTEROID_DISTANCE): break self.asteroids.append(Asteroid(position, self.asteroids.append)) def main_loop(self): while True: self._handle_input() self._process_game_logic() self._draw() def _init_pygame(self): pygame.init() pygame.display.set_caption("COVID Destroyer") def _handle_input(self): for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): quit() elif (self.spaceship and event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE): self.spaceship.shoot() is_key_pressed = pygame.key.get_pressed() if self.spaceship: if is_key_pressed[pygame.K_RIGHT]: self.spaceship.rotate(clockwise=True) elif is_key_pressed[pygame.K_LEFT]: self.spaceship.rotate(clockwise=False) if is_key_pressed[pygame.K_UP]: self.spaceship.accelerate() def _process_game_logic(self): for game_object in self._get_game_objects(): game_object.move(self.screen) if self.spaceship: for asteroid in self.asteroids: if asteroid.collides_with(self.spaceship): self.spaceship = None self.message = "GAME OVER" break for bullet in self.bullets[:]: for asteroid in self.asteroids[:]: if asteroid.collides_with(bullet): self.asteroids.remove(asteroid) self.bullets.remove(bullet) asteroid.split() break # Bullets automatically destroyed and don't return when screen is left: for bullet in self.bullets[:]: if not self.screen.get_rect().collidepoint(bullet.position): self.bullets.remove(bullet) # Asteroids automatically destroyed and don't return when screen is left: for asteroid in self.asteroids[:]: if not self.screen.get_rect().collidepoint(asteroid.position): self.asteroids.remove(asteroid) # Logic dictating enemy sprites bounce off top and bottom screen # if asteroid.bottom >= HEIGHT or asteroid.top <= 0: # asteroid.direction = dx, -dy # Game message if all enemy sprites destroyed if not self.asteroids and self.spaceship: self.message = "WINNER" def _draw(self): self.screen.blit(self.background, (0, 0)) for game_object in self._get_game_objects(): game_object.draw(self.screen) if self.message: print_text(self.screen, self.message, self.font) pygame.display.flip() self.clock.tick(60) def _get_game_objects(self): game_objects = [*self.asteroids, *self.bullets] if self.spaceship: game_objects.append(self.spaceship) return game_objects
class SpaceRocks: MIN_ASTEROID_DISTANCE = 250 def __init__(self): self._init_pygame() self.screen = pygame.display.set_mode((800, 600)) self.background = load_sprite("space", False) self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 64) self.message = "" self.asteroids = [] self.bullets = [] self.spaceship = Spaceship((400, 300), self.bullets.append) for _ in range(6): while True: position = get_random_position(self.screen) if (position.distance_to(self.spaceship.position) > self.MIN_ASTEROID_DISTANCE): break self.asteroids.append(Asteroid(position, self.asteroids.append)) def main_loop(self): clock = pygame.time.Clock() clock.tick(120) while True: self._handle_input() self._process_game_logic() self._draw() def _init_pygame(self): pygame.init() pygame.display.set_caption("AstroBit") global rm rm = Read_Microbit() rm.start() def _handle_input(self): for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() quit() rm.terminate() rm.join() elif (q.get().startswith("shot")): self.spaceship.shoot() if self.spaceship: if q.get().startswith("right"): self.spaceship.rotate(clockwise=True) elif q.get().startswith("left"): self.spaceship.rotate(clockwise=False) if q.get().startswith("up"): self.spaceship.accelerate() if q.get().startswith("down"): self.spaceship.decelerate() if q.get().startswith("shot"): self.spaceship.shoot() q.task_done() def _process_game_logic(self): for game_object in self._get_game_objects(): game_object.move(self.screen) if self.spaceship: for asteroid in self.asteroids: if asteroid.collides_with(self.spaceship): self.spaceship = None self.message = "You lost!" break for bullet in self.bullets[:]: for asteroid in self.asteroids[:]: if asteroid.collides_with(bullet): self.asteroids.remove(asteroid) self.bullets.remove(bullet) asteroid.split() break for bullet in self.bullets[:]: if not self.screen.get_rect().collidepoint(bullet.position): self.bullets.remove(bullet) if not self.asteroids and self.spaceship: self.message = "You won!" def _draw(self): self.screen.blit(self.background, (0, 0)) for game_object in self._get_game_objects(): game_object.draw(self.screen) if self.message: print_text(self.screen, self.message, self.font) pygame.display.flip() self.clock.tick(60) def _get_game_objects(self): game_objects = [*self.asteroids, *self.bullets] if self.spaceship: game_objects.append(self.spaceship) return game_objects