'' '
What's New:
Optimization: 1. If the bullet hit the wall, let the bullets disappear
2 can transmit up to three bullets, not always launch
'' '
# Import module pygame
Import pygame, Time, Random
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 500
bg_color pygame.Color = (0,0,0)
text_color pygame.Color = (255,0,0)
class MainGame ():
window = none
my_tank = none
list # storage enemy tanks
enemyTankList = []
# define the number of enemy tanks
enemyTankCount = 5
# store our bullet list
myBulletList = []
DEF __init __ (Self):
Pass
# start the game
def startGame (self ):
# load main window
# window initialization
pygame.display.init ()
# set the size of the window and display
MainGame.window = pygame.display.set_mode ([SCREEN_WIDTH, SCREEN_HEIGHT ])
Our tanks # initialization
MainGame.my_tank = Tank (350,250)
# initialization enemy tanks and enemy tanks add to the list
self.createEnemyTank ()
# Set window title
pygame.display.set_caption ( 'tankedaizhan 1.03')
True the while:
# use of tanks moving a little slow
the time.sleep (0.02)
# set the fill color to the window
MainGame.window.fill (bG_COLOR)
get event #
self.getEvent ()
# draw text
MainGame.window.blit (self. getTextSuface ( 'remaining number of enemy tanks D%'% len (MainGame.enemyTankList)), (10,10))
# call display method tank
MainGame.my_tank.displayTank ()
# loop through enemy tanks list, display the enemy tank
self.blitEnemyTank ()
# we cycle through display bullet tank
self.blitMyBullet ()
# call moving method
# switch if the tank is open, it may be moved
IF Not MainGame.my_tank.stop:
MainGame.my_tank.move ()
the pygame .display.update ()
# initialization enemy tanks and enemy tanks add to the list
DEF createEnemyTank (Self):
Top = 100
# cycles generated enemy tanks
for I in Range (MainGame.enemyTankCount):
left = Random. the randint (0,600)
Speed = the random.randint (l, 4)
Enemy = EnemyTank (left, Top, Speed)
MainGame.enemyTankList.append (Enemy)
# Loop through the list of enemy tanks, enemy tanks show
DEF blitEnemyTank (Self):
for enemyTank in MainGame.enemyTankList:
enemyTank.displayTank ()
enemyTank.randMove ()
# Loop through our store bullet list
DEF blitMyBullet (Self):
for in MainGame.myBulletList myBullet:
# determine whether the current state of the bullet is alive, and if so, display and mobile,
IF myBullet.live:
myBullet.displayBullet ()
# call the bullet moving method
myBullet.move ()
# otherwise in the list delete
the else:
MainGame.myBulletList.remove (myBullet)
# End of the game
DEF ENDGAME (Self):
Print ( 'Thank you, welcome again use')
Exit ()
# upper left corner of the text drawn
DEF getTextSuface (Self, text):
# initialize the font module
pygame.font.init ()
# View All Font name
# Print (pygame.font.get_fonts ())
# Gets font Font object
font = pygame.font.SysFont ( 'Kaiti', 18)
# draw a text message
textSurface = font.render (text, True, tEXT_COLOR)
textSurface return
# get events
DEF getEvent (Self):
# get all events
EventList pygame.event.get = ()
# iterate events
for event in EventList:
# judge pressed key is turned off or the keyboard press
# If the press is to exit, close window
IF Event.type == pygame.quit:
self.endGame ()
# if the keyboard is pressed
IF Event.type == pygame.KEYDOWN:
# determination is pressed on the lower left and right
IF event.key == pygame.K_LEFT:
# switching direction
MainGame.my_tank.direction = 'L'
# modify the switching state of the tank
MainGame.my_tank.stop = False
# MainGame.my_tank.move ()
Print ( ' pressing the left, moving to the left tank ')
elif event.key == pygame.K_RIGHT:
# switching direction
MainGame.my_tank.direction =' R '
# Modify the switching state of the tank
MainGame.my_tank.stop = False
# MainGame.my_tank.move ()
Print ( 'right button, move right tank')
elif event.key == pygame.K_UP:
# switching direction
MainGame.my_tank = .Direction 'the U-'
# tank modified switching state
MainGame.my_tank.stop = False
# MainGame.my_tank.move ()
Print ( 'the button is pressed, the tank moves upwards')
elif event.key == pygame.K_DOWN:
# switching direction
MainGame.my_tank.direction = 'D'
# modify the switching state of the tank
= False MainGame.my_tank.stop
# MainGame.my_tank.move ()
Print ( 'button is pressed, the tank is moved downward')
elif event.key == pygame.K_SPACE:
Print ( 'bullets fired')
# If the current I square bullet list size of 3 or less when it may create
IF len (MainGame.myBulletList) <3:
# Create our tanks fired bullets
myBullet = bullet (MainGame.my_tank)
MainGame.myBulletList.append (myBullet)
# Loosening direction keys, the tank stops moving, modifying the switching state of the tank
IF Event.type == pygame.KEYUP:
# key determination is released, down, left, and right tank when it stops moving
if event.key == or event.key == pygame.K_DOWN or pygame.K_UP event.key == == pygame.K_RIGHT pygame.K_LEFT or event.key:
MainGame.my_tank.stop = True
Tank class ():
# distance left from the upper left to add Top
DEF the __init __ (Self, left, Top):
# Load saved images
self.images = {
'the U-': pygame.image.load ( 'IMG / p1tankU.gif' ),
'D': pygame.image.load ( 'IMG / p1tankD.gif'),
'L': pygame.image.load ( 'IMG / p1tankL.gif'),
'R & lt': pygame.image.load ( 'IMG / p1tankR.gif'),
}
# direction
self.direction = 'L'
# acquired images according to the direction of the current picture Surface
self.image = self.images [self.direction]
# image acquisition area according
self.rect = self .image.get_rect ()
left and top # set area
self.rect.left=left
self.rect.top=top
# Speed determines the moving speed
self.speed. 5 =
# Tank mobile switching
self.stop = True
#移动
def move(self):
#判断坦克的方向进行移动
if self.direction == 'L':
if self.rect.left>0:
self.rect.left -= self.speed
elif self.direction == 'U':
if self.rect.top>0:
self.rect.top -= self.speed
elif self.direction == 'D':
if self.rect.top+self.rect.height<SCREEN_HEIGHT:
self.rect.top += self.speed
elif self.direction == 'R':
if self.rect.left+self.rect.height<SCREEN_WIDTH:
self.rect.left += self.speed
# Shooting
DEF SHOT (Self):
Pass
# show tank method
DEF displayTank (Self):
# get displayed objects
self.image = self.images [self.direction]
# call the blit method shows
MainGame.window.blit (self. Image, self.rect)
# our tanks
class MyTank (tank):
DEF __init __ (Self):
Pass
# Enemy tanks
class EnemyTank (Tank):
DEF the __init __ (Self, left, Top, Speed):
# Load picture collection
self.images = {
'the U-': pygame.image.load ( 'IMG / enemy1U.gif'),
'D': pygame.image.load ( 'IMG / enemy1D.gif'),
'L': pygame.image.load ( 'IMG / enemy1L.gif'),
'R & lt': pygame.image.load ( 'IMG /enemy1R.gif ')
}
# directions, randomly generated enemy tanks direction
self.direction self.randDirection = ()
# acquired image according to a direction
self.image = self.images [self.direction]
# region
self.rect = self .image.get_rect ()
# on the left and top assignment
self.rect.left = left
Self.rect.top=top
# Speed
self.speed Speed =
# mobile switch key
self.flag = True
# salary increase a variable step number STEP
self.step = 60
# Direction of enemy tanks randomly generated
DEF randDirection (Self):
NUM = the random.randint (l, 4)
IF NUM ==. 1:
return 'the U-'
elif NUM == 2:
return 'D'
elif NUM ==. 3:
return "L"
elif NUM ==. 4:
return 'R & lt'
# Random movement of enemy tanks method
DEF randMove (Self):
IF self.step <= 0:
# Review direction
self.direction self.randDirection = ()
# concession number reset
self.step = 60
the else:
self.move ()
# concession number decremented
self.step-. 1 =
# bullet class
class bullet ():
DEF the __init __ (Self, tank):
# load picture
self.image = pygame.image.load ( 'IMG / enemymissile.gif')
# tank determines the direction of the bullet in the direction
self.direction = tank.direction
# acquisition region
self.rect self.image.get_rect = ()
# top and left about the direction of the bullet
if self.direction == 'U':
self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2
self.rect.top = tank.rect.top - self.rect.height
elif self.direction == 'D':
self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2
self.rect.top = tank.rect.top + tank.rect.height
elif self.direction == 'L':
self.rect.left = tank.rect.left - self.rect.width / 2 - self.rect.width / 2
self.rect.top = tank.rect.top + tank.rect.width / 2 - self.rect.width / 2
elif self.direction == 'R':
self.rect.left = tank.rect.left + tank.rect.width
= + tank.rect.width tank.rect.top self.rect.top / 2 - self.rect.width / 2
# bullet velocity
self.speed = 6
state # bullet, whether hit the wall, if the wall encounter , modify the state
self.live = True
# mobile
DEF move (Self):
IF self.direction == 'the U-':
IF self.rect.top> 0:
self.rect.top- = self.speed
the else:
# Review state bullet
self.live = False
elif self.direction == 'R & lt':
IF self.rect.left + self.rect.width <SCREEN_WIDTH:
self.rect.left + = self.speed
the else:
# bullets state modified
self .live = False
== self.direction elif 'D':
IF self.rect.top + self.rect.height <SCREEN_HEIGHT:
self.rect.top + = self.speed
the else:
# bullet modified state
self.live = False
elif self.direction == 'L':
IF self.rect.left> 0:
self.rect.left- = self.speed
the else:
# bullet modified state
self.live = False
method # show bullet
DEF displayBullet (Self):
# the picture surface into a window
MainGame.window.blit (self.image, self.rect)
class wall ():
DEF __init __ (Self):
Pass
# display wall method
DEF displayWall (Self):
Pass
class Explode ():
DEF __init __ (Self):
Pass
# show explosions method
DEF displayExplode (Self):
Pass
class Music ():
DEF __init __ (Self):
Pass
# playing music
def play ( Self):
Pass
IF __name__ __ == '__ main__':
. MainGame () startGame ()
# MainGame () getTextSuface ().