该游戏代码根据某教程编写,未实现音效和游戏终止功能。很多地方写得不好,待优化。安装pygame,即可运行。
界面效果:
简陋的代码:
import pygame
import time
from pygame.locals import *
import random
class Base(object):
def __init__(self, screen_temp, x, y, image_name):
self.x = x
self.y = y
self.screen = screen_temp
self.image = pygame.image.load(image_name)
class BasePlane(Base):
def __init__(self, screen_temp, x, y, image_name):
Base.__init__(self, screen_temp, x, y, image_name)
def display(self, plane):
self.screen.blit(self.image, (self.x, self.y))
for b in self.bullet_list:
b.move()
b.display()
if b.judge():
self.bullet_list.remove(b)
if isinstance(plane, HeroPlane) and b.y >= 540 and b.y <= 600 and b.x >= plane.x + 20 and b.x <= plane.x + 100:
plane.crash('./feiji/hero_blowup_n3.png')
self.bullet_list.remove(b)
elif isinstance(plane, EnemyPlane) and b.y >= 0 and b.y <= 39 and b.x >= plane.x and b.x <= plane.x + 51:
plane.crash('./feiji/enemy0_down2.png')
self.bullet_list.remove(b)
def crash(self, image_name):
self.image = pygame.image.load(image_name)
class BaseBullet(Base):
def __init__(self, screen_temp, x, y, image_name):
Base.__init__(self, screen_temp, x, y, image_name)
def display(self):
self.screen.blit(self.image, (self.x, self.y))
class EnemyPlane(BasePlane):
def __init__(self, screen_temp, x, y, image_name):
BasePlane.__init__(self, screen_temp, x, y, image_name)
self.direction = 'right'
self.bullet_list = []
def move(self):
if self.direction == 'right':
self.x += 5
elif self.direction == 'left':
self.x -= 5
if self.x > 480 - 50:
self.direction = 'left'
elif self.x < 0:
self.direction = 'right'
def fire(self):
r = random.randint(1, 100)
if r == 30 or r == 60 or r == 90:
self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y, './feiji/bullet1.png'))
class HeroPlane(BasePlane):
def __init__(self, screen_temp, x, y, image_name):
BasePlane.__init__(self, screen_temp, x, y, image_name)
self.bullet_list = []
def move_left(self):
self.x-=5
def move_right(self):
self.x+=5
def fire(self):
self.bullet_list.append(Bullet(self.screen, self.x, self.y, './feiji/bullet.png'))
class EnemyBullet(BaseBullet):
def __init__(self, screen_temp, x, y, image_name):
BaseBullet.__init__(self, screen_temp, x + 25, y + 40, image_name)
def move(self):
self.y += 20
def judge(self):
if self.y > 750:
return True
else:
return False
class Bullet(BaseBullet):
def __init__(self, screen_temp, x, y, image_name):
BaseBullet.__init__(self, screen_temp, x + 40, y - 20, image_name)
def move(self):
self.y -= 20
def judge(self):
if self.y < 0:
return True
else:
return False
def key_control(hero):
for event in pygame.event.get():
if event.type == QUIT:
print("exit")
exit()
elif event.type == KEYDOWN:
if event.key == K_a or event.key == K_LEFT:
print('left')
hero.move_left()
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero.move_right()
elif event.key == K_SPACE:
print('space')
hero.fire()
def main():
screen = pygame.display.set_mode((480, 750), 0, 32)
pygame.display.set_caption("飞机大战")
background = pygame.image.load('./feiji/background.png')
hero = HeroPlane(screen, 200, 600, './feiji/hero1.png')
enemy = EnemyPlane(screen, 0, 0, './feiji/enemy0.png')
while True:
screen.blit(background, (0, 0))
hero.display(enemy)
enemy.display(hero)
enemy.move()
enemy.fire()
pygame.display.update()
key_control(hero)
time.sleep(0.02)
if __name__ == '__main__':
main()