俄罗斯方块大家应该不陌生吧!让我们一起学习俄罗斯方块的代码吧。
说错了,是看些俄罗斯方块的代码,大佬们应该早知道了,不说了,上源码:
#俄罗斯方块
# -*- coding: utf-8 -*-
import pygame, random, time
from pygame.locals import *
pygame.init()
#pygame.key.set_repeat(1, 100) #重复输入模式
#窗口
screen = pygame.display.set_mode((660,520), 0, 32)
#静态界面(背景、色块等)
interface = pygame.image.load(r'C:\Users\Administrator\Desktop\interface.jpg').convert()
gameover = pygame.image.load(r'C:\Users\Administrator\Desktop\GAME_OVER.jpg').convert()
#方块名称、颜色、初始位置
squares_dict = {0:'z_left', 1:'z_right', 2:'long', 3:'cross', 4:'rect', 5:'l_left', 6:'l_right'}
'''color_dict用于实例化的时候生成颜色标记'''
color_dict = {'z_left':1, 'z_right':2, 'long':3, 'cross':4, 'rect':5, 'l_left':6, 'l_right':7}
'''color_sheet用于表示颜色标记对应的颜色RGB'''
color_sheet = {1:(231,63,136), 2:(192,219,64), 3:(168,201,206), 4:(143,111,161), 5:(254,252,66), 6:(59,140,170), 7:(249,159,15)}
init_pos_dict = {'z_left':[(4,1), (5,1), (5,2), (6,2)],
'z_right':[(7,1), (6,1), (6,2), (5,2)],
'long':[(4,2), (5,2), (6,2), (7,2)],
'cross':[(5,1), (6,1), (7,1), (6,2)],
'rect':[(5,1), (6,1), (5,2), (6,2)],
'l_left':[(4,1), (5,1), (6,1), (6,2)],
'l_right':[(7,1), (6,1), (5,1), (5,2)]}
#碰撞检测系统(装饰器,对squares类内的方法进行装饰)
def pp_test(cls):
class new_class:
def __init__(self, name, color, now_pos, flag, stop_flag):
self.wrapper = cls(name, color, now_pos, flag, stop_flag)
self.name = self.wrapper.name
self.color = self.wrapper.color
self.now_pos = self.wrapper.now_pos
self.flag = self.wrapper.flag
self.stop_flag = self.wrapper.stop_flag
def TURN(self):
temp = self.wrapper.now_pos
temp1 = self.wrapper.flag
self.wrapper.TURN()
for i in self.wrapper.now_pos:
if area[i[1]][i[0]] == 1:
self.wrapper.now_pos = temp
self.wrapper.flag = temp1
self.now_pos = self.wrapper.now_pos
self.flag = self.wrapper.flag
def LEFT(self):
temp = self.wrapper.now_pos
self.wrapper.LEFT()
for i in self.wrapper.now_pos:
if area[i[1]][i[0]] == 1:
self.wrapper.now_pos = temp
self.now_pos = self.wrapper.now_pos
def RIGHT(self):
temp = self.wrapper.now_pos
self.wrapper.RIGHT()
for i in self.wrapper.now_pos:
if area[i[1]][i[0]] == 1:
self.wrapper.now_pos = temp
self.now_pos = self.wrapper.now_pos
def DOWN(self):
temp = self.wrapper.now_pos
self.wrapper.DOWN()
for i in self.wrapper.now_pos:
if area[i[1]][i[0]] == 1:
self.wrapper.now_pos = temp
self.wrapper.stop_flag = 1
self.now_pos = self.wrapper.now_pos
self.stop_flag = self.wrapper.stop_flag
return new_class
#方块系统
@pp_test #碰撞检测装饰器
class squares:
def __init__(self, name, color, now_pos, flag, stop_flag):
self.name = name
self.color = color
self.now_pos = now_pos
self.flag = flag
self.stop_flag = stop_flag
#方块的旋转操作
def TURN(self):
global now_pos
now_pos = self.now_pos
#z_left的两种状态
if self.name == 'z_left':
if self.flag == 0:
now_pos[0] = Pos(self.now_pos[0]) + (2,-1)
now_pos[1] = Pos(self.now_pos[1]) + (1,0)
now_pos[2] = Pos(self.now_pos[2]) + (0,-1)
now_pos[3] = Pos(self.now_pos[3]) + (-1,0)