Aircraft War Project

Source: Internet
Author: User

Learning a small Python Project
Project name: Aircraft War Project
Project language: Python
Function: The plane continuously launches three bullets, hitting the enemy plane, destroying the enemy plane, hitting the enemy plane, destroying the plane, and ending the game.

Import pygamefrom plane_sprites import * class planegame (object): '''aircraft vs. Primary game''' def _ init _ (Self): Print ("game initialization ") #1. Create the game window self. screen = pygame. display. set_mode (screen_rect.size) #2. Create the Game clock self. clock = pygame. time. clock () #3. Call the private method, self of the genie and the genie group. _ create_sprites () #4. set a timer event --- create an enemy pygame. time. set_timer (create_enemy_event, 1000) pygame. time. set_timer (hero_fire_event, 500) def _ create_sprites (Self): # create a background Genie and a Sprite group Bg1 = backgroud () bg2 = backgroud (true) self. back_group = pygame. sprite. group (Bg1, bg2) # create an enemy's genie group self. enemy_group = pygame. sprite. group () # create the hero's genie and the genie group self. hero = hero () self. hero_group = pygame. sprite. group (self. hero) def start_game (Self): Print ("start of the game... ") While true: #1, set the refresh frequency self. clock. tick (frame_pre_sec) #2. event listening self. _ event_hander () #3. Collision Detection self. _ check_collide () #4. Follow the new/drawn sprite group self. _ update_sprites () #5. The update display is pygame. display. update () def _ event_hander (Self): For event in pygame. event. get (): # determine whether to exit if event. type = pygame. quit: planegame. _ game_over () Elif event. type = create_enemy_event: # create an enemy plane genie enemy = enemy () # Add the enemy plane genie to the enemy plane genie group self. enemy_group.add (enemy) # print ("out of enemy planes... ") Elif event. type = hero_fire_event: Self. hero. fire () # Elif event. type = pygame. Keydown and event. key = pygame. k_right: # print ("Move to the right") # Use the method provided by the keyboard to obtain the keyboard buttons --- keys_pressed = pygame. key. get_pressed () # judge the key index value 1 If keys_pressed [pygame. k_right]: Self. hero. speed = 2 Elif keys_pressed [pygame. k_left]: Self. hero. speed =-2 else: Self. hero. speed = 0 def _ check_collide (Self): #1. The bullet destroys the enemy pygame. sprite. groupcollide (self. hero. bullets, self. enemy_group, true, true) #2. attack the enemy Xiong enemise = pygame. sprite. spritecollide (self. hero, self. enemy_group, true) #3. Check whether the list contains content. If yes, if Len (enemise)> 0: Self. hero. kill () # end the game planegame. _ game_over () def _ update_sprites (Self): Self. back_group.update () self. back_group.draw (self. screen) self. enemy_group.update () self. enemy_group.draw (self. screen) self. hero_group.update () self. hero_group.draw (self. screen) self. hero. bullets. update () Self. Hero. Bullets. Draw (self. Screen) @ staticmethod def _ game_over (): Print ("the game is over .. ") Pygame. quit () Exit () If _ name _ = '_ main _': # Game initialization game = planegame () # Start game. start_game ()
Import randomimport pygame # The constant screen_rect that defines the module size = pygame. rect (0, 0,480,700) # define the refresh Frame Rate frame_pre_sec = 60 # create an enemy's timer constant create_enemy_event = pygame. userevent # hero_fire_event = pygame. userevent + 1 class gamescript (pygame. sprite. sprite): "" def _ init _ (self, image_name, speed = 1): # Call the initialization method of the parent class super (). _ init _ () # define the object property self. image = pygame. image. load (image_name) self. rect = self. image. get_rect () self. speed = speed def Update (Self): # Move self vertically on the screen. rect. Y + = self. speedclass backgroud (gamescript): "game background Genie" def _ init _ (self, is_alt = false): #1. Call the parent class method, implement the creation of the genie (images/background.png super (). _ init __(". /images/background.png ") #2. Determine whether the image is an alternate image. If yes, set the initial position if is_alt: Self. rect. y =-self. rect. height def Update (Self): #1. Call the method of the parent class to implement super (). update () #2. Determine whether to remove the image from the screen. If it is removed from the screen, set the image to the top if self. rect. y> = screen_rect.height: Self. rect. y =-self. rect. heightclass enemy (gamescript): "" "def _ init _ (Self): #1. Call the parent class method to create an, specify the enemy image super () at the same time (). _ init __(". /images/enemy1.png ") #2. Specify the initial Random speed of the enemy machine self. speed = random. randint (2, 4) #3, specifying the random location of the first known enemy machine # self. rect. y =-self. rect. height is equivalent to the following statement self. rect. bottom = 0 max_x = screen_rect.width-self. rect. width self. rect. X = random. randint (0, max_x) def Update (Self): #1. Call the parent class method to keep the vertical flight super (). update () #2. Determine whether to fly out of the screen. If yes, you need to delete the enemy from the genie group if self. rect. y> = screen_rect.height: Print ("Exit screen, delete from the genie group") # kill method. You can remove the genie from the genie group and the genie will be destroyed. kill () def _ del _ (Self): Print ("% s" % self. rect) class hero (gamescript): "Hero Genie" def _ init _ (Self): #1. Call the parent class method and set images super (). _ init __(". /images/me1.png ") self. speed = 0 #2, set the hero's initial position self. rect. centerx = screen_rect.centerx self. rect. bottom = screen_rect.bottom-120 #3. Create a bullet genie group self. bullets = pygame. sprite. group () def Update (Self): # The hero moves self horizontally. rect. X + = self. speed # The control hero cannot leave the screen if self. rect. x <0: Self. rect. X = 0 Elif self. rect. right> screen_rect.right: Self. rect. right = screen_rect.right def fire (Self): Print ("fired bullets... ") for I In (1, 2, 3): #1, create the bullet genie bullet = bullet () #2, and set the initial position of the bullet genie bullet. rect. centerx = self. rect. centerx bullet. rect. bottom = self. rect. y-I * 20 #3. Add the genie to the genie group self. bullets. add (bullet) Class bullet (gamescript): '''bullet genie ''' def _ init _ (Self): # Call the parent class method and set the bullet image, set the initial speed to super (). _ init __(". /images/bullet1.png ",-2) def Update (Self): # Call the parent class method to let the bullet fly vertically in super (). update () # determine whether a bullet is flying out of the screen if self. rect. bottom <0: Self. kill ()

This is version 1.0 of the project. The project is not particularly well-developed and needs to be improved. Version 2.0 is released.

Aircraft War Project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.