[Python] games and python games

Source: Internet
Author: User

[Python] games and python games

 

Alien_invasion.py

Import sysimport pygamefrom settings import Settingsfrom ship import Shipimport game_functions as gffrom pygame. sprite import Groupdef run_game (): # initialize the game and create a Screen Object pygame. init () ai_settings = Settings () screen = pygame. display. set_mode (ai_settings.screen_width, ai_settings.screnn_height) pygame. display. set_caption ('alien Invasion ') # create a ship = Ship (ai_settings, screen) # create a storage bullet Group bullets = Group () # Set the background color bg_color = (230,230,230) # Start the main loop of the game while True: gf. check_events (ai_settings, screen, ship, bullets) ship. update () gf. update_bullets (bullets) gf. update_screen (ai_settings, screen, ship, bullets) run_game ()

 

Bullet. py

Import pygamefrom pygame. sprite import Spriteclass Bullet (Sprite): def _ init _ (self, ai_settings, screen, ship): super (Bullet, self ). _ init _ () self. screen = screen self. rect = pygame. rect (0, 0, ai_settings.bullet_width, ai_settings.bullet_height) self. rect. centerx = ship. rect. centerx self. rect. top = ship. rect. top # store the position self of the bullet represented by decimal places. y = float (self. rect. y) self. color = ai_settings.bullet_color self. speed_factor = ai_settings.bullet_speed_factor def update (self): self. y-= self. speed_factor # update the bullet position self. rect. y = self. y def draw_bullet (self): pygame. draw. rect (self. screen, self. color, self. rect)

 

Game_functions

Import sysimport pygamefrom bullet import Bulletdef check_events (ai_settings, screen, ship, bullets): # Listen to keyboard and mouse events for event in pygame. event. get (): if event. type = pygame. QUIT: sys. exit () elif event. type = pygame. KEYDOWN: check_keydown_events (event, ai_settings, screen, ship, bullets) elif event. type = pygame. KEYUP: check_keyup_events (event, ship) def update_screen (ai_settings, screen, ship, bullets): # re-paint the screen every cycle. fill (ai_settings.bg_color) # repaint all bullets for bullet in bullets behind the ship and aliens. sprites (): bullet. draw_bullet () ship. blitme () # Make the recently drawn screen visible to pygame. display. flip () def check_keydown_events (event, ai_settings, screen, ship, bullets): if event. key = pygame. k_RIGHT: ship. moving_right = True elif event. key = pygame. k_LEFT: ship. moving_left = True elif event. key = pygame. k_SPACE: # create a bullet and add it to the bullets group. fire_bullet (ai_settings, screen, ship, bullets) def check_keyup_events (event, ship): if event. key = pygame. k_RIGHT: ship. moving_right = False elif event. key = pygame. k_LEFT: ship. moving_left = Falsedef update_bullets (bullets): bullets. update () # delete a deleted bullet in bullets. copy (): if bullet. rect. bottom <= 0: bullets. remove (bullet) def fire_bullet (ai_settings, screen, ship, bullets): if len (bullets) <ai_settings.bullets_allowed: new_bullet = Bullet (ai_settings, screen, ship) bullets. add (new_bullet)

Settings. py

Class Settings (): def _ init _ (self): # Screen Settings self. screen_width = 1200 self. screnn_height = 800 self. bg_color = (230,230,230) self. ship_speed_factor = 1.5 # Set the bullet self. bullet_speed_factor = 1 self. bullet_width = 3 self. bullet_height = 15 self. bullet_color = 60, 60, 60 self. bullets_allowed = 5

Ship. py

Import pygameclass Ship (): def _ init _ (self, ai_settings, screen): self. screen = screen self. ai_settings = ai_settings # load the spacecraft image and obtain its external Rectangle self. image = pygame. image. load ('images/ship.bmp ') self. rect = self. image. get_rect () self. screen_rect = screen. get_rect () # Place each new ship in the center of the screen self. rect. centerx = self. screen_rect.centerx self. rect. bottom = self. screen_rect.bottom # store small numbers of self in the spacecraft property center. center = float (self. rect. centerx) # Move the ID self. moving_right = False self. moving_left = False def blitme (self): # Draw the ship self at the specified position on the screen. screen. BITs (self. image, self. rect) def update (self): if self. moving_right and self. rect. right <self. screen_rect.right: self. center + = self. ai_settings.ship_speed_factor elif self. moving_left and self. rect. left> 0: self. center-= self. ai_settings.ship_speed_factor # update the rect object self. rect. centerx = self. center

 

Related Article

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.