pygame做飞机大战游戏
时间: 2023-11-30 21:32:54 浏览: 90
pygame实现飞机大战
好的,下面是一个简单的飞机大战游戏的pygame代码示例,你可以参考一下:
```python
import pygame
import random
pygame.init()
# 设置游戏窗口大小
screen = pygame.display.set_mode((480, 700))
# 设置游戏标题
pygame.display.set_caption("飞机大战")
# 定义颜色
BLACK = pygame.Color(0, 0, 0)
WHITE = pygame.Color(255, 255, 255)
GREEN = pygame.Color(0, 255, 0)
RED = pygame.Color(255, 0, 0)
# 加载背景图片
bg = pygame.image.load("background.png")
# 加载飞机图片
player_img = pygame.image.load("player.png")
# 加载敌机图片
enemy_img = pygame.image.load("enemy.png")
# 加载子弹图片
bullet_img = pygame.image.load("bullet.png")
# 设置字体
font = pygame.font.SysFont("arial", 36)
# 定义玩家飞机类
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = player_img
self.rect = self.image.get_rect()
self.rect.centerx = 240
self.rect.bottom = 680
self.speed = 5
self.bullets = pygame.sprite.Group()
def update(self):
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_LEFT]:
self.rect.x -= self.speed
if key_pressed[pygame.K_RIGHT]:
self.rect.x += self.speed
if key_pressed[pygame.K_SPACE]:
bullet = Bullet(self.rect.centerx, self.rect.top)
self.bullets.add(bullet)
def draw(self, surface):
surface.blit(self.image, self.rect)
self.bullets.draw(surface)
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
self.bullets.add(bullet)
# 定义子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_img
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speed = -10
def update(self):
self.rect.y += self.speed
if self.rect.bottom < 0:
self.kill()
# 定义敌机类
class Enemy(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = enemy_img
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, 480 - self.rect.width)
self.rect.y = random.randint(-100, -50)
self.speed = random.randint(3, 8)
def update(self):
self.rect.y += self.speed
if self.rect.top > 700:
self.kill()
# 创建精灵组
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
# 设置游戏帧率
clock = pygame.time.Clock()
FPS = 60
# 游戏循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 更新玩家飞机
player.update()
# 创建敌机
if random.randint(0, 60) == 0:
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
# 更新所有精灵
all_sprites.update()
# 碰撞检测
hits = pygame.sprite.groupcollide(enemies, player.bullets, True, True)
for hit in hits:
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
hits = pygame.sprite.spritecollide(player, enemies, False)
if hits:
text = font.render("Game Over", True, RED)
screen.blit(text, (240 - text.get_rect().width // 2, 300))
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()
exit()
# 绘制背景
screen.blit(bg, (0, 0))
# 绘制所有精灵
all_sprites.draw(screen)
# 更新屏幕
pygame.display.update()
# 控制游戏帧率
clock.tick(FPS)
```
以上是一个非常简单的飞机大战游戏的pygame代码示例,你可以根据自己的需求进行修改和完善。
阅读全文