程序界面:
介绍:使用pygame包开发的一个简易飞机大战游戏,主要用来熟悉python的面向对象编程。
Python代码:
飞机大战.py
import random
import pygame
import time
class BasePlane(object):
def __init__(self, screen_temp, x, y, image_name, hp):
self.x = x
self.y = y
self.hp = hp
self.screen = screen_temp
self.image = pygame.image.load(image_name)
self.bullet_list = [] # 存储发射出去的子弹对象引用
def display(self):
self.screen.blit(self.image, (self.x, self.y))
for bullet in self.bullet_list:
bullet.dispaly()
bullet.move()
if bullet.judge(): # 判断子弹是否越界
self.bullet_list.remove(bullet)
class HeroPlane(BasePlane):
def __init__(self, screen_temp):
BasePlane.__init__(self, screen_temp, 210, 600, "./feiji/hero1.png", 1000)
# 爆炸效果用的如下属性
self.hit = False # 表示是否要爆炸
self.bomb_list = [] # 用来存储爆炸时需要的图片
self.__crate_images() # 调用这个方法向bomb_list中添加图片
self.image_num = 0 # 用来记录while True的次数,当次数达到一定值时才显示一张爆炸的图,然后清空,,当这个次数再次达到时,再显示下一个爆炸效果的图片
self.image_index = 0 # 用来记录当前要显示的爆炸效果的图片的序号
def display(self):
# 如果被击中,就显示爆炸效果,否则显示普通的飞机效果
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)
print("你挂了...")
exit() # 调用exit让游戏退出
# self.image_index = 0
else:
self.screen.blit(self.image, (self.x, self.y))
for