python飞机大战游戏


import pygame
import random

# 初始化Pygame
pygame.init()

# 游戏窗口设置
win_width, win_height = 800, 600
window = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("飞机大战 Game")

# 颜色设置
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

# 飞机设置
player_width, player_height = 50, 50
player_x = win_width // 2 - player_width // 2
player_y = win_height - player_height
player_speed = 5
player_img = pygame.image.load("player.png")

# 子弹设置
bullet_width, bullet_height = 10, 30
bullet_x = 0
bullet_y = player_y
bullet_speed = 10
bullet_state = "ready"  # "ready"表示子弹可以发射,"fire"表示子弹正在飞行中
bullet_img = pygame.image.load("bullet.png")

# 敌机设置
enemy_width, enemy_height = 50, 50
enemy_x = random.randint(0, win_width - enemy_width)
enemy_y = 0
enemy_speed = 4
enemy_img = pygame.image.load("enemy.png")

# 游戏分数
score = 0

# 游戏结束标志
game_over = False


# 绘制飞机函数
def draw_player(x, y):
    window.blit(player_img, (x, y))


# 绘制子弹函数
def draw_bullet(x, y):
    window.blit(bullet_img, (x, y))


# 绘制敌机函数
def draw_enemy(x, y):
    window.blit(enemy_img, (x, y))


# 碰撞检测函数
def is_collision(enemy_x, enemy_y, bullet_x, bullet_y):
    distance = ((enemy_x - bullet_x) ** 2 + (enemy_y - bullet_y) ** 2) ** 0.5
    if distance < 30:
        return True
    else:
        return False


# 游戏主循环
clock = pygame.time.Clock()

while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True

        # 监听键盘事件,控制飞机移动和子弹发射
        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:
                if bullet_state == "ready":
                    bullet_x = player_x + player_width // 2 - bullet_width // 2
                    bullet_state = "fire"
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                player_speed = 0

    # 更新飞机的位置
    player_x += player_speed

    # 控制飞机在窗口内移动
    if player_x < 0:
        player_x = 0
    elif player_x > win_width - player_width:
        player_x = win_width - player_width

    # 更新子弹的位置
    if bullet_state == "fire":
        bullet_y -= bullet_speed
        if bullet_y <= 0:
            bullet_y = player_y
            bullet_state = "ready"

    # 更新敌机的位置
    enemy_y += enemy_speed

    # 检查是否出界
    if enemy_y > win_height:
        enemy_x = random.randint(0, win_width - enemy_width)
        enemy_y = 0
        score += 1

    # 检查是否碰撞
    if is_collision(enemy_x, enemy_y, bullet_x, bullet_y):
        enemy_x = random.randint(0, win_width - enemy_width)
        enemy_y = 0
        score += 1
        bullet_y = player_y
        bullet_state = "ready"

    # 绘制背景
    window.fill(BLACK)

    # 绘制飞机
    draw_player(player_x, player_y)

    # 绘制子弹
    draw_bullet(bullet_x, bullet_y)

    # 绘制敌机
    draw_enemy(enemy_x, enemy_y)

    # 绘制分数
    font = pygame.font.Font(None, 36)
    text = font.render("Score: " + str(score), True, WHITE)
    window.blit(text, (10, 10))

    # 更新屏幕
    pygame.display.update()

    # 控制游戏帧率
    clock.tick(60)

# 游戏结束,退出Pygame
pygame.quit()

你可能感兴趣的:(python,游戏,pygame)