下面是一个使用 Python 语言编写的简单版俄罗斯方块游戏代码:
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置方块大小和形状
block_size = 20
shape = [(0, 0, 1, 1), (1, 0, 1, 1), (2, 0, 1, 1), (3, 0, 1, 1)]
# 设置方块颜色
color = [(255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255)]
# 定义方块类
class Block:
def __init__(self, x, y):
self.x = x
self.y = y
self.shape = random.choice(shape)
self.color = random.choice(color)
self.score = 0
def draw(self):
for i, j in enumerate(self.shape):
pygame.draw.rect(screen, self.color[i], [self.x + j[0] * block_size, self.y + j[1] * block_size, block_size, block_size])
def move_left(self):
if self.x > 0:
self.x -= block_size
def move_right(self):
if self.x < screen_width - block_size * len(self.shape[0]):
self.x += block_size
def move_down(self):
if self.y < screen_height - block_size:
self.y += block_size
def fall(self):
while self.y < screen_height - block_size:
self.y += block_size
if self.is_out():
return True
pygame.display.flip()
return False
def is_out(self):
for i, j in enumerate(self.shape):
if self.x + j[0] * block_size < 0 or self.x + j[0] * block_size >= screen_width or self.y + j[1] * block_size < 0 or self.y + j[1] * block_size >= screen_height:
return True
for k in range(len(shape)):
if shape[k][0] == j[0] and shape[k][1] == j[1] and shape[k][2] == j[2] and shape[k][3] == j[3] and (i != k and (self.x + k * block_size < 0 or self.x + k * block_size >= screen_width or self.y + k * block_size < 0 or self.y + k * block_size >= screen_height)):
return True
return False
def check_score(self):
for i in range(len(shape)):
if shape[i][0] == self.shape[i][0] and shape[i][1] == self.shape[i][1] and shape[i][2] == self.shape[i][2] and shape[i][3] == self.shape[i][3]:
if self.x + i * block_size == screen_width - block_size * len(shape[0]) or self.y == screen_height - block_size:
return True
return False
苏公网安备 32132202001088号
| Copyright 北极熊 版权所有