飞机大作战(初级)
完整代码如下: 注意,需要在对应同一个文件中放入相应的游戏需要用的图片及音效。否则可能报错
import pygame
import random
GREEN = (84, 255, 159)
BLACK = (0, 0, 0)
WIDTH, HEIGHT = 800, 600
CREATE_ENEMY = pygame.USEREVENT
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("飞机大战")
clock = pygame.time.Clock()
pygame.time.set_timer(CREATE_ENEMY, 2000)
class Hero(pygame.sprite.Sprite):
def __init__(self, speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("./image/plane.png")
self.rect = self.image.get_rect()
self.rect.width *= 0.6
self.rect.height *= 0.6
self.image = pygame.transform.scale(self.image, (self.rect.width, self.rect.height))
self.rect.x = 100
self.rect.y = 100
self.speed = speed
self.ready_to_fire = 0
def update(self, *args):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.rect.y -= self.speed
if keys[pygame.K_DOWN]:
self.rect.y += self.speed
if keys[pygame.K_LEFT]:
self.rect.x -= self.speed
if keys[pygame.K_RIGHT]:
self.rect.x += self.speed
if keys[pygame.K_SPACE]:
if self.ready_to_fire == 0:
self.fire()
self.ready_to_fire += 1
if self.ready_to_fire > 5:
self.ready_to_fire = 0
else:
self.ready_to_fire = 0
def fire(self):
bullet = Bullet(10)
bullet.rect.x = self.rect.right
bullet.rect.centery = self.rect.centery
bullet_group.add(bullet)
class Bullet(pygame.sprite.Sprite):
def __init__(self, speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("./image/bullet.png")
self.rect = self.image.get_rect()
self.speed = speed
def update(self, *args):
self.rect.x += self.speed
if self.rect.x > WIDTH:
self.kill()
class Enemy(pygame.sprite.Sprite):
def __init__(self, speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("./image/enemy1.png")
self.rect = self.image.get_rect()
self.rect.x = WIDTH
self.rect.y = random.randint(0, HEIGHT)
self.speed = speed
def update(self, *args):
self.rect.x -= self.speed
if self.rect.x < 0:
self.kill()
class Explode(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.images = [pygame.image.load("./image/explode" + str(i) + ".png") for i in range(1, 4) ]
self.image_index = 0
self.image = self.images[self.image_index]
self.rect = self.image.get_rect()
self.ready_to_change = 0
def update(self, *args):
if self.image_index < 2:
self.ready_to_change += 1
if self.ready_to_change % 4 == 0:
self.image_index += 1
self.image = self.images[self.image_index]
else:
self.kill()
class BackGround(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("./image/background.jpg")
self.rect = self.image.get_rect()
self.ready_to_move = 0
def update(self, *args):
if self.ready_to_move == 0:
self.rect.x -= 1
if self.rect.right <= 0:
self.rect.x = self.rect.width
if self.ready_to_move > 1:
self.ready_to_move = 0
else:
self.ready_to_move += 1
hero = Hero(4)
bg = BackGround()
bg2 = BackGround()
bg2.rect.x = bg2.rect.width
bg_group = pygame.sprite.Group()
hero_group = pygame.sprite.Group()
enemy_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
explode_group = pygame.sprite.Group()
hero_group.add(hero)
bg_group.add(bg, bg2)
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == CREATE_ENEMY:
enemy_group.add(Enemy(random.randint(1, 4)))
collision = pygame.sprite.groupcollide(enemy_group, bullet_group, True, True)
for enemy in collision.keys():
explode = Explode()
explode.rect = enemy.rect
explode_group.add(explode)
screen.fill(BLACK)
for group in [bg_group, hero_group, enemy_group, bullet_group, explode_group]:
group.update()
group.draw(screen)
pygame.display.update()
本代码的不足: 飞机可以飞出屏幕; 飞机不会阵亡; 子弹为连发,但非为多排发;
效果如下图:
(注:代码为搬运,如有不妥,删,仅供学习参考使用)
|