plane_battle_code

# -*- coding:utf-8 -*- 
import pygame
from pygame.locals import *
import time

'''
Description
1. Press the b key to let the player's plane explode
2. The principle of the explosion effect is: change the picture
'''


class Hero(object):
def __init__(self, screen_temp):
self.x = 210
self.y = 700
self.image = pygame.image.load("./feiji/hero1.png")
self.screen = screen_temp
self .bullet_list = [] # Used to store the reference of the bullet object

# The following attributes are used for the explosion effect
self.hit = False # Indicates whether to explode
self.bomb_list = [] # Used to store the pictures needed for the explosion
self.__crate_images() # Call this method to add pictures to bomb_list
self.image_num = 0 # Used to record the number of times while True, when the number of times reaches a certain value, an explosion image will be displayed, and then emptied. When the number of times is reached again, the next image of the explosion effect will be displayed
self.image_index = 0 # The serial number of the image used to record the current explosion effect

def __crate_images(self):
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n1.png"))
self.bomb_list.append (pygame.image.load("./feiji/hero_blowup_n2.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n3.png"))
self.bomb_list.append(pygame.image .load("./feiji/hero_blowup_n4.png"))

def display(self):
"""Show the player's plane"""
# If it is hit, it will show the explosion effect, otherwise it will show the normal plane effect
if self. hit == True:
self.screen.blit(self.bomb_list[self.image_index],(self.x, self.y))
self.image_num += 1
if self.image_num == 7:
self.image_num = 0
self.image_index += 1
if self.image_index > 3:
time.sleep(1)
exit() # call exit to exit the game
# self. image_index = 0
else:
self.screen.blit(self.image, (self.x, self.y))

# Whether the player's plane is hit or not, display the bullets fired
for bullet in self.bullet_list:
bullet. display()
bullet.move()

def move_left(self):
self.x -= 8

def move_right(self):
self.x += 8

def fire(self):
"""Fill a bullet by creating a bullet object"""
print("-----1----")
bullet = Bullet(self.screen, self.x, self.y) # Create a bullet object
self.bullet_list.append(bullet)

def bomb(self):
self.hit = True


class Bullet(object):
def __init__(self, screen_temp, x_temp, y_temp):
self.x = x_temp + 40
self.y = y_temp - 20
self.image = pygame.image.load("./feiji/bullet.png")
self.screen = screen_temp

def display(self):
self.screen.blit(self.image, (self.x, self. y))

def move(self):
self.y -= 4


class EnemyPlane(object):
def __init__(self, screen_temp):
self.x = 0
self.y = 0
self.image = pygame.image.load("./feiji/enemy0.png")
self.screen = screen_temp
# self.bullet_list = []#A reference to store the bullet object
self.direction = "right" # Used to set the default movement direction of this plane

def display(self):
"""Display the enemy plane"""
self.screen.blit(self.image, (self.x, self. y))

def move(self):

if self.direction == "right":
self.x += 2
elif self.direction == "left":
self.x -= 2

if self.x > 480 - 50:
self.direction = "left"
elif self.x < 0:
self.direction = "right"


def key_control(hero_temp):
# Get events, such as keys, etc.
for event in pygame.event.get():

# Determine whether the exit button is clicked
if event.type == QUIT:
print("exit")
exit()
# Determine whether it is pressed key
elif event.type == KEYDOWN:
# Check if the key is a or left
if event.key == K_a or event.key == K_LEFT:
print('left')
hero_temp.move_left()

# Check if the key is d or right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right()

# Check if the key is the space bar
elif event.key == K_SPACE:
print('space')
hero_temp.fire()
elif event.key == K_b:
print('b')
hero_temp.bomb()


def main():
screen = pygame.display.set_mode((480, 852), 0, 32)
background = pygame.image.load("./feiji/background.png")

# 创建玩家飞机
hero = Hero(screen)

# 创建敌机
enemy = EnemyPlane(screen)

while True:
screen.blit(background, (0, 0))
hero.display()
enemy.display()
enemy.move()
pygame.display.update()
key_control(hero)


if __name__ == "__main__":
main()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325185915&siteId=291194637