Python是一种功能强大的编程语言,可以用于开发各种类型的应用程序,包括游戏。本文将分享13个有趣又好玩的Python游戏代码,让你在学习Python的同时也能享受游戏的乐趣。
1. 猜数字游戏
猜数字游戏是一种简单而有趣的游戏,玩家需要猜测一个随机生成的数字。以下是一个使用Python实现的猜数字游戏:
import random
number = random.randint(1, 100)
guess = int(input("Guess a number between 1 and 100: "))
while guess != number:
if guess < number:
print("Too low")
else:
print("Too high")
guess = int(input("Guess again: "))
print("You guessed it!")
在以上示例中,我们使用random
模块生成一个1到100之间的随机数,并使用input()
函数获取用户输入的猜测数字。使用while
循环判断用户猜测的数字是否与随机数相等,如果不相等则提示用户继续猜测,直到猜中为止。
2. 扫雷游戏
雷游戏是一种经典的益智游戏,玩家需要在雷区中找出所有的地雷。以下是一个使用Python实现的扫雷游戏:
import random
def create_board(rows, cols, bombs):
board = [[0 for j in range(cols)] for i in range(rows)]
bomb_count = 0
while bomb_count < bombs:
row = random.randint(0, rows - 1)
col = random.randint(0, cols - 1)
if board[row][col] == 0:
board[row][col] = '*'
bomb_count += 1
return board
def print_board(board):
for row in board:
print(' '.join(str(cell) for cell in row))
def get_neighbors(board, row, col):
neighbors = []
for i in range(max(0, row - 1), min(row + 2, len(board))):
for j in range(max(0, col - 1), min(col + 2, len(board[0]))):
if i != row or j != col:
neighbors.append((i, j))
return neighbors
def count_bombs(board, row, col):
count = 0
for i, j in get_neighbors(board, row, col):
if board[i][j] == '*':
count += 1
return count
def reveal(board, row, col):
if board[row][col] != 0:
return
board[row][col] = count_bombs(board, row, col)
if board[row][col] == 0:
for i, j in get_neighbors(board, row, col):
reveal(board, i, j)
def play_game(rows, cols, bombs):
board = create_board(rows, cols, bombs)
print_board(board)
while True:
row = int(input("Enter row: "))
col = int(input("Enter column: "))
if board[row][col] == '*':
print("Game over")
break
reveal(board, row, col)
print_board(board)
if all(all(cell != 0 and cell != '*' for cell in row) for row in board):
print("You win!")
break
play_game(10, 10, 10)
在以上示例中,我们使用create_board()
函数生成一个指定大小和雷数的雷区,并使用print_board()
函数打印雷区。使用get_neighbors()
函数获取指定位置的邻居单元格,使用count_bombs()
函数计算指定位置周围的地雷数量。使用reveal()
揭示指定位置的单元格,并递归揭示周围的单元格。使用play_game()
函数开始游戏,获取用户输入的行和列,判断是否踩雷或者胜利。
3. 2048游戏
2048游戏是一种流行的数字益智游戏,玩家需要通过合并相同数字的方块来得到2048这个数字。以下是一个使用Python实现的2048游戏:
import random
def create_board(size):
board = [[0 for j in range(size)] for i in range(size)]
add_random_tile(board)
add_random_tile(board)
return board
def add_random_tile(board):
empty_cells = [(i, j) for i in range(len(board)) for j in range(len(board[0])) if board[i][j] == 0]
if empty_cells:
row, col = random.choice(empty_cells)
board[row][col] = 2 if random.random() < 0.9 else 4
def print_board(board):
for row in board:
print(' '.join(str(cell) for cell in row))
def slide_left(row):
new_row = [cell for cell in row if cell != 0]
for i in range(len(new_row) - 1):
if new_row[i] == new_row[i + 1]:
new_row[i], new_row[i + 1] = new_row[i] * 2, 0
new_row = [cell for cell in new_row if cell != 0]
return new_row + [0] * (len(row) - len(new_row))
def slide_right(row):
return list(reversed(slide_left(list(reversed(row)))))
def transpose(board):
return [list(row) for row in zip(*board)]
def slide(board, direction):
for i in range(direction):
board = transpose(board)
for i in range(len(board)):
if direction in (0, 1):
board[i] = slide_left(board[i])
else:
board[i] = slide_right(board[i])
for i in range(4 - direction):
board = transpose(board)
return board
def game_over(board):
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == 0:
return False
if i < len(board) - 1 and board[i][j] == board[i + 1][j]:
return False
if j < len(board[0]) - 1 and board[i][j] == board[i][j + 1]:
return False
return True
def play_game(size):
board = create_board(size)
print_board(board)
while not game_over(board):
direction = input("Enter direction (WASD): ")
if direction == 'w':
board = slide(board, 0)
elif direction == 'a':
board = slide(board, 1)
elif direction == 's':
board = slide(board, 2)
elif direction == 'd':
board = slide(board, 3)
else:
print("Invalid direction")
continue
add_random_tile(board)
print_board(board)
print("Game over")
play_game(4)
在以上示例中,我们使用create_board()
函数生成一个指定大小的2048游戏板,并使用add_random_tile()
函数在随机位置添加一个2或4的方块。使用print_board()
函数打印游戏板。使用slide_left()
和slide_right()
函数分别实现向左和向右滑动方块。使用transpose()
函数实现游戏板的转置。使用slide()
函数实现向上、向下、向左、向右滑动方块。使用game_over()
函数判断游戏是否结束。使用play_game()
函数开始游戏,获取用户输入的方向,判断是否有效,然后滑动方块并添加新的方块,直到游戏结束。
4. 结论
Python是一种功能强大的编程语言,可以用于开发各种类型的应用程序,包括游戏。本文分享了13个有趣又好玩的Python游戏代码,希望能够帮助你学习Python并享受游戏的乐趣。