用python写一个雷霆战机
时间: 2024-05-29 10:03:39 浏览: 103
好的,以下是一个简单的雷霆战机游戏示例,使用Pygame库编写。
```python
import pygame
import random
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 初始化 Pygame
pygame.init()
# 设置屏幕大小
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 设置游戏标题
pygame.display.set_caption("雷霆战机")
# 加载背景图片
background_image = pygame.image.load("background.jpg").convert()
# 定义玩家飞机类
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("player.png").convert()
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.x = 350
self.rect.y = 500
def update(self):
# 获取键盘事件
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rect.x -= 5
elif keys[pygame.K_RIGHT]:
self.rect.x += 5
elif keys[pygame.K_UP]:
self.rect.y -= 5
elif keys[pygame.K_DOWN]:
self.rect.y += 5
# 确保玩家飞机在屏幕范围内
if self.rect.x < 0:
self.rect.x = 0
elif self.rect.x > SCREEN_WIDTH - self.rect.width:
self.rect.x = SCREEN_WIDTH - self.rect.width
elif self.rect.y < 0:
self.rect.y = 0
elif self.rect.y > SCREEN_HEIGHT - self.rect.height:
self.rect.y = SCREEN_HEIGHT - self.rect.height
# 定义敌机类
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("enemy.png").convert()
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(SCREEN_WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
self.speedx = random.randrange(-3, 3)
def update(self):
self.rect.y += self.speedy
self.rect.x += self.speedx
# 敌机超过屏幕底端时重新生成
if self.rect.top > SCREEN_HEIGHT + 10 or self.rect.left < -25 or self.rect.right > SCREEN_WIDTH + 20:
self.rect.x = random.randrange(SCREEN_WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
# 创建所有精灵组
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
# 创建玩家飞机
player = Player()
all_sprites.add(player)
# 创建敌机
for i in range(10):
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
# 设置游戏循环
done = False
clock = pygame.time.Clock()
score = 0
while not done:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
# 创建子弹
bullet = pygame.sprite.Sprite()
bullet.image = pygame.Surface([4, 10])
bullet.image.fill(RED)
bullet.rect = bullet.image.get_rect()
bullet.rect.x = player.rect.x + player.rect.width / 2
bullet.rect.y = player.rect.y - 10
bullets.add(bullet)
all_sprites.add(bullet)
# 更新所有精灵
all_sprites.update()
# 检测子弹是否击中敌机
hits = pygame.sprite.groupcollide(enemies, bullets, True, True)
for hit in hits:
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
score += 1
# 检测敌机是否撞到玩家飞机
hits = pygame.sprite.spritecollide(player, enemies, False)
if hits:
done = True
# 绘制背景和所有精灵
screen.blit(background_image, [0, 0])
all_sprites.draw(screen)
# 绘制得分
font = pygame.font.Font(None, 36)
text = font.render("Score: " + str(score), True, WHITE)
screen.blit(text, [10, 10])
# 更新屏幕
pygame.display.flip()
# 控制游戏速度
clock.tick(60)
# 退出 Pygame
pygame.quit()
```
该游戏使用箭头键控制玩家飞机移动,空格键发射子弹,玩家需要躲避敌机并尽可能地击落敌机,得分越高越好。
阅读全文