1. 猜單詞遊戲
import random words = ["apple", "banana", "cherry", "date", "elderberry"] def guess_word(): word = random.choice(words) guessed_letters = [] attempts = 6 while attempts > 0: display_word = "" for letter in word: if letter in guessed_letters: display_word += letter else: display_word += "_" print(display_word) guess = input("請輸入一個字母:") if guess in word and guess not in guessed_letters: guessed_letters.append(guess) if all(letter in guessed_letters for letter in word): print("恭喜你猜對了單詞!") break else: attempts -= 1 print(f"猜錯了,你還有 {attempts} 次機會。") if attempts == 0: print(f"遊戲結束,單詞是 {word}") if __name__ == "__main__": guess_word()
2. 數字炸彈遊戲
import random def number_bomb(): num = random.randint(1, 100) guess = None attempts = 10 while guess!= num and attempts > 0: guess = int(input("請輸入你猜測的數字(1 - 100): ")) attempts -= 1 if guess < num: print("猜小了,還剩", attempts, "次機會。") elif guess > num: print("猜大了,還剩", attempts, "次機會。") if guess == num: print("恭喜你猜對了!") else: print("遊戲結束,數字是", num) if __name__ == "__main__": number_bomb()
3. 貪吃蛇遊戲
import pygame import random # 基礎設定 # 螢幕高度 SCREEN_HEIGHT = 480 # 螢幕寬度 SCREEN_WIDTH = 600 # 小方格大小 GRID_SIZE = 20 # 顏色定義 WHITE = (255, 255, 255) BLACK = (0, 0, 0) GREEN = (0, 255, 0) # 初始化 pygame pygame.init() # 建立螢幕 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("貪吃蛇遊戲") # 遊戲時鐘 clock = pygame.time.Clock() # 初始蛇的位置和長度 snake_pos = [200, 100] snake_body = [[snake_pos[0], snake_pos[1]]] # 食物的初始位置 food_pos = [random.randint(0, SCREEN_WIDTH // GRID_SIZE - 1) * GRID_SIZE, random.randint(0, SCREEN_HEIGHT // GRID_SIZE - 1) * GRID_SIZE] # 遊戲結束標誌 game_over = False # 遊戲方向,初始向右 direction = 'RIGHT' change_to = direction def game_over_screen(): font = pygame.font.SysFont(None, 48) text = font.render("遊戲結束,按任意鍵重新開始", True, WHITE) screen.blit(text, [SCREEN_WIDTH // 2 - 200, SCREEN_HEIGHT // 2 - 50]) pygame.display.flip() def update_snake(): global food_pos, game_over if not game_over: if direction == 'RIGHT': snake_pos[0] += GRID_SIZE elif direction == 'LEFT': snake_pos[0] -= GRID_SIZE elif direction == 'UP': snake_pos[1] -= GRID_SIZE elif direction == 'DOWN': snake_pos[1] += GRID_SIZE # 檢查是否吃到食物 if [snake_pos[0], snake_pos[1]] == food_pos: food_pos = [random.randint(0, SCREEN_WIDTH // GRID_SIZE - 1) * GRID_SIZE, random.randint(0, SCREEN_HEIGHT // GRID_SIZE - 1) * GRID_SIZE] else: # 去除蛇尾 del snake_body[0] # 檢查是否撞到自己或邊界 if [snake_pos[0], snake_pos[1]] in snake_body[1:]: game_over = True elif snake_pos[0] < 0 or snake_pos[0] >= SCREEN_WIDTH or snake_pos[1] < 0 or snake_pos[1] >= SCREEN_HEIGHT: game_over = True snake_body.append(list(snake_pos)) def draw_snake_and_food(): for pos in snake_body: pygame.draw.rect(screen, GREEN, [pos[0], pos[1], GRID_SIZE, GRID_SIZE]) pygame.draw.rect(screen, WHITE, [food_pos[0], food_pos[1], GRID_SIZE, GRID_SIZE]) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP and direction!= 'DOWN': change_to = 'UP' elif event.key == pygame.K_DOWN and direction!= 'UP': change_to = 'DOWN' elif event.key == pygame.K_LEFT and direction!= 'RIGHT': change_to = 'LEFT' elif event.key == pygame.K_RIGHT and direction!= 'LEFT': change_to = 'RIGHT' if change_to == 'UP' and direction!= 'DOWN': direction = 'UP' elif change_to == 'DOWN' and direction!= 'UP': direction = 'DOWN' elif change_to == 'LEFT' and direction!= 'RIGHT': direction = 'LEFT' elif change_to == 'RIGHT' and direction!= 'LEFT': direction = 'RIGHT' update_snake() screen.fill(BLACK) draw_snake_and_food() if game_over: game_over_screen() else: pygame.display.flip() clock.tick(10)
PS:網站將不斷更新