Python小游戏代码大全可复制免费

我需要一些免费的Python小游戏代码,以便我可以学习和练习编程技能。这些代码应该是易于理解和复制的,并且不需要额外的库或工具。

2 个回答

陈sir
以下整理了一些简单且可复制的 **Python 小游戏代码**(可直接复制运行,需安装对应依赖库): --- ### 1. 猜数字游戏 python import random number = random.randint(1, 100) guess = 0 count = 0 print("猜数字游戏(1-100),输入数字开始吧!") while guess != number: guess = int(input("请输入你的猜测:")) count += 1 if guess < number: print("猜小了!") elif guess > number: print("猜大了!") else: print(f"恭喜!你用了 {count} 次猜对了!") --- ### 2. 贪吃蛇(需安装 `pygame`) python # 安装依赖:pip install pygame import pygame import random import time # 初始化 pygame.init() width, height = 600, 400 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("贪吃蛇") # 颜色定义 WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) # 蛇和食物初始化 snake = [[100, 50], [90, 50], [80, 50]] food = [random.randrange(1, width//10)*10, random.randrange(1, height//10)*10] direction = "RIGHT" clock = pygame.time.Clock() # 游戏循环 running = True while running: screen.fill(WHITE) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP and direction != "DOWN": direction = "UP" elif event.key == pygame.K_DOWN and direction != "UP": direction = "DOWN" elif event.key == pygame.K_LEFT and direction != "RIGHT": direction = "LEFT" elif event.key == pygame.K_RIGHT and direction != "LEFT": direction = "RIGHT" # 移动蛇 head = snake[0].copy() if direction == "UP": head[1] -= 10 elif direction == "DOWN": head[1] += 10 elif direction == "LEFT": head[0] -= 10 elif direction == "RIGHT": head[0] += 10 snake.insert(0, head) if head == food: food = [random.randrange(1, width//10)*10, random.randrange(1, height//10)*10] else: snake.pop() # 绘制 for pos in snake: pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], 10, 10)) pygame.draw.rect(screen, RED, pygame.Rect(food[0], food[1], 10, 10)) pygame.display.flip() clock.tick(15) pygame.quit() --- ### 3. 井字棋(Tic-Tac-Toe) python def print_board(board): for row in board: print(" | ".join(row)) print("-" * 9) def check_win(board, player): for row in board: if all(cell == player for cell in row): return True for col in range(3): if all(board[row][col] == player for row in range(3)): return True if all(board[i][i] == player for i in range(3)) or all(board[i][2-i] == player for i in range(3)): return True return False board = [[" "]*3 for _ in range(3)] players = ["X", "O"] current = 0 while True: print_board(board) player = players[current] row = int(input(f"玩家 {player},输入行号(0-2):")) col = int(input(f"玩家 {player},输入列号(0-2):")) if board[row][col] == " ": board[row][col] = player if check_win(board, player): print_board(board) print(f"玩家 {player} 获胜!") break current = 1 - current else: print("该位置已被占用!") --- ### 4. 免费资源推荐 1. **GitHub 仓库** - [Python小游戏合集](https://github.com/topics/python-games) - [经典游戏复刻](https://github.com/grantjenks/free-python-games) 2. **教程网站** - [Real Python](https://realpython.com) - [FreeCodeCamp](https://www.freecodecamp.org) --- **提示**:直接复制代码后,需安装必要的库(如 `pygame`),并确保 Python 版本 ≥3.6。
清凉一吻
当然,这里有一些免费的Python小游戏代码,你可以尝试学习和修改它们: ```python # 小游戏:猜数字 import random secret_number = random.randint(1, 100) guesses = 0 print(