World War II aircraft python3

A. Tutorials and configure the source environment
1. environment configuration
Open pycharm, into the file, open settings

Open the project interpreter, click on the top right of the +

Search and import pygame module

This environment it is configured.

2. Use the tutorial source code
to create a new folder named python "aircraft"
and then create a folder named "images" in a new folder, used to store the desired picture game

 

The following images downloaded to your desktop (have to change the name) and drag the "images" folder (drag and drop directly)

The picture renamed background, png format

 

The picture renamed bullet1, png format

 

The picture renamed enemy1, png format

 

The picture renamed me1, png format


Next, create a python file in the "aircraft" folder named "plane_main"
and then copy the following code into it and you're done it! !

II. Source
Import Random
Import the pygame


# Screen size constant
SCREEN_RECT pygame.Rect = (0, 0, 480, 700)
# refresh frame rate
Frame_per_sec = 60
# create the enemy timer constant
CREATE_ENEMY_EVENT = pygame.USEREVENT
# hero fired bullets events
HERO_FIRE_EVENT = pygame.USEREVENT + 1


GameSprite class (pygame.sprite.Sprite):
"" "airplane war game Wizard" ""

def __init__(self, image_name, speed=1):

# Initialization method is called the parent class
super () .__ init __ ()

# Attribute definition object
self.image = pygame.image.load (image_name)
self.rect = self.image.get_rect ()
self.speed = Speed

def update(self):

# Moved in the vertical direction of the screen
self.rect.y + = self.speed


Background class (GameSprite):
"" "game background Wizard" ""

def __init__(self, is_alt=False):

# 1. Call parent class method to achieve creation wizard (Image / RECT / Speed)
Super () .__ the init __ ( "./ ImagesRF Royalty Free / background.png")

2 # determines whether the alternate image, if there is a need to set the initial position of the
IF is_alt:
self.rect.y = -self.rect.height

def update(self):

# 1. Call parent class method to achieve
super (). Update ()

2 # determines whether the off-screen, if the screen is removed, to the top of the screen image setting
IF self.rect.y> = SCREEN_RECT.height:
self.rect.y = -self.rect.height


Enemy class (GameSprite):
"" "enemy Wizard" ""

def __init__(self):

# 1. Call parent class method to create the enemy wizard, specifying the enemy picture
super () .__ init __ ( " ./ images / enemy1.png")

# 2 specified initial random velocity enemy ~. 3. 1
self.speed the random.randint = (. 1,. 3)

3. Specify the location of the initial random enemy #
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 parent class method, holding vertical flight
super (). Update ()

# 2 fly screen to determine whether, and if so, need to remove the enemy from Elf group
IF self.rect.y> = SCREEN_RECT.height:
# Print ( "fly out of the screen needs to be removed from the Wizard group ... ')
# the method may kill all sprites sprite removed from the group, the wizard will be automatically destroyed
self.kill ()

__ __del DEF (Self):
# Print ( "enemy hung S%"% self.rect)
Pass


Hero class (GameSprite):
"" "heroic spirit" ""

def __init__(self):

1. call parent class method # provided & Speed Image
Super () .__ the init __ ( "./ Images / me1.png", 0)

2. Set # Hero initial position
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom - 120

# 3. Create a bullet Elf group
self.bullets = pygame.sprite.Group ()

def update(self):

# Hero moved horizontally
self.rect.x + = self.speed

Hero can not leave the control screen #
IF self.rect.x <0:
self.rect.x = 0
elif self.rect.right> SCREEN_RECT.right:
self.rect.right = SCREEN_RECT.right

Fire DEF (Self):
Print ( "fired bullets ...")

I in for (0,. 1, 2):
# 1. Create bullet sprite
bullet = Bullet ()

2. Set # sprite position
bullet.rect.bottom self.rect.y = - I * 20 is
bullet.rect.centerx = self.rect.centerx

# 3. Elf Elf added to the group
self.bullets.add (bullet)


Bullet class (GameSprite):
"" "bullet Wizard" ""

def __init__(self):

# Call the parent class method, provided bullet image, set the initial speed
super () .__ init __ ( " ./ images / bullet1.png", -2)

def update(self):

# Call the parent class method, let the bullets fly vertically
super (). Update ()

# Determines whether the bullet flying screen
IF self.rect.bottom <0:
self.kill ()

__del __ DEF (Self):
Print ( "bullet destroyed ...")


PlaneGame class (Object):
"" "war planes main game." ""

__init __ DEF (Self):
Print ( "game Initialization")

# 1. Create a game window
self.screen = pygame.display.set_mode (SCREEN_RECT.size)
# 2. Create a game clock
self.clock = pygame.time.Clock ()
# 3. call a private method, elves and sprites group create
self .__ create_sprites ()

# 4. Set a timer event - Creating enemy 1S
pygame.time.set_timer (CREATE_ENEMY_EVENT, 1000)
pygame.time.set_timer (HERO_FIRE_EVENT, 500)

def __create_sprites(self):

# Create a background sprites and elves group
BG1 = Background ()
BG2 = Background (True)

self.back_group = pygame.sprite.Group(bg1, bg2)

# Create the enemy of elves group
self.enemy_group = pygame.sprite.Group ()

# Create a hero of elves and sprites group
self.hero = Hero ()
self.hero_group = pygame.sprite.Group (self.hero)

start_game DEF (Self):
Print ( "start of the game ...")

True the while:
# 1. Set the refresh frame rate
self.clock.tick (Frame_per_sec)
# 2. Event Listeners
Self .__ event_handler ()
# 3. Collision Detection
Self .__ check_collide ()
# 4. Update / Draw Elf group
self .__ update_sprites ( )
# 5. display update
the pygame.display.update ()

def __event_handler(self):

for event in pygame.event.get():

# Determine whether to quit the game
IF event.type == pygame.quit:
PlaneGame .__ game_over ()
elif event.type == CREATE_ENEMY_EVENT:
# Print ( "enemy appearance ...")
# create the enemy sprites
enemy = Enemy ()

# Add the enemy to enemy sprite sprite group
self.enemy_group.add (Enemy)
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 ...")

# Get the keyboard provides use keyboard - key tuple
keys_pressed pygame.key.get_pressed = ()
# tuple determines the corresponding key index. 1
IF keys_pressed [pygame.K_RIGHT]:
self.hero.speed = 2
elif keys_pressed [pygame.K_LEFT]:
self.hero.speed = -2
the else:
self.hero.speed = 0

def __check_collide(self):

# 1 bullet to destroy the enemy
pygame.sprite.groupcollide (self.hero.bullets, self.enemy_group, True, True)

# 2 enemy planes crashing into the Heroes
enemies = pygame.sprite.spritecollide (self.hero, self.enemy_group, True)

# Judge when there is a list of content
if len (enemies)> 0:

# Let the hero sacrifice
self.hero.kill ()

# End of 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(http://www.my516.com)
self.hero.bullets.draw(self.screen)

@staticmethod
DEF __game_over ():
Print ( "Game Over")

pygame.quit()
exit()


if __name__ == '__main__':

# Create game objects
game = PlaneGame ()

# Start the game
game.start_game ()

Guess you like

Origin www.cnblogs.com/hyhy904/p/11488483.html