提供一个python的打飞机游戏代码
时间: 2023-11-13 08:55:25 浏览: 79
这里给您提供一个简单的Python打飞机游戏的代码,使用Pygame库实现:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 设置游戏窗口
screen_width = 480
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("打飞机")
# 加载背景图片
background_img = pygame.image.load("images/background.png").convert()
# 加载玩家飞机图片
player_img = pygame.image.load("images/player.png").convert_alpha()
player_width, player_height = player_img.get_size()
# 加载敌机图片
enemy_img = pygame.image.load("images/enemy.png").convert_alpha()
enemy_width, enemy_height = enemy_img.get_size()
# 加载子弹图片
bullet_img = pygame.image.load("images/bullet.png").convert_alpha()
bullet_width, bullet_height = bullet_img.get_size()
# 设置字体
font = pygame.font.SysFont(None, 32)
# 定义函数:绘制文本
def draw_text(text, color, x, y):
text_surface = font.render(text, True, color)
screen.blit(text_surface, (x, y))
# 定义类:玩家飞机
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = player_img
self.rect = self.image.get_rect()
self.rect.centerx = screen_width // 2
self.rect.bottom = screen_height - 10
self.speed_x = 0
self.speed_y = 0
def update(self):
self.rect.x += self.speed_x
self.rect.y += self.speed_y
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > screen_width:
self.rect.right = screen_width
if self.rect.top < 0:
self.rect.top = 0
elif self.rect.bottom > screen_height:
self.rect.bottom = screen_height
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
# 定义类:敌机
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = enemy_img
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, screen_width - enemy_width)
self.rect.y = random.randint(-enemy_height, -10)
self.speed_y = random.randint(1, 3)
def update(self):
self.rect.y += self.speed_y
if self.rect.top > screen_height:
self.kill()
# 定义类:子弹
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = bullet_img
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speed_y = -10
def update(self):
self.rect.y += self.speed_y
if self.rect.bottom < 0:
self.kill()
# 创建精灵组
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
# 创建玩家飞机
player = Player()
all_sprites.add(player)
# 设置帧率
clock = pygame.time.Clock()
fps = 60
# 游戏循环
score = 0
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.speed_x = -5
elif event.key == pygame.K_RIGHT:
player.speed_x = 5
elif event.key == pygame.K_UP:
player.speed_y = -5
elif event.key == pygame.K_DOWN:
player.speed_y = 5
elif event.key == pygame.K_SPACE:
player.shoot()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player.speed_x = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
player.speed_y = 0
# 更新精灵组
all_sprites.update()
# 生成敌机
if random.randint(0, 50) == 0:
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
# 检测碰撞
hits = pygame.sprite.groupcollide(enemies, bullets, True, True)
for hit in hits:
score += 10
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
hits = pygame.sprite.spritecollide(player, enemies, False)
if hits:
running = False
# 绘制屏幕
screen.blit(background_img, (0, 0))
all_sprites.draw(screen)
draw_text("得分: " + str(score), (255, 255, 255), 10, 10)
pygame.display.update()
# 控制帧率
clock.tick(fps)
# 结束游戏
pygame.quit()
```
这个游戏中,玩家控制飞机左右移动,上下移动,按空格键发射子弹,击落敌机得分,如果被敌机撞到则游戏结束。您可以根据需要进行修改和扩展。
阅读全文