4/14/2023 0 Comments Alien invasion gameWe need to delete the missing bullets, otherwise the game will do more and more unnecessary work, and the process will be slower and slower. For the screen we built, the coordinates in the upper left corner are (0, 0), and the lower right corner is the width and height we set. Their y-axis coordinates will get smaller and smaller. To draw and fire all bullets on the screen, traverse the sprites in the group bullets, and then call draw_bullet().Īt present, the bullet will disappear at the top of the screen, but it does not mean that it has been deleted, because our screen does not contain them, they still exist. The method bullets.sprites() returns a list containing all the sprites in the grouped bullets. The method add () is similar to append (), but is written specifically for Pygame marshalling.įinally, to display the bullet, we need to write in the update screen: for bullet in (): Then define a new method: def _fire_bullet(self): Write when pressed: elif event.key = pygame.K_SPACE: ![]() When the space button is pressed, a bullet is created and fired. ![]() In the main loop, this group is used to draw bullets on the screen and update the position of each bullet. ![]() is similar to a list, but provides additional features to help develop games. This group is used to store all valid bullets in order to manage the bullets fired. (self.screen,lor,self.rect) #Draw bulletsĪfter that, create a group for storing bullets in the function: = ai_ #Located in the middle and upper part of the spacecraft Self.rect = pygame.Rect(0,0,_width,_height) #Create a rectangle representing the bullet at (0, 0) and set the correct position Mask the initial display, and then add the above three sentences. Self.screen = _mode((0, 0), pygame.FULLSCREEN) # self.screen = _mode((_width, _height)) # Set game screen size You can also set the speed of the spaceship, and add in the settings: #Spacecraft speedĪfter that, change 1 in the update speed to _ Before replacing the speed, do the following:Īdd the following statement to the operation of pressing the key: elif event.key = pygame.K_q: Similarly, we can set up, down, left and right buttons, and set them not to exceed the screen size: def update(self): The flag bit added is _right, respond when the user presses the right button, set it to False when it is released, and add an update function to the class of the spacecraft: def update(self): In check_ Add to event: def _check_event(self): KEYUP: triggered when the user releases the key on the keyboard. KEYDOWN: triggered when the user presses any key on the keyboard. Make it move continuously: when pressed, trigger a flag bit to true. Then write it into the main function and click Run to see the spacecraft in the center of the screen: = self.screen_rect.midbottom # Put it in the center at the bottom of the screen Self.rect = _rect() # Specify ship location Self.image = ('Images/ship.bmp') # import images Self.screen_rect = ai_rect() # Set initial position Self.screen = ai_game.screen # Initialize spacecraft we set above into this file: class Settings:Ĭreate a new Images folder and add the ship file ship.bmp to it. ![]() (self.bg_color) # Cycle through the screen each timeĬreate a file named settings.py to import new settings when new features are added to the game. If event.type = pygame.QUIT: # Is the close key pressed Self.bg_color = (230, 230, 230) # Set background colorįor event in (): # Monitor keyboard and mouse _caption("Alien Invasion") # Set game name Self.screen = _mode((1200, 800)) # Set game screen size Pygame.init() # Initialize the game and create game resources (1) Create Pygame window and respond to user inputĬreate a file named alien_ The file of invasion.py is the main running file of the program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |