---
# System prepended metadata

title: 設計遊戲

---

# 進階程式設計 期末 Project
### (1/3、1/8、1/10 present )
1.小組成員 : 班級208座號7姓名林琮旂 班級208座號14姓名黃莨期

2.設計理念: 小時候有玩過雷霆戰機的經驗，加上這次老師教的程式碼，可以運用在類似的射擊遊戲上，於是就想復刻童年回憶。
3.遊戲說明
我們的遊戲為操空一台穿梭在太空中的飛行器，在前進的途中需要時刻注意從上方飛來的隕石，並使用上下左右鍵來控制飛機移動，藉此躲避隕石，此外飛機頂部也會發射出雷射光將隕石粉碎，亦可利用這個方式來讓自己存活。
4.遊戲目標: 採擊殺積分制，殺越多得分越高。
5.遊戲方式: 三條命，擊殺一名敵人得十分，殺越多得越多，分數高者勝。
6.開發時遭遇的困難(實例): 
   要將很多種程式碼結合，放錯位置就會沒辦法動
   本來是要設計成讓子彈按按鍵才開始射擊，但由於時間不足的緣故，只好放棄。

7.透過什麼方式解決(實例): 上網查資料、詢問老師、查看之前上課時的筆記、詢問AI

8.工作分配:
  a:林琮旂 遊戲開發(75%)、遊戲架構

  b:黃莨期 遊戲開發(25%)、圖片整理、簡報製作


# 程式碼
```python
import pygame
import random
import time

pygame.init()

WIDTH, HEIGHT = 800, 600

font = pygame.font.SysFont(None, 40) #創建字型系統

class Fighter(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((50, 50))
        self.image.fill((255, 255, 255))
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH // 2, HEIGHT // 2)
        self.speed = 8
        self.lives = 3

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and self.rect.left > 0:
            self.rect.x -= self.speed
        if keys[pygame.K_RIGHT] and self.rect.right < WIDTH:
            self.rect.x += self.speed
        if keys[pygame.K_UP] and self.rect.top > 0:
            self.rect.y -= self.speed
        if keys[pygame.K_DOWN] and self.rect.bottom < HEIGHT:
            self.rect.y += self.speed

class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface((10, 30))
        self.image.fill((255, 255, 255))
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.bottom = y
        self.speed = -15

    def update(self):
        self.rect.y += self.speed
        if self.rect.bottom < 0:
            self.kill()

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((50, 50))
        self.image.fill((255, 0, 0))
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(0, WIDTH - 50)
        self.rect.y = 0
        self.speed = random.randint(3, 7)

    def update(self):
        self.rect.y += self.speed
        if self.rect.top > HEIGHT:
            self.rect.x = random.randint(0, WIDTH - 50)
            self.rect.y = 0
            self.speed = random.randint(3, 7)

def restart_game(player, enemies, bullets, all_sprites, score):
    player.lives = 3
    enemies.empty()    #清空敵人
    bullets.empty()    #清空子彈
    all_sprites.empty()
    all_sprites.add(player)
    score = 0
    return player, enemies, bullets, all_sprites, score

def show_game_over_screen():
    screen.fill((0, 0, 0))
    game_over_text = font.render("Game Over", True, (255, 255, 255))
    score_text = font.render(f"Score: {score}", True, (255, 255, 255))  # 創建兩個文字 Surface，分別為 "Game Over" 和 "Score: {score}"。
    screen.blit(game_over_text, (WIDTH // 2 - 100, HEIGHT // 2 - 50))
    screen.blit(score_text, (WIDTH // 2 - 60, HEIGHT // 2))
    pygame.display.flip()
    time.sleep(5)  #停留五秒

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("戰鬥機射擊遊戲")

player = Fighter()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
bullets = pygame.sprite.Group()
enemies = pygame.sprite.Group()

score = 0
clock = pygame.time.Clock()
bullet_timer = pygame.time.get_ticks()#子彈發射頻率

running = True
game_over = False
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_r and game_over:#pygame.KEYDOWN按下按鍵pygame.K_r 按R鍵

            player, enemies, bullets, all_sprites, score = restart_game(player, enemies, bullets, all_sprites, score)#restart_game設定為初始狀態
            game_over = False
            game_over = False

    now = pygame.time.get_ticks() #獲取當時的時間
    if now - bullet_timer > 200:
        bullet_timer = now
        bullet1 = Bullet(player.rect.centerx, player.rect.top)
        bullet2 = Bullet(player.rect.centerx - 15, player.rect.top)
        bullet3 = Bullet(player.rect.centerx + 15, player.rect.top)
        all_sprites.add(bullet1, bullet2, bullet3)
        bullets.add(bullet1, bullet2, bullet3)

    if random.randint(0, 100) < 2:
        enemy = Enemy()
        all_sprites.add(enemy)
        enemies.add(enemy)

    all_sprites.update()

    hits = pygame.sprite.groupcollide(bullets, enemies, True, True)#碰撞偵測
    for bullet, enemy_list in hits.items():
        for enemy in enemy_list:
            score += 10

        if not enemies:
            pass

    hits = pygame.sprite.spritecollide(player, enemies, False)
    for hit in hits:
        player.lives -= 1
        hit.kill()
        if player.lives == 0:
            show_game_over_screen()
            game_over = True

    screen.fill((0, 0, 0))  # 黑色
    all_sprites.draw(screen)
    enemies.draw(screen)

    score_text = font.render(f"Score: {score}", True, (255, 255, 255))  # 白色
    screen.blit(score_text, (10, 10))

    lives_text = font.render(f"Lives: {player.lives}", True, (255, 255, 255))  # 白色
    screen.blit(lives_text, (WIDTH - 100, 10))

    if game_over:
        game_over_text = font.render("Press 'R' to Restart", True, (255, 255, 255))  # 白色
        screen.blit(game_over_text, (WIDTH // 2 - 120, HEIGHT // 2 + 50))

    pygame.display.flip()

    clock.tick(60)

pygame.quit()
```