帮我用python写一个飞机大战
时间: 2023-03-09 07:14:22 浏览: 165
好的,我可以帮助你用 Python 写一个飞机大战游戏。
首先,我们需要导入 Python 的 pygame 库。pygame 是一个专门用于制作游戏的库,它提供了许多有用的工具,例如绘图、声音、输入和动画。
接下来,我们可以创建一个窗口,在窗口中绘制背景、飞机和子弹。我们还可以使用 pygame 库的事件处理功能来处理用户的输入,例如键盘按键和鼠标点击。
最后,我们可以使用 pygame 库的动画功能来让飞机动起来,并在每一帧之间更新游戏状态,例如飞机的位置、子弹的位置和敌人的位置。
这是一个简单的例子,你可以通过添加更多的元素,例如敌人、障碍物、道具等来使游戏更加丰富多彩。
希望这些信息能帮到你!
相关问题
帮我用Python写一个飞机大战,注释要中文的
很高兴为您服务!我可以为您提供基础的Python代码框架,您可以自行添加功能和注释。# 导入所需的模块
import pygame
import random# 初始化游戏
pygame.init()# 创建游戏窗口
screen = pygame.display.set_mode((800, 600))# 游戏标题
pygame.display.set_caption("飞机大战")# 加载游戏背景图片
background = pygame.image.load('background.png')# 加载游戏角色图片
player_image = pygame.image.load('player.png')
enemy_image = pygame.image.load('enemy.png')# 设置角色初始位置
player_x = 370
player_y = 480
enemy_x = random.randint(0, 736)
enemy_y = 0# 定义移动步长
move_x = 0
move_y = 0# 定义玩家移动步长
player_speed = 5# 定义子弹大小和位置
bullet_x = 0
bullet_y = 480
bullet_y_change = 10# 定义敌机移动步长
enemy_speed = 5# 加载子弹图片
bullet_image = pygame.image.load('bullet.png')# 加载背景音乐
pygame.mixer.music.load('background.wav')# 播放背景音乐
pygame.mixer.music.play(-1)# 判断游戏是否结束
running = True# 开始游戏循环
while running:
# 设置游戏背景颜色
screen.fill((0, 0, 0))
# 加载背景图片
screen.blit(background, (0, 0))
# 加载玩家图片
screen.blit(player_image, (player_x, player_y))
# 加载敌机图片
screen.blit(enemy_image, (enemy_x, enemy_y))
# 加载子弹图片
screen.blit(bullet_image, (bullet_x, bullet_y))
# 循环遍历获取按键
for event in pygame.event.get():
# 判断是否点击了退出按钮
if event.type == pygame.QUIT:
# 停止游戏循环
running = False
# 判断是否按下了键
if event.type == pygame.KEYDOWN:
# 判断按下的是上下左右键
if event.key == pygame.K_LEFT:
move_x = -player_speed
if event.key == pygame.K_RIGHT:
move_x = player_speed
if event.key == pygame.K_UP:
move_y = -player_speed
if event.key == pygame.K_DOWN:
move_y = player_speed
# 判断是否松开了键
if event.type == pygame.KEYUP:
move_x = 0
move_y = 0
# 移动玩家位置
player_x += move_x
player_y += move_y
# 移动敌机位置
enemy_y += enemy_speed
# 移动子弹位置
bullet_y -= bullet_y_change # 设置游戏边界
if player_x < 0:
player_x = 0
if player_x > 736:
player_x = 736
if player_y < 0:
player_y = 0
if player_y > 536:
player_y = 536 # 子弹边界
if bullet_y < 0:
bullet_y = 480
bullet_x = player_x # 判断玩家和敌机是否发生碰撞
if enemy_y > 536:
enemy_y = 0
enemy_x = random.randint(0, 736)
if player_x > enemy_x and player_x < enemy_x + 64 or player_x + 64 > enemy_x and player_x + 64 < enemy_x + 64:
if player_y > enemy_y and player_y < enemy_y + 64 or player_y + 64 > enemy_y and player_y + 64 < enemy_y + 64:
# 碰撞后重置游戏
player_x = 370
player_y = 480
# 判断子弹和敌机是否发生碰撞
if bullet_y > enemy_y and bullet_y < enemy_y + 64 or bullet_y + 64 > enemy_y and bullet_y + 64 < enemy_y + 64:
if bullet_x > enemy_x and bullet_x < enemy_x + 64 or bullet_x + 64 > enemy_x and bullet_x + 64 < enemy_x + 64:
# 碰撞后重置游戏
enemy_y = 0
enemy_x = random.randint(0, 736)
bullet_y = 480
bullet_x = player_x # 刷新游戏界面
pygame.display.update()
帮我用Python写一段飞机大战的游戏代码
好的,以下是一份简单的飞机大战游戏代码,使用Python Pygame库实现:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 游戏窗口大小
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800
# 颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("飞机大战")
# 载入图片
bg_img = pygame.image.load("images/bg.jpg").convert()
player_img = pygame.image.load("images/hero.png").convert_alpha()
enemy1_img = pygame.image.load("images/enemy1.png").convert_alpha()
enemy2_img = pygame.image.load("images/enemy2.png").convert_alpha()
bullet_img = pygame.image.load("images/bullet.png").convert_alpha()
bomb_img = pygame.image.load("images/bomb.png").convert_alpha()
# 设置字体
font = pygame.font.Font(None, 36)
# 音效
pygame.mixer.music.load("sounds/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1)
bullet_sound = pygame.mixer.Sound("sounds/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sounds/use_bomb.wav")
bomb_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sounds/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sounds/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sounds/enemy1_down.wav")
enemy1_down_sound.set_volume(0.1)
enemy2_down_sound = pygame.mixer.Sound("sounds/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
game_over_sound = pygame.mixer.Sound("sounds/game_over.wav")
game_over_sound.set_volume(0.2)
# 玩家飞机类
class Player(pygame.sprite.Sprite):
def __init__(self, plane_img, plane_rect, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = plane_img
self.rect = plane_rect
self.rect.topleft = init_pos
self.speed = 8
self.bullets = pygame.sprite.Group()
self.is_hit = False
self.bomb_num = 3
self.bullet_num = 0
self.invincible_ticks = 0
def move_up(self):
if self.rect.top > 0:
self.rect.top -= self.speed
def move_down(self):
if self.rect.bottom < SCREEN_HEIGHT - 60:
self.rect.top += self.speed
def move_left(self):
if self.rect.left > 0:
self.rect.left -= self.speed
def move_right(self):
if self.rect.right < SCREEN_WIDTH:
self.rect.right += self.speed
def shoot(self):
bullet = Bullet(bullet_img, self.rect.midtop)
self.bullets.add(bullet)
bullet_sound.play()
def use_bomb(self):
if self.bomb_num > 0:
self.bomb_num -= 1
bomb_sound.play()
for enemy in enemies:
if enemy.rect.bottom > 0:
enemy.hit()
def get_bomb(self):
self.bomb_num += 1
get_bomb_sound.play()
def get_bullet(self):
self.bullet_num += 30
get_bullet_sound.play()
def update(self, invincible_ticks):
if invincible_ticks > 0:
self.invincible_ticks = invincible_ticks
else:
self.invincible_ticks = 0
if self.invincible_ticks % 2 == 0:
self.is_hit = False
screen.blit(self.image, self.rect)
else:
self.is_hit = True
# 敌机类
class Enemy(pygame.sprite.Sprite):
def __init__(self, enemy_img, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = enemy_img
self.rect = self.image.get_rect()
self.rect.topleft = init_pos
self.speed = random.randint(3, 5)
self.is_hit = False
self.down_ticks = 0
def move(self):
self.rect.top += self.speed
def hit(self):
self.is_hit = True
self.down_ticks = 10
def update(self):
if not self.is_hit:
self.move()
screen.blit(self.image, self.rect)
else:
if self.down_ticks == 10:
if self.image == enemy1_img:
enemy1_down_sound.play()
elif self.image == enemy2_img:
enemy2_down_sound.play()
self.down_ticks -= 1
if self.down_ticks == 0:
self.kill()
# 子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self, bullet_img, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_img
self.rect = self.image.get_rect()
self.rect.midbottom = init_pos
self.speed = 10
def move(self):
self.rect.top -= self.speed
def update(self):
self.move()
if self.rect.bottom <= 0:
self.kill()
# 游戏结束函数
def game_over(score):
game_over_font = pygame.font.Font(None, 48)
game_over_text = game_over_font.render("游戏结束", True, WHITE)
game_over_rect = game_over_text.get_rect()
game_over_rect.centerx = screen.get_rect().centerx
game_over_rect.centery = screen.get_rect().centery - 60
screen.blit(game_over_text, game_over_rect)
score_font = pygame.font.Font(None, 36)
score_text = score_font.render("得分:" + str(score), True, WHITE)
score_rect = score_text.get_rect()
score_rect.centerx = screen.get_rect().centerx
score_rect.centery = screen.get_rect().centery
screen.blit(score_text, score_rect)
pygame.display.update()
pygame.mixer.music.stop()
game_over_sound.play()
pygame.time.delay(2000)
pygame.quit()
exit()
# 创建玩家飞机
player_rect = player_img.get_rect()
player_rect.topleft = [SCREEN_WIDTH // 2 - player_rect.width // 2, SCREEN_HEIGHT - player_rect.height - 60]
player = Player(player_img, player_rect, [SCREEN_WIDTH // 2 - player_rect.width // 2, SCREEN_HEIGHT - player_rect.height - 60])
# 创建敌机
enemies = pygame.sprite.Group()
enemies_down = pygame.sprite.Group()
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 500)
enemy1_down_imgs = []
for i in range(1, 6):
enemy1_down_imgs.append(pygame.image.load("images/enemy1_down" + str(i) + ".png").convert_alpha())
enemy2_down_imgs = []
for i in range(1, 6):
enemy2_down_imgs.append(pygame.image.load("images/enemy2_down" + str(i) + ".png").convert_alpha())
# 分数
score = 0
# 游戏主循环
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_UP:
player.move_up()
elif event.key == pygame.K_DOWN:
player.move_down()
elif event.key == pygame.K_LEFT:
player.move_left()
elif event.key == pygame.K_RIGHT:
player.move_right()
elif event.key == pygame.K_SPACE:
player.shoot()
elif event.key == pygame.K_b:
player.use_bomb()
elif event.type == ADDENEMY:
enemy_pos = [random.randint(0, SCREEN_WIDTH - enemy1_img.get_width()), 0]
enemy = Enemy(enemy1_img, enemy_pos)
enemies.add(enemy)
# 绘制背景
screen.blit(bg_img, (0, 0))
# 绘制玩家飞机
if not player.is_hit:
screen.blit(player.image, player.rect)
else:
player.invincible_ticks -= 1
if player.invincible_ticks == 0:
player.is_hit = False
# 绘制子弹
player.bullets.update()
player.bullets.draw(screen)
# 绘制敌机
enemies.update()
enemies.draw(screen)
# 碰撞检测
if pygame.sprite.spritecollide(player, enemies, False) and not player.is_hit:
player.is_hit = True
player.invincible_ticks = 60
for enemy in enemies:
enemy.hit()
if player.bomb_num > 0:
player.use_bomb()
else:
game_over(score)
for bullet in player.bullets:
if pygame.sprite.spritecollide(bullet, enemies, True):
if bullet.rect.bottom > 0:
bullet.kill()
score += 10
# 更新分数
score_text = font.render("得分:" + str(score), True, WHITE)
screen.blit(score_text, (10, 10))
# 获取道具
prop_type = random.randint(0, 1000)
if prop_type == 0:
prop_pos = [random.randint(0, SCREEN_WIDTH - bomb_img.get_width()), 0]
prop = Prop(bomb_img, prop_pos, 0)
props.add(prop)
elif prop_type == 1:
prop_pos = [random.randint(0, SCREEN_WIDTH - bullet_img.get_width()), 0]
prop = Prop(bullet_img, prop_pos, 1)
props.add(prop)
# 更新道具
props.update()
props.draw(screen)
# 更新屏幕
pygame.display.update()
# 游戏结束
if player.is_hit:
player.invincible_ticks -= 1
if player.invincible_ticks == 0:
player.is_hit = False
if player.bomb_num <= 0 and not pygame.sprite.spritecollide(player, props, True, pygame.sprite.collide_mask):
game_over(score)
```
请注意,此代码仅提供了一个基本的游戏框架,您可以在此基础上添加更多功能和改进。
阅读全文