简介
俄罗斯方块是一款风靡全球的电视游戏机和掌上游戏机游戏,它由俄罗斯人阿列克谢·帕基特诺夫发明,故得此名。俄罗斯方块的基本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分。由于上手简单、老少皆宜,从而家喻户晓,风靡世界。
相关文件
小伙伴们可以关注小编的Python源码、问题解答&学习交流群:733089476
有很多的资源可以白嫖的哈,需要源码的小伙伴可以在+君羊领取
游戏规则
由小方块组成的不同形状的板块陆续从屏幕上方落下来,玩家通过调整板块的位置和方向,使它们在屏幕底部拼出完整的一条或几条。这些完整的横条会随即消失,给新落下来的板块腾出空间,与此同时,玩家得到分数奖励。没有被消除掉的方块不断堆积起来,一旦堆到屏幕顶端,玩家便告输,游戏结束。
环境搭建
Python版本:3.7.8
安装Python并添加到环境变量,pip安装需要的相关模块即可。
效果展示
开始界面
游戏界面
结束界面
首先
导入模块
import sys
import pygame
import random
import time
from settings import *
from piece import Piece
from gamewall import GameWall
from gamedisplay import GameDisplay
from gamestate import GameState
from gameresource import GameResource
主函数代码
def main():
#初始化pygame。启用Pygame必不可少的一步,在程序开始阶段执行。
pygame.init()
#创建屏幕对象
screen = pygame.display.set_mode((1200, 900) ) #分辨率是1200*900
pygame.display.set_caption("俄罗斯方块 需要源码+Q群:733089476") #窗口标题
pygame.key.set_repeat(100, 100) # 一直按下某个键,每过100毫秒就引发一个KEYDOWN事件
#屏幕背景色
bg_color = (230, 230, 230)
game_state = GameState(screen)
game_resource = GameResource()
game_resource.play_bg_music()
#游戏主循环
while True:
#方块触底的话
if game_state.piece and game_state.piece.is_on_bottom:
game_state.touch_bottom()
#监视键盘和鼠标事件
check_events(game_state, game_resource)
#设定屏幕背景
screen.blit(game_resource.load_bg_img(), (0, 0))
#绘制方块
if game_state.piece:
game_state.piece.paint()
#绘制游戏区域网格线和墙体
GameDisplay.draw_game_window(screen, game_state, game_resource)
#让最近绘制的屏幕可见
pygame.display.flip()
def check_events(game_state, game_resource):
'''捕捉和处理键盘按键事件'''
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
on_key_down(event, game_state, game_resource)
elif event.type == pygame.USEREVENT:
game_state.piece.move_down()
def on_key_down(event, game_state, game_resource