另外能否说得明白一点,初学者不太明白……
现在的问题:
(解决,忘写global deli了) 1 出错 global deli 没有定义 (59行)
其他的什么显示问题还是搞不明白啊……
有谁能告知pygame的用法啊- -
这样改对么?
代码: 全选
from time import sleep
from random import randint
import sys
import os
from _thread import *
from msvcrt import getwch
#import pygame
#from pygame.locals import *
def ooo(): #初始化用函数
global gameover
global dosp
global score
global reli
# global bkg
while 1: #dosp大于0时,转换为整型,然后继续
dosp = input('Inupt the speed, which above 0:')
dosp = int(dosp)
if dosp > 0:
break
gameover = 0
# bkg = [200,201,202,203,204,205,206,207,208,209] #背景方块,默认的是不可见的,用于判定是否到达底端
score = 0
reli = {'ctt':'' , 'ipp': 0 , 'bkg':[200,201,202,203,204,205,206,207,208,209] , 'clr' : [] , 'ckt' : 0}
def ccttc(rst = randint(0,6)): #将所获得的随机数转换为方块类型并记录
global reli
ccttcl = {'0':'s1','1':'z1','2':'l1','3':'j1','4':'i1','5':'o','6':'t1'}
reli['ctt'] = ccttcl[str(rst)]
def ippc(): #初始化控制中方块重心(即基准点)
global reli
reli['ipp'] = 34
def cttp(): #由控制中方块重心的记录描绘出方块形状
global reli
reli['clr'] = []
if reli['ctt'] == 's1':
reli['clr'].append(ipp)
reli['clr'].append(reli['ipp'] - 10)
reli['clr'].append(reli['ipp'] - 1)
reli['clr'].append(reli['ipp'] - 9)
reli['ckt'] = 2
elif reli['ctt'] == 's2':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] + 10)
reli['clr'].append(reli['ipp'] - 1)
reli['clr'].append(reli['ipp'] - 11)
reli['ckt'] = 2
elif reli['ctt'] == 'z1':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 10)
reli['clr'].append(reli['ipp'] + 1)
reli['clr'].append(reli['ipp'] - 11)
reli['ckt'] = 2
elif reli['ctt'] == 'z2':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 10)
reli['clr'].append(reli['ipp'] - 1)
reli['clr'].append(reli['ipp'] + 9)
reli['ckt'] = 2
elif reli['ctt'] == 'l1':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 1)
reli['clr'].append(reli['ipp'] + 1)
reli['clr'].append(reli['ipp'] - 11)
reli['ckt'] = 4
elif reli['ctt'] == 'l2':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 10)
reli['clr'].append(reli['ipp'] + 10)
reli['clr'].append(reli['ipp'] - 9)
reli['ckt'] = 4
elif reli['ctt'] == 'l3':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 1)
reli['clr'].append(reli['ipp'] + 1)
reli['clr'].append(reli['ipp'] + 11)
reli['ckt'] = 4
elif reli['ctt'] == 'l4':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 10)
reli['clr'].append(reli['ipp'] + 10)
reli['clr'].append(reli['ipp'] + 9)
reli['ckt'] = 4
elif reli['ctt'] == 'j1':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 1)
reli['clr'].append(reli['ipp'] + 1)
reli['clr'].append(reli['ipp'] - 9)
reli['ckt'] = 4
elif reli['ctt'] == 'j2':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 10)
reli['clr'].append(reli['ipp'] + 10)
reli['clr'].append(reli['ipp'] + 11)
reli['ckt'] = 4
elif reli['ctt'] == 'j3':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 1)
reli['clr'].append(reli['ipp'] + 1)
reli['clr'].append(reli['ipp'] + 9)
reli['ckt'] = 4
elif reli['ctt'] == 'j4':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 10)
reli['clr'].append(reli['ipp'] + 10)
reli['clr'].append(reli['ipp'] - 11)
reli['ckt'] = 4
elif reli['ctt'] == 'i1':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 1)
reli['clr'].append(reli['ipp'] + 1)
reli['clr'].append(reli['ipp'] + 2)
reli['ckt'] = 2
elif reli['ctt'] == 'i2':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 10)
reli['clr'].append(reli['ipp'] + 10)
reli['clr'].append(reli['ipp'] + 20)
reli['ckt'] = 2
elif reli['ctt'] == 'o':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] + 1)
reli['clr'].append(reli['ipp'] - 9)
reli['clr'].append(reli['ipp'] - 10)
reli['ckt'] = 1
elif reli['ctt'] == 't1':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 1)
reli['clr'].append(reli['ipp'] + 1)
reli['clr'].append(reli['ipp'] - 10)
reli['ckt'] = 4
elif reli['ctt'] == 't2':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 10)
reli['clr'].append(reli['ipp'] + 10)
reli['clr'].append(reli['ipp'] + 1)
reli['ckt'] = 4
elif reli['ctt'] == 't3':
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 1)
reli['clr'].append(reli['ipp'] + 1)
reli['clr'].append(reli['ipp'] + 10)
reli['ckt'] = 4
else:
reli['clr'].append(reli['ipp'])
reli['clr'].append(reli['ipp'] - 10)
reli['clr'].append(reli['ipp'] + 10)
reli['clr'].append(reli['ipp'] - 1)
reli['ckt'] = 4
#旧显示部分
# os.system('cls')
# ptlist = deli['clr'] + deli['bkg']
# print ('━━━━━━━━━━━━')
# for ptn in range(0,200):
# if ptn % 10 == 0:
# print ('┃',end = '')
# if ptn in ptlist:
# if ptn % 10 == 9:
# print ('■┃')
# else:
# print ('■',end = '')
# else:
# if ptn % 10 == 9:
# print (' ┃')
# else:
# print (' ',end = '')
# print ('━━━━━━━━━━━━')
#旧显示部分完
#计划用新模块代替
mylock = allocate_lock() #线程锁 避免显示出错
def pd(): #判定有没有输,有没有到达底端,到达低端的话就消除
global gameover
global score
global dosp
for pnum in range(40,200,10):
pnco = 0
for pnum2 in range(pnum,pnum + 10):
if pnum2 in reli['bkg']:
pnco += 1
if pnco == 10:
reli['bkg'].remove(pnum)
reli['bkg'].remove(pnum + 1)
reli['bkg'].remove(pnum + 2)
reli['bkg'].remove(pnum + 3)
reli['bkg'].remove(pnum + 4)
reli['bkg'].remove(pnum + 5)
reli['bkg'].remove(pnum + 6)
reli['bkg'].remove(pnum + 7)
reli['bkg'].remove(pnum + 8)
reli['bkg'].remove(pnum + 9)
score += (dosp * 50)
a = 0
while a != len(reli['bkg']): #把reli['bkg']列表中小于PNUM的数加上10
if reli['bkg'][(a)] < pnum:
reli['bkg'][(a)] += 10
a += 1
for anum in range(0,40):
if anum in reli['bkg']:
gameover = 1
def downmove(): #下落
global reli
dmmco = 0
for eachNum in reli['clr']:
if (eachNum + 10) in reli['bkg']:
if not dmmco == 1:
sleep(1 / (dosp))
reli['bkg'].append(reli['clr'][0])
reli['bkg'].append(reli['clr'][1])
reli['bkg'].append(reli['clr'][2])
reli['bkg'].append(reli['clr'][3])
pd()
ccttc()
ippc()
dmmco = 1
if dmmco == 0:
reli['ipp'] = reli['ipp'] + 10
cttp()
def gmi(): #定时下落(定时使用downmove函数)
global dosp
global gameover
while gameover == 0:
sleep(1 / (dosp))
mylock.acquire()
downmove()
mylock.release()
#keyin = pygame.key.get_pressed()
def inp(): #键盘输入判定
global reli
global gameover
while gameover == 0:
if keyin[K_UP]: #输入了上键的话
mylock.acquire()
if not deli['ctt'] == 'o': #除方块O外转换方向
deli['ctt'][-1] = str((int(deli['ctt'][-1]) + 1) % deli[ckt])
cttp()
mylock.release()
elif keyin[K_DOWN]: #下键
mylock.acquire()
downmove()
mylock.release()
elif keyin[K_LEFT]: #左键
acou = 0
mylock.acquire()
for aanum in range(0,200,10): #最左部无效
if not aanum in reli['clr']:
acou += 1
if acou == 20:
kkkkkk = 0
for eachNum in reli['clr']: #防止撞车
if (eachNum - 1) in reli['bkg']:
kkkkkk = 1
if kkkkkk == 0:
reli['ipp'] = reli['ipp'] - 1
cttp()
mylock.release()
else:
if keyin[K_RIGHT]: #右键
dcou = 0
mylock.acquire()
for ddnum in range(9,209,10): #最右部无效
if not ddnum in reli['clr']:
dcou += 1
if dcou == 20:
dddddd = 0
for eachNum in reli['clr']: #防止撞车
if (eachNum + 1) in reli['bkg']:
dddddd = 1
if dddddd == 0:
reli['ipp'] = reli['ipp'] + 1
cttp()
mylock.release()
def main():
ooo()
ippc()
ccttc()
cttp()
start_new_thread(gmi,()) #开始新线程
start_new_thread(inp,()) #开始新线程
while gameover == 0:
pass
sleep(3)
print('Game Over')
print('Score:')
print(score)
input('Press Enter Key to Quit the Game')
sys.exit()
if __name__ == '__main__':
main()
#需改进的地方:显示,输入判定,消除判定