python 写一个 雷电游戏的代码
时间: 2023-12-20 21:03:46 浏览: 69
雷电游戏代码
好的,下面是一个简单的 Python 雷电游戏代码,希望能帮到你:
```python
import pygame
import random
# 定义常量
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800
BULLET_WIDTH = 8
BULLET_HEIGHT = 20
ENEMY_WIDTH = 50
ENEMY_HEIGHT = 50
# 初始化 Pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("雷电游戏")
# 加载背景图片
bg_img = pygame.image.load("bg.jpg").convert_alpha()
# 加载玩家飞机图片
player_img = pygame.image.load("player.png").convert_alpha()
player_rect = player_img.get_rect()
player_rect.centerx = SCREEN_WIDTH // 2
player_rect.bottom = SCREEN_HEIGHT - 20
# 加载子弹图片
bullet_img = pygame.image.load("bullet.png").convert_alpha()
# 加载敌机图片
enemy_img = pygame.image.load("enemy.png").convert_alpha()
# 加载游戏音效
bullet_sound = pygame.mixer.Sound("bullet.wav")
enemy_down_sound = pygame.mixer.Sound("enemy_down.wav")
# 定义子弹类
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.top += 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.centerx = random.randint(ENEMY_WIDTH // 2, SCREEN_WIDTH - ENEMY_WIDTH // 2)
self.rect.bottom = 0
self.speed = random.randint(3, 5)
def update(self):
self.rect.top += self.speed
if self.rect.top > SCREEN_HEIGHT:
self.kill()
# 创建玩家飞机精灵组
player_group = pygame.sprite.Group()
player_group.add(pygame.sprite.Sprite())
# 创建子弹精灵组
bullet_group = pygame.sprite.Group()
# 创建敌机精灵组
enemy_group = pygame.sprite.Group()
# 设置游戏时钟
clock = pygame.time.Clock()
# 定义游戏结束函数
def game_over():
font = pygame.font.SysFont(None, 48)
text = font.render("Game Over", True, (255, 0, 0))
text_rect = text.get_rect()
text_rect.centerx = SCREEN_WIDTH // 2
text_rect.centery = SCREEN_HEIGHT // 2
screen.blit(text, text_rect)
pygame.display.update()
pygame.time.wait(2000)
pygame.quit()
exit()
# 游戏循环
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
exit()
elif event.key == pygame.K_SPACE:
bullet_sound.play()
bullet = Bullet(player_rect.centerx, player_rect.top)
bullet_group.add(bullet)
# 处理键盘输入
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_rect.left -= 5
if player_rect.left < 0:
player_rect.left = 0
elif keys[pygame.K_RIGHT]:
player_rect.right += 5
if player_rect.right > SCREEN_WIDTH:
player_rect.right = SCREEN_WIDTH
elif keys[pygame.K_UP]:
player_rect.top -= 5
if player_rect.top < 0:
player_rect.top = 0
elif keys[pygame.K_DOWN]:
player_rect.bottom += 5
if player_rect.bottom > SCREEN_HEIGHT:
player_rect.bottom = SCREEN_HEIGHT
# 更新玩家飞机位置
player_group.sprites()[0].rect = player_rect
# 更新子弹位置
bullet_group.update()
# 更新敌机位置
enemy_group.update()
# 检测子弹是否击中敌机
hits = pygame.sprite.groupcollide(bullet_group, enemy_group, True, True)
for hit in hits:
enemy_down_sound.play()
enemy = Enemy()
enemy_group.add(enemy)
# 检测敌机是否撞到玩家飞机
hits = pygame.sprite.spritecollide(player_group.sprites()[0], enemy_group, True)
if hits:
game_over()
# 绘制游戏元素
screen.blit(bg_img, (0, 0))
player_group.draw(screen)
bullet_group.draw(screen)
enemy_group.draw(screen)
pygame.display.update()
# 控制游戏帧率
clock.tick(60)
```
阅读全文