import sys
import pygame
from pygame.locals import *
import random
import time
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
WINDOW_WIDTH = 10
WINDOW_HEIGHT = 20
BLOCK_SIZE = 30
SCROLL_SPEED = 15
BASE_SCORE = 100
LEVELS = [
{'score': BASE_SCORE, 'speed': SCROLL_SPEED},
]
def create_block():
shape_colors = [
(-1, -1), (1, -1),
(-1, 1), (1, 1)
]
colors = [
BLACK,
WHITE,
WHITE,
WHITE
]
shape = random.choice(shape_colors)
color = random.choice(colors)
return shape, color
def create_window():
pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('俄罗斯方块')
return screen
def is_game_over(screen, score, level):
if score >= LEVELS[level]['score']:
return True
return False # 这里需要根据游戏逻辑判断是否结束
def main():
screen = create_window()
clock = pygame.time.Clock()
font = pygame.font.Font(None, 74)
level_index = 0
score = BASE_SCORE
while True:
if is_game_over(screen, score, level_index):
break
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_LEFT:
move_block('left')
elif event.key == K_RIGHT:
move_block('right')
elif event.key == K_UP:
rotate_block()
elif event.key == K_DOWN or event.key == K_PAGEUP:
drop_block()
screen.fill(BLACK)
draw_score(font, 100, (WINDOW_WIDTH//2 - 140, WINDOW_HEIGHT - 50), score,
size=30)
draw_level(screen, WINDOW_HEIGHT - 70, level_index + 1, font, 25, size=30)
pygame.display.flip()
clock.tick(60)
def move_block(direction):
global block_x, block_y, block_shape, block_color
if direction == 'left':
block_x -= BLOCK_SIZE
if block_x < 0:
block_x = 0
elif direction == 'right':
block_x += BLOCK_SIZE
if block_x > WINDOW_WIDTH - BLOCK_SIZE:
block_x = WINDOW_WIDTH - BLOCK_SIZE
if (block_x, block_y) in occupied_blocks:
return False # 不能移动
else:
for i in range(len(block_shape)):
if block_shape[i]:
x = block_x + BLOCK_SIZE * i
y = block_y
occupied_blocks.add((x, y))
block_x_change += 1
def rotate_block():
global block_shape, block_color
new_shape = list(block_shape)
new_shape.append(new_shape[0])
del new_shape[-1]
old_shape = block_shape
block_shape = new_shape
for i in range(len(old_shape)):
if old_shape[i]:
x = block_x + BLOCK_SIZE * i
y = block_y
occupied_blocks.remove((x, y))
for i in range(len(block_shape)):
if block_shape[i]:
x = block_x + BLOCK_SIZE * i
y = block_y
occupied_blocks.add((x, y))
def drop_block():
global block_y
while True:
block_y += 1
if block_y > WINDOW_HEIGHT - BLOCK_SIZE:
block_y = WINDOW_HEIGHT - BLOCK_SIZE
break
hasCollision = False
for i in range(len(block_shape)):
if block_shape[i]:
x = block_x + BLOCK_SIZE * i
y = block_y
if (x, y) in occupied_blocks:
hasCollision = True
break
if hasCollision:
for i in range(len(block_shape)):
if block_shape[i]:
x = block_x + BLOCK_SIZE * i
y = block_y
occupied_blocks.add((x, y))
score += 10
level_index += 1
if level_index < len(LEVELS):
time.sleep(1000) # 等待下一个级别
else:
break
return
block_y = WINDOW_HEIGHT - BLOCK_SIZE
block_x = (WINDOW_WIDTH // 2) - (BLOCK_SIZE * (len(block_shape) // 2))
block_y = WINDOW_HEIGHT - BLOCK_SIZE
occupied_blocks = set()
block_shape, block_color = create_block()
def draw_score(font, size, x_pos, y_pos):
text = font.render(f"得分:{score}", True, WHITE)
text_rect = text.get_rect(x=x_pos, y=y_pos)
screen.blit(text, text_rect)
def draw_level(font, size, x_pos, y_pos):
level_text = str(level_index + 1)
text = font.render(level_text, True, WHITE)
text_rect = text.get_rect(x=x_pos, y=y_pos)
screen.blit(text, text_rect)
pygame.init()
screen = create_window()
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
break
screen.fill(BLACK)
draw_score(font, 100, (WINDOW_WIDTH//2 - 140), WINDOW_HEIGHT - 50)
draw_level(font, 25, (WINDOW_WIDTH//2 - 85), WINDOW_HEIGHT - 70)
pygame.display.flip()
clock.tick(60)
pygame.quit()