适合小白练习的几个Python小游戏项目-含开发思路
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:网站将不断更新








