2025年5月7日 星期三

射擊遊戲 修


 # -*- coding: utf-8 -*-

import pygame

import sys

import random


pygame.init()


# 畫面設定

WIDTH, HEIGHT = 800, 600

WHITE = (255, 255, 255)

BLUE = (0, 200, 255)


screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("射擊遊戲")


# 載入圖片

enemy_img = pygame.image.load("enemy_ship.png")

enemy_img = pygame.transform.scale(enemy_img, (60, 40))


player_image = pygame.image.load("player_turret.jpg")

player_image = pygame.transform.scale(player_image, (80, 60))


# 玩家位置與速度

player_width = 80

player_height = 60

player_x = WIDTH // 2 - player_width // 2

player_y = HEIGHT - player_height - 10

player_speed = 8


# 子彈設定

bullet_width = 5

bullet_height = 10

bullet_speed = 8

bullets = []


# 敵人設定

enemy_rows = 5

enemy_cols = 10

enemy_width = WIDTH // enemy_cols

enemy_height = 40

enemies = []


for row in range(enemy_rows):

    for col in range(enemy_cols):

        x = col * enemy_width + 10

        y = row * enemy_height + 40

        rect = pygame.Rect(x, y, 60, 40)

        enemies.append({'rect': rect, 'alive': True})


# 分數

score = 0

font = pygame.font.SysFont(None, 36)


# 遊戲主迴圈

clock = pygame.time.Clock()

running = True

while running:

    clock.tick(60)


    # 事件處理

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            running = False

        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:

            bullet_x = player_x + player_width // 2 - bullet_width // 2

            bullet_y = player_y

            bullets.append(pygame.Rect(bullet_x, bullet_y, bullet_width, bullet_height))


    ss = random.randint(1,10)    

    if ss >= 9:

        bullet_x = player_x + player_width // 2 - bullet_width // 2

        bullet_y = player_y

        bullets.append(pygame.Rect(bullet_x, bullet_y, bullet_width, bullet_height))


    # 控制玩家左右移動

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:

        player_x -= player_speed

    if keys[pygame.K_RIGHT]:

        player_x += player_speed

    player_x += random.randint(-40,60)

    if player_x> WIDTH - player_width:

        for k in range(3):

            bullet_x = player_x + player_width // 2 - bullet_width // 2

            bullet_y = player_y

            bullets.append(pygame.Rect(bullet_x, bullet_y, bullet_width, bullet_height))

        player_x = 10

    player_x = max(0, min(WIDTH - player_width, player_x))


    # 更新子彈

    for bullet in bullets[:]:

        bullet.y -= bullet_speed

        if bullet.bottom < 0:

            bullets.remove(bullet)


    # 子彈擊中敵人

    for bullet in bullets[:]:

        for enemy in enemies:

            if enemy['alive'] and enemy['rect'].colliderect(bullet):

                enemy['alive'] = False

                bullets.remove(bullet)

                score += 10

                break


    # 畫面更新

    screen.fill(WHITE)


    # 畫敵人

    for enemy in enemies:

        if enemy['alive']:

            screen.blit(enemy_img, enemy['rect'].topleft)


    # 畫玩家炮塔

    screen.blit(player_image, (player_x, player_y))


    # 畫子彈

    for bullet in bullets:

        pygame.draw.rect(screen, BLUE, bullet)


    # 分數

    score_text = font.render(f"Score: {score}", True, (0, 0, 0))

    screen.blit(score_text, (10, 10))


    pygame.display.flip()


pygame.quit()

sys.exit()

沒有留言:

張貼留言