1. Pycharm game engine setup

2. Requirements analysis

3. The main logic

1.1 Start the Game

(1) Load the game window with the help of the game engine document

Website: www.pygame.org/docs/

Color reference document

Game window running results

(2) Event processing, close the game window, the tank movement direction button corresponding

Event handling official documentation

Keyboard operation, press up and down left and right keys, running results of the program (tank up and down left and right turn around movement)

1.2 Ending the Game

1.3 Text prompt for the remaining number of enemy tanks

Text formatting, color, etc. is done on the canvas, just stick the canvas on the game window

Game loading window (run result)

1.4 Loading our tanks

Orientation, image properties

The four directions of the tank, each representing 4 different pictures, put these pictures in the dictionary according to the keywords

Add image methods and image parameters

Operation results (renderings)

1.5 Our tanks are turning around and moving

If event.key == PyGame. K_LEFT: Print (" Tank turn left, Tank_p1.direction = 'L' # Move maingame.tank_p1.move () elif event.key == pygame.K_RIGHT: Print (" Tank head to the right, Tank_p1.direction = 'R' # Move the tank maingame.tank_p1.move () elif event.key == pygame.K_UP: Print (" Tank head up, Tank_p1.direction = 'U' # Move maingame.tank_p1.move () elif event.key == Tank_p1.direction = 'D' # print() maingame.tank_p1.move ()Copy the code
Def move(self): def move(self): if self. Direction == 'L': Self. rect.left -= self.speed # if the tank moves to the right, the left value increases elif self.direction == 'R': Self. rect.left += self.speed # tank moves up, top value decreases elif self.direction == 'U': Self.rect. top -= self.speed # tank moves down, top value increases elif self.direction == 'D': self.rect.top += self.speedCopy the code

Move the result of the run to the right

1.6 Boundary treatment in our tanks just in general

In the game engine, when the default tank orientation is left, the upper left dot represents the tank

Tanks are not allowed out of the game window

Def move(self): def move(self): def move(self): def move(self): def move(self): def move(self): def move(self): def move(self): def move(self): def move(self): def move(self): def move(self): Self. rect.left -= self.speed # if the tank moves to the right, the left value increases elif self.direction == 'R': If self.rect.left +self.rect.height < maingame.screen_width: Elif self.direction == 'U': if self.rect.top > 0: Self. rect.top -= self.speed # tank moves down, top value increases elif self.direction == 'D': if self.rect.top + self.rect.height < MainGame.SCREEN_HEIGHT: self.rect.top += self.speedCopy the code

1.7 Tank movement switch

Press the button, the tank keeps moving, release the button, stop moving, and fire bullets as it moves

If event.key == PyGame. K_LEFT: Print (" Tank turn left, Tank_p1. direction = 'L' maingame.tank_p1. stop = False elif event.key == PyGame.k_right: Print (" Tank head to the right, Tank_p1. direction = 'R' maingame.tank_p1. stop = False elif event.key == PyGame.k_up: Print (" Tank head up, Tank_p1. direction = 'U' maingame.tank_p1. stop = False elif event.key == PyGame.k_down: Print (" Tank head down, Tank_p1. direction = 'D' maingame.tank_p1. stop = False elif event.key == PyGame.k_space: Print (" Tank firing bullets ") if event.type == pyGame.keyUp: # If the arrow key is released, Key == PyGame. K_LEFT or event.key == PyGame. K_RIGHT or event.key == PyGame. K_UP or event.key == Pygame. K_DOWN: # Change the tank movement status maingame.tank_p1. stop = TrueCopy the code

Handles tank movement speed

Time. Sleep (0.02)Copy the code

2. Load enemy tanks

2.1 Randomly generate enemy tanks and load them in the window

The main code

Class EnemyTank(Tank): # def __init__(self,left,top,speed): self. Images = {'U': Pygame. Image. The load (' img/enemy tanks _U. PNG '), 'D' : pygame. Image. The load (' img/enemy tanks D.p ng '), 'L' : Pygame.image.load ('img/ enemy tank _L.png'), 'R': Load ('img/ enemy tank _r.png ')} self. Direction = self. RandDirection () self. Image = self Rect-> self.rect = self.image.get_rect() # specify the initial position of the tank from the x and y axes self.rect.left = left self.rect.top = top # Self. speed = 5 self.stop = True def randDirection(self): num = random. Randint (1,4) if num ==1: return 'U' elif num ==2: return 'D' elif num ==3: return 'L' elif num ==4: return 'R'Copy the code

The results

2.2 Random movement of enemy tanks

Part of the code

def randMove(self):
    if self.step <=0:
        self.direction = self.randDirection()
        self.step = 50
    else:
        self.move()
        self.step -= 1
Copy the code

rendering

3. Bullet packaging

3.1 Position of bullet

self.rect = self.image.get_rect()
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.top
elif self.direction =='D':
    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 == '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
    self.rect.top = Tank.rect.top + Tank.rect.width / 2 - self.rect.width/2
Copy the code

3.2 Movement of bullets

Def bulletMove(self): if self.direction == 'U': if self.rect.top > 0: self.rect.top -=self.speed else: pass elif self.direction == 'D': if self.rect.top < MainGame.SCREEN_HEIGHT - self.rect.height: self.rect.top += self.speed else: pass elif self.direction == 'L': if self.rect.left > 0: self.rect.left -= self.speed elif self.direction == 'R': if self.rect.left < MainGame.SCREEN_WIDTH - self.rect.width: self.rect.left += self.speed else: passCopy the code

rendering

3.2 Bullet extinction, control the number of bullets

The key code

Elif event.key == pygame.K_SPACE: print(" Bullet_list ") if len(maingame.bullet_list) < 3: Maingame.bullet_list.append (m) else: maingame.bullet_list.append (m) else: Print (" Bullet count on current screen: %d"%len(maingame.bullet_list))Copy the code

4. Enemy tanks fired randomly