使用python实现游戏打飞机
时间: 2023-12-25 17:28:15 浏览: 66
好的,下面是一个简单的打飞机游戏的Python实现,你可以参考一下:
```python
import pygame
import random
# 初始化 pygame
pygame.init()
# 设置窗口大小和标题
WINDOW_WIDTH = 480
WINDOW_HEIGHT = 700
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("打飞机")
# 加载图片和音效资源
bg_img = pygame.image.load("bg.png")
plane_img = pygame.image.load("plane.png")
bullet_img = pygame.image.load("bullet.png")
enemy_img = pygame.image.load("enemy.png")
boom_imgs = [pygame.image.load("boom1.png"), pygame.image.load("boom2.png"),
pygame.image.load("boom3.png"), pygame.image.load("boom4.png")]
bullet_sound = pygame.mixer.Sound("bullet.wav")
boom_sound = pygame.mixer.Sound("boom.wav")
# 设置字体
font = pygame.font.SysFont("Arial", 32)
# 定义游戏对象的类
class GameObject:
def __init__(self, x, y, img):
self.x = x
self.y = y
self.img = img
self.width = img.get_width()
self.height = img.get_height()
def draw(self):
window.blit(self.img, (self.x, self.y))
def collide_with(self, other):
return (self.x < other.x + other.width and self.x + self.width > other.x and
self.y < other.y + other.height and self.y + self.height > other.y)
# 定义子弹类
class Bullet(GameObject):
def __init__(self, x, y, img):
super().__init__(x, y, img)
self.speed = -10
def move(self):
self.y += self.speed
def is_out_of_screen(self):
return self.y < -self.height
# 定义敌人类
class Enemy(GameObject):
def __init__(self, x, y, img):
super().__init__(x, y, img)
self.speed = 5
self.direction = random.choice([-1, 1])
def move(self):
self.x += self.direction * self.speed
self.y += self.speed
def is_out_of_screen(self):
return self.y > WINDOW_HEIGHT
# 定义游戏主循环
def game_loop():
# 初始化游戏对象
player = GameObject(WINDOW_WIDTH // 2 - plane_img.get_width() // 2, WINDOW_HEIGHT - plane_img.get_height(), plane_img)
bullets = []
enemies = []
score = 0
game_over = False
# 设置游戏时钟
clock = pygame.time.Clock()
# 游戏主循环
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
# 发射子弹
bullet_sound.play()
bullet = Bullet(player.x + player.width // 2 - bullet_img.get_width() // 2,
player.y - bullet_img.get_height(), bullet_img)
bullets.append(bullet)
# 移动玩家
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player.x > 0:
player.x -= 5
if keys[pygame.K_RIGHT] and player.x < WINDOW_WIDTH - player.width:
player.x += 5
if keys[pygame.K_UP] and player.y > 0:
player.y -= 5
if keys[pygame.K_DOWN] and player.y < WINDOW_HEIGHT - player.height:
player.y += 5
# 移动子弹和敌人
for bullet in bullets:
bullet.move()
for enemy in enemies:
enemy.move()
# 生成敌人
if random.randint(1, 60) == 1:
enemy = Enemy(random.randint(0, WINDOW_WIDTH - enemy_img.get_width()), -enemy_img.get_height(), enemy_img)
enemies.append(enemy)
# 检测子弹和敌人的碰撞
for bullet in bullets:
for enemy in enemies:
if bullet.collide_with(enemy):
boom_sound.play()
bullets.remove(bullet)
enemies.remove(enemy)
score += 1
# 检测敌人和玩家的碰撞
for enemy in enemies:
if enemy.collide_with(player):
game_over = True
# 移除超出屏幕的子弹和敌人
bullets = [bullet for bullet in bullets if not bullet.is_out_of_screen()]
enemies = [enemy for enemy in enemies if not enemy.is_out_of_screen()]
# 绘制背景和游戏对象
window.blit(bg_img, (0, 0))
player.draw()
for bullet in bullets:
bullet.draw()
for enemy in enemies:
enemy.draw()
# 绘制分数
text = font.render("Score: " + str(score), True, (255, 255, 255))
window.blit(text, (10, 10))
# 更新屏幕
pygame.display.update()
# 控制帧率
clock.tick(60)
# 游戏结束
window.fill((0, 0, 0))
text = font.render("Game Over", True, (255, 255, 255))
window.blit(text, (WINDOW_WIDTH // 2 - text.get_width() // 2, WINDOW_HEIGHT // 2 - text.get_height() // 2))
pygame.display.update()
pygame.time.delay(2000)
# 启动游戏主循环
game_loop()
# 退出 pygame
pygame.quit()
```
这个游戏有一个飞机和一些敌人,玩家可以使用方向键控制飞机移动,按空格键发射子弹,击落敌人可以得分,撞到敌人或者敌人飞出屏幕则游戏结束。你可以根据自己的需求对代码进行修改和优化。
阅读全文