Home > other >  Pygame _01 little game _ plane war
Pygame _01 little game _ plane war

Time:11-22

the plane war

1. Prepare module

the first step: open the CMD



step 2: input instruction PIP install pygame



2. Start writing

the first step: import module and initialize
 import pygame, sys, random, and the time, tkinter 
The from tkinter import *
The from pygame. Locals import *
Pygame. The init ()

step 2: create a width of 480/650 respectively window
 canvas=pygame. Display. Set_mode ((480, 650)) 
Canvas. The fill ((255, 255, 255))

# set the window title
Pygame. Display. Set_caption (" aircraft wars ")
Bg=pygame. Image. The load (" images/bg4. PNG ")

the third step: loading pictures
A
 # enemyEnemy1=pygame. Image. The load (" images/enemy1. PNG ") 
Enemy # 2
Enemy2=pygame. Image. The load (" images/enemy2. PNG ")
# three enemy
Enemy3=pygame. Image. The load (" images/enemy3. PNG ")
# bullets
B=pygame. Image. The load (" images/bullet1. PNG ")

# heroH=pygame. Image. The load (" images/hero. PNG ")
# start the game picture
The startgame=pygame. Image. The load (" images/the startgame. PNG ")
# logo picture
Logo=pygame. Image. The load (" images/logo. PNG ")
# suspended picture
Pause=pygame. Image. The load (" images/game_pause_nor. PNG ")

the fourth step: add time interval method
 def isActionTime (lastTime, interval) : 
If lastTime==0:
Return True
CurrentTime=time. Time ()
Return currentTime - lastTime & gt;=interval

step 5: define the background class
 class Sky () : 
Def __init__ (self) :
The self. The width=480
The self. The height=852
The self. The img=bg
The self. The x1=0
The self. The y1=0
The self. The x2=0
Self. Y2=- self. Height
# to create the paint method
Def paint (self) :
Canvas. Blit (self. Img (self. X1, self. Y1))
Canvas. Blit (self. Img (self) x2, self. Y2))
# to create step method
Def step (self) :
Self. Y1=self. Y1 + 1
Self. Y2=self. Y2 + 1
If self. Y1 & gt; Self. Height:
Self. Y1=- self. Height
If self. Y2 & gt; Self. Height:
Self. Y2=- self. Height

step 6: define the superclass FlyingObject
 class FlyingObject (object) : 
Def __init__ (self, x, y, width, height, life, img) :
Self. X=x
The self. The y=y
The self. The width=width
The self. The height=height
Self. Life=life
The self. The img=img
# enemy aircraft moving interval
The self. The lastTime=0
The self. The interval=0.01
# add or remove properties
Self. CanDelete=False
# define the paint method
Def paint (self) :
Canvas. Blit (self. Img, (self. X, the self. The y))
# define step method
Def step (self) :
# to judge whether the time interval to move
If not isActionTime (self lastTime, self interval) :
Return
The self. The lastTime=time. Time ()
# control movement speed
Self. Y=self. Y + 2
# define hit method to judge whether a collision between two objects
Def hit (self, component) :
C=component
Return c.x & gt; The self. The X-ray c.w idth and c.x & lt; Self. X + self. Width and \
C.y & gt; The self. The y - c. eight and c.y & lt; The self. The y + self. Height
# define bang method treatment objects after the collision between
Def bang (self, bangsign) :
# the enemy and the hero machine processing
after the collisionIf bangsign:
If hasattr (self, 'score') :
GameVar. Score +=self. Score
If bangsign==2:
The self. The life -=1
# set to delete attribute to True
Self. CanDelete=True
# enemy planes and bullets collision after processing
The else:
The self. The life -=1
If self. Life==0:
# set to delete attribute to True
Self. CanDelete=True
# if componentEnter & lt;=7:
# CameVar. Es1=GameVar. Es1=[' die ']
If hasattr (self, 'score') :
GameVar. Score +=self. Score
# define outOfBounds method determine whether object cross-border
Def outOfBounds (self) :
Return the self. The y & gt; 650

step 7: refactoring Enemy class
 class Enemy (FlyingObject) : 
Def __init__ (self, x, y, width, height, type, life, score, img) :
FlyingObject. __init__ (self, x, y, width, height, life, img)
Self. Type=type
The self. The score=score

step 8: refactoring Hero class
 class Hero (FlyingObject) : 
Def __init__ (self, x, y, width, height, life, img) :
FlyingObject. __init__ (self, x, y, width, height, life, img)
Self. X=480/2 - self. Width/2
The self. The y=650 - self. The height - 30
Self. ShootLastTime=0
Self. ShootInterval=0.3
Def shoot (self) :
If not isActionTime (self shootLastTime, self shootInterval) :
Return
Self. ShootLastTime=time. Time ()
GameVar. Bullets. Append (Bullet (self. X + self. Width/2-5, the self. The y - 10, 10, 10, 1, b))

step 9: refactoring Bullet class
 class Bullet (FlyingObject) : 
Def __init__ (self, x, y, width, height, life, img) :
FlyingObject. __init__ (self, x, y, width, height, life, img)
Def step (self) :
Self. Y=self. Y - 2
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related