[python] Games-play_plane

Source: Internet
Author: User
Tags set background

alien_invasion.py

ImportSYSImportPygame fromSettingsImportSettings fromShipImport shipImportgame_functions as GF fromPygame.spriteImportGroupdefrun_game ():#Initialize the game and create a screen objectpygame.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 shipship =ship (ai_settings, Screen)#create a stored bullet groupBullets =Group ()#Set Background colorBg_color = (230, 230, 230)    #start the main loop of the game     whileTrue:gf.check_events (ai_settings, screen, ship, bullets) ship.update () gf.update_bullets (bullet S) gf.update_screen (ai_settings, screen, ship, bullets) run_game ()

bullet.py

ImportPygame fromPygame.spriteImportSpriteclassBullet (Sprite):def __init__(self, ai_settings, screens, 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#stores the bullet position in decimal notationSELF.Y =float (self.rect.y) Self.color=Ai_settings.bullet_color Self.speed_factor=Ai_settings.bullet_speed_factordefUpdate (self): SELF.Y-=Self.speed_factor#Update Bullet PositionSelf.rect.y =Self.ydefdraw_bullet (self): pygame.draw.rect (self.screen, self.color, Self.rect)

Game_functions

ImportSYSImportPygame fromBulletImportBulletdefcheck_events (ai_settings, screen, ship, bullets):#listening for keyboard and mouse events     forEventinchPygame.event.get ():ifEvent.type = =Pygame. QUIT:sys.exit ()elifEvent.type = =Pygame. Keydown:check_keydown_events (event, ai_settings, screen, ship, bullets)elifEvent.type = =Pygame. Keyup:check_keyup_events (event, Ship)defupdate_screen (ai_settings, screen, ship, bullets):#Redraw the screen every time you cycleScreen.fill (ai_settings.bg_color)#redraw all the bullets behind the spaceship and the Aliens .     forBulletinchbullets.sprites (): bullet.draw_bullet () ship.blitme ( )#make the recently drawn screen visiblePygame.display.flip ()defcheck_keydown_events (event, ai_settings, screen, ship, bullets):ifEvent.key = =Pygame. K_RIGHT:ship.moving_right=TrueelifEvent.key = =Pygame. K_LEFT:ship.moving_left=TrueelifEvent.key = =Pygame. K_space:#Create a bullet and add it to the group Bulletsfire_bullet (ai_settings, screen, ship, bullets)defcheck_keyup_events (event, ship):ifEvent.key = =Pygame. K_RIGHT:ship.moving_right=FalseelifEvent.key = =Pygame. K_LEFT:ship.moving_left=Falsedefupdate_bullets (bullets): bullets.update ()#remove bullets that have disappeared     forBulletinchbullets.copy ():ifBullet.rect.bottom <=0:bullets.remove (bullet)deffire_bullet (ai_settings, screen, ship, bullets):ifLen (bullets) <Ai_settings.bullets_allowed:new_bullet=Bullet (ai_settings, screen, ship) bullets.add (new_bullet)

settings.py

 class   Settings ():  def   #   screen settings  self.screen_width = 1200 s Elf.screnn_height  = 800 self.bg_color  = (n, v, 230) self.ship_speed_factor  = 1.5 #   Bullet set  self.bullet_speed_factor = 1 self.bullet_width  = 3 Self.bullet_height  = 15 Self.bullet_color  = $, 60 self.bullets_allowed  = 5 

ship.py

ImportPygameclassship ():def __init__(self, ai_settings, screen): self.screen=screen self.ai_settings=ai_settings#load the spaceship image and get the outer rectangleSelf.image = Pygame.image.load ('images/ship.bmp') Self.rect=self.image.get_rect () Self.screen_rect=Screen.get_rect ()#put each new spaceship in the center of the bottom of the screen .Self.rect.centerx =Self.screen_rect.centerx Self.rect.bottom=Self.screen_rect.bottom#storing small values in the Ship's property centerSelf.center =float (self.rect.centerx)#Mobile IdentitySelf.moving_right =False Self.moving_left=FalsedefBlitme (self):#draw a spaceship at the specified position on the screenself.screen.blit (self.image, Self.rect)defUpdate (self):ifSelf.moving_right andSelf.rect.right <Self.screen_rect.right:self.center+=Self.ai_settings.ship_speed_factorelifSelf.moving_left andSelf.rect.left >0:self.center-=Self.ai_settings.ship_speed_factor#Update Rect ObjectSelf.rect.centerx = Self.center

[python] Games-play_plane

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.