用python写一个飞机大战小游戏
时间: 2023-08-31 21:32:22 浏览: 102
用Python写一个飞机大战小游戏
3星 · 编辑精心推荐
### 回答1:
好的,以下是一个简单的Python飞机大战小游戏的代码示例:
```
import pygame
import random
# 初始化pygame
pygame.init()
# 设置游戏窗口大小
screen_width = 480
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
# 加载背景图片
background = pygame.image.load('images/background.png')
# 加载飞机图片
plane_img = pygame.image.load('images/plane.png')
plane_rect = plane_img.get_rect()
plane_width = plane_rect.width
plane_height = plane_rect.height
# 加载子弹图片
bullet_img = pygame.image.load('images/bullet.png')
bullet_rect = bullet_img.get_rect()
bullet_width = bullet_rect.width
bullet_height = bullet_rect.height
# 加载敌机图片
enemy_img = pygame.image.load('images/enemy.png')
enemy_rect = enemy_img.get_rect()
enemy_width = enemy_rect.width
enemy_height = enemy_rect.height
# 设置字体
font = pygame.font.Font(None, 36)
# 设置游戏帧率
clock = pygame.time.Clock()
fps = 60
# 定义飞机类
class Plane(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = plane_img
self.rect = self.image.get_rect()
self.rect.centerx = screen_width // 2
self.rect.bottom = screen_height - 10
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
elif key_pressed[pygame.K_RIGHT]:
self.rect.x += self.speed
if key_pressed[pygame.K_SPACE]:
self.fire()
# 防止飞机飞出屏幕
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > screen_width:
self.rect.right = screen_width
def fire(self):
bullet = Bullet()
bullet.rect.centerx = self.rect.centerx
bullet.rect.bottom = self.rect.top
self.bullets.add(bullet)
# 定义子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = bullet_img
self.rect = self.image.get_rect()
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):
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 = random.randint(1, 5)
def update(self):
self.rect.y += self.speed
if self.rect.top > screen_height:
self.kill()
# 创建精灵组
all_sprites = pygame.sprite.Group()
# 创建飞机精灵
plane = Plane()
all_sprites.add(plane)
# 创建敌机精灵组
enemies = pygame.sprite.Group()
# 设置初始得分
score = 0
# 游戏主循环
running = True
while running:
### 回答2:
飞机大战是一个经典的小游戏,使用Python编写是一个很好的选择。下面是一个简单的飞机大战小游戏的Python代码示例:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口大小
window_width = 800
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
# 加载图片资源
background_image = pygame.image.load("background.jpg")
player_image = pygame.image.load("player.png")
enemy_image = pygame.image.load("enemy.png")
# 设置玩家初始位置
player_width = 50
player_height = 50
player_x = window_width / 2 - player_width / 2
player_y = window_height * 0.8
# 设置敌人初始位置
enemy_width = 50
enemy_height = 50
enemy_x = random.randint(0, window_width - enemy_width)
enemy_y = -enemy_height
# 设置玩家移动速度
player_speed = 5
# 设置敌人移动速度
enemy_speed = 2
# 游戏循环
running = True
while running:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 玩家移动
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < window_width - player_width:
player_x += player_speed
# 更新敌人位置
enemy_y += enemy_speed
if enemy_y > window_height:
enemy_x = random.randint(0, window_width - enemy_width)
enemy_y = -enemy_height
# 碰撞检测
if player_x < enemy_x + enemy_width and player_x + player_width > enemy_x \
and player_y < enemy_y + enemy_height and player_y + player_height > enemy_y:
print("游戏结束") # 碰撞后结束游戏
# 绘制游戏场景
window.blit(background_image, (0, 0))
window.blit(player_image, (player_x, player_y))
window.blit(enemy_image, (enemy_x, enemy_y))
pygame.display.update()
# 游戏结束
pygame.quit()
```
以上代码是一个简单的飞机大战小游戏,玩家使用左右箭头键来控制飞机移动,敌人会从屏幕上方随机位置下落,当玩家的飞机与敌人发生碰撞时游戏结束。这只是一个基本的实现,你可以根据自己的需求进行拓展和优化,添加更多功能和特效。
阅读全文