gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
写一个飞机大战小游戏
- User
写一个飞机大战小游戏
- Assistant
写一个飞机大战小游戏
这是一个简单的飞机大战小游戏的示例代码:
import pygame
import random
# 初始化pygame
pygame.init()
# 设置游戏窗口大小
window_width = 480
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("飞机大战")
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# 加载飞机图片
player_img = pygame.image.load('player.png')
enemy_img = pygame.image.load('enemy.png')
# 获取飞机图片的宽高
player_width = player_img.get_width()
player_height = player_img.get_height()
enemy_width = enemy_img.get_width()
enemy_height = enemy_img.get_height()
# 定义玩家飞机的初始位置
player_x = (window_width - player_width) // 2
player_y = window_height - player_height - 20
# 定义敌机的初始位置和速度
enemy_x = random.randint(0, window_width - enemy_width)
enemy_y = 0
enemy_speed = 3
# 定义玩家的移动速度
player_speed = 5
# 定义玩家子弹的初始位置和速度
bullet_x = player_x + player_width // 2
bullet_y = player_y
bullet_speed = 7
bullet_width = 5
bullet_height = 10
bullet_state = "ready" # ready表示子弹不可见,fire表示子弹正在发射中
# 定义分数
score = 0
# 加载字体
font = pygame.font.SysFont(None, 30)
# 定义显示分数的函数
def show_score(x, y):
score_text = font.render("Score: " + str(score), True, WHITE)
window.blit(score_text,(x, y))
# 定义显示玩家飞机的函数
def show_player(x, y):
window.blit(player_img, (x, y))
# 定义显示敌机的函数
def show_enemy(x, y):
window.blit(enemy_img, (x, y))
# 定义发射子弹的函数
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
window.blit(pygame.Surface([bullet_width, bullet_height]), (x, y))
# 定义碰撞检测函数
def is_collision(enemy_x, enemy_y, bullet_x, bullet_y):
if bullet_x >= enemy_x and bullet_x <= enemy_x + enemy_width and bullet_y >= enemy_y and bullet_y <= enemy_y + enemy_height:
return True
return False
# 游戏主循环
running = True
while running:
window.fill(BLACK) # 清空窗口
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 处理键盘按下事件
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x -= player_speed
if event.key == pygame.K_RIGHT:
player_x += player_speed
if event.key == pygame.K_SPACE and bullet_state == "ready":
bullet_x = player_x + player_width // 2
fire_bullet(bullet_x, bullet_y)
# 控制玩家飞机不能超出窗口边界
if player_x < 0:
player_x = 0
elif player_x > window_width - player_width:
player_x = window_width - player_width
# 移动敌机
enemy_y += enemy_speed
# 检测敌机是否飞出窗口底部,如果是则重新生成
if enemy_y > window_height:
enemy_x = random.randint(0, window_width - enemy_width)
enemy_y = 0
score += 1
# 检测子弹是否击中敌机
if is_collision(enemy_x, enemy_y, bullet_x, bullet_y):
bullet_state = "ready"
enemy_x = random.randint(0, window_width - enemy_width)
enemy_y = 0
score += 1
# 移动子弹
if bullet_state == "fire":
bullet_y -= bullet_speed
# 如果子弹飞出窗口上方,重置子弹位置
if bullet_y < 0:
bullet_state = "ready"
bullet_y = player_y
show_enemy(enemy_x, enemy_y)
show_player(player_x, player_y)
if bullet_state == "fire":
fire_bullet(bullet_x, bullet_y)
show_score(10, 10)
pygame.display.update() # 更新窗口显示
# 退出游戏
pygame.quit()
请注意,该代码需要配合图片文件(player.png和enemy.png)和pygame库的安装使用。