用python设计小游戏源代码_用Python设计一个经典小游戏
时间: 2024-03-01 16:51:09 浏览: 22
好的,我可以为你提供一个用 Python 设计的经典小游戏的源代码,比如俄罗斯方块。
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 设置游戏窗口的大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('俄罗斯方块')
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 定义方块的大小
block_size = 20
# 定义游戏区域大小
play_width = 10 * block_size
play_height = 20 * block_size
# 定义游戏区域的位置
play_x = (screen_width - play_width) // 2
play_y = screen_height - play_height - 50
# 定义字体
font = pygame.font.SysFont(None, 30)
# 定义方块类
class Block:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
# 定义方块形状
block_shapes = [
[[1, 1, 1],
[0, 1, 0]],
[[0, 2, 2],
[2, 2, 0]],
[[3, 3, 0],
[0, 3, 3]],
[[4, 0, 0],
[4, 4, 4]],
[[0, 0, 5],
[5, 5, 5]],
[[6, 6, 6, 6]],
[[7, 7],
[7, 7]]
]
# 随机生成方块
def new_block():
shape = random.choice(block_shapes)
block = []
for i in range(len(shape)):
row = []
for j in range(len(shape[i])):
if shape[i][j] != 0:
row.append(Block(j, i, shape[i][j]))
block.append(row)
return block
# 初始化游戏
def init_game():
global play_area, current_block, next_block, score
play_area = [[0] * 10 for _ in range(20)]
current_block = new_block()
next_block = new_block()
score = 0
# 绘制游戏区域
def draw_play_area():
for i in range(len(play_area)):
for j in range(len(play_area[i])):
if play_area[i][j] != 0:
pygame.draw.rect(screen, colors[play_area[i][j]], (play_x + j * block_size, play_y + i * block_size, block_size, block_size), 0)
# 绘制方块
def draw_block(block, x, y):
for i in range(len(block)):
for j in range(len(block[i])):
if block[i][j] != 0:
pygame.draw.rect(screen, colors[block[i][j]], (x + j * block_size, y + i * block_size, block_size, block_size), 0)
# 绘制下一个方块
def draw_next_block():
pygame.draw.rect(screen, WHITE, (play_x + play_width + 20, play_y, block_size * 4, block_size * 4), 0)
draw_block(next_block, play_x + play_width + 30, play_y + 10)
# 绘制得分
def draw_score():
score_text = font.render('Score: ' + str(score), True, WHITE)
screen.blit(score_text, (play_x + play_width + 20, play_y + 100))
# 判断方块是否可以移动
def can_move(block, x, y):
for i in range(len(block)):
for j in range(len(block[i])):
if block[i][j] != 0:
if x + j < 0 or x + j >= 10 or y + i >= 20 or play_area[y + i][x + j] != 0:
return False
return True
# 将方块加入游戏区域
def add_block_to_play_area(block, x, y):
for i in range(len(block)):
for j in range(len(block[i])):
if block[i][j] != 0:
play_area[y + i][x + j] = block[i][j]
# 判断是否有满行
def check_full_lines():
lines = []
for i in range(len(play_area)):
if 0 not in play_area[i]:
lines.append(i)
return lines
# 删除满行
def remove_full_lines(lines):
global score
for line in lines:
play_area.pop(line)
play_area.insert(0, [0] * 10)
score += 10 * len(lines)
# 主循环
def main_loop():
global current_block, next_block, score
clock = pygame.time.Clock()
fall_time = 0
fall_speed = 0.5
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if can_move(current_block, current_block_x - 1, current_block_y):
current_block_x -= 1
if event.key == pygame.K_RIGHT:
if can_move(current_block, current_block_x + 1, current_block_y):
current_block_x += 1
if event.key == pygame.K_UP:
rotated_block = list(zip(*current_block[::-1]))
if can_move(rotated_block, current_block_x, current_block_y):
current_block = rotated_block
if event.key == pygame.K_DOWN:
if can_move(current_block, current_block_x, current_block_y + 1):
current_block_y += 1
fall_time += clock.get_rawtime()
clock.tick()
if fall_time / 1000 >= fall_speed:
if can_move(current_block, current_block_x, current_block_y + 1):
current_block_y += 1
else:
add_block_to_play_area(current_block, current_block_x, current_block_y)
lines = check_full_lines()
if lines:
remove_full_lines(lines)
current_block = next_block
next_block = new_block()
current_block_x = 3
current_block_y = 0
if not can_move(current_block, current_block_x, current_block_y):
game_over = True
fall_time = 0
screen.fill(BLACK)
draw_play_area()
draw_block(current_block, play_x + current_block_x * block_size, play_y + current_block_y * block_size)
draw_next_block()
draw_score()
pygame.display.flip()
pygame.quit()
# 开始游戏
colors = [BLACK, RED, GREEN, BLUE, WHITE]
init_game()
current_block_x = 3
current_block_y = 0
main_loop()
```
这是一个简单的俄罗斯方块游戏,可以在 Pygame 中运行。