python小白做的2048小游戏,本来是用wasd,之后想改进成方向键控制,不过试了很多方法都不行。
K_LEFT下面的print('ininin')是为了测试有没有成功接收到方向键,不过一直无法成功。
有没有大佬帮忙看看哪里出了什么问题(代码如下)
import random
import pygame,sys,time
from pygame.locals import *
matix=[[0 for i in range(4)]for i in range(4)]
score = 0
flag=True
def display():
print(' \033[1;31;42m 欢迎进入2048小游戏 \033[0m')
print('\r'
' ———— ———— ———— ———— \n'
'|%4s|%4s|%4s|%4s|\n'
'|%4s|%4s|%4s|%4s|\n'
'|%4s|%4s|%4s|%4s|\n'
'|%4s|%4s|%4s|%4s|\n'
' ———— ———— ———— ———— '
%(ifZero(matix[0][0]), ifZero(matix[0][1]),
ifZero(matix[0][2]), ifZero(matix[0][3]),
ifZero(matix[1][0]), ifZero(matix[1][1]),
ifZero(matix[1][2]), ifZero(matix[1][3]),
ifZero(matix[2][0]), ifZero(matix[2][1]),
ifZero(matix[2][2]), ifZero(matix[2][3]),
ifZero(matix[3][0]), ifZero(matix[3][1]),
ifZero(matix[3][2]), ifZero(matix[3][3]))
)
print(' \033[1;31;47m 得分:%s \033[0m'%(score))
def ifZero(s):
return s if s!=0 else ""
def init():
initNumFlag=0
while True:
k=2 if random.randrange(0,10)>1 else 4
s=divmod(random.randrange(0,16),4)
if matix[s[0]][s[1]]==0:
matix[s[0]][s[1]]=k
initNumFlag+=1
if initNumFlag==2:
break
display()
def moveLeft(): #向左移动
global score
for i in range(4):
for j in range(3):
for k in range(1+j,4):
if matix[i][k]>0:
if matix[i][j] == 0:
matix[i][j] = matix[i][k]
matix[i][k] = 0
elif matix[i][j] == matix[i][k]:
matix[i][j] *= 2
score += matix[i][j]
matix[i][k] = 0
break
addRandomNum()
def moveRight(): #向右移动
global score
for i in range(4):
for j in range(3,0,-1):
for k in range(j-1,-1,-1):
if matix[i][k]>0:
if matix[i][j]==0:
if k>0 and matix[i][k]==matix[i][k-1]:
matix[i][k]*=2
score+=matix[i][k] #将当前的数作为score加上
matix[i][k-1]=0
if k==2 and matix[i][k-1]==0 and matix[i][k]==matix[i][k-2]:
matix[i][k] *=2
score += matix[i][k] #将当前的数作为score加上
matix[i][k-2] = 0
matix[i][j] = matix[i][k]
matix[i][k]=0
elif matix[i][j] == matix[i][k]:
matix[i][j]*=2
score+=matix[i][j]
matix[i][k]=0
break
addRandomNum()
def moveDown(): #向下移动
global score
for i in range(4):
for j in range(3, 0, -1):
for k in range(j - 1, -1, -1):
if matix[k][i] > 0:
if matix[j][i] == 0:
matix[j][i] = matix[k][i]
matix[k][i] = 0
elif matix[j][i] == matix[k][i]:
matix[j][i] *= 2
score += matix[j][i]
matix[k][i] = 0
break
addRandomNum()
def moveUp(): #向上移动
global score
for i in range(4):
for j in range(3):
for k in range(j + 1, 4):
if matix[k][i] > 0:
if matix[j][i] == 0:
matix[j][i] = matix[k][i]
matix[k][i] = 0
elif matix[k][i] == matix[j][i]:
matix[j][i] *= 2
score += matix[j][i]
matix[k][i] = 0
break
addRandomNum()
def addRandomNum(): #移动后自动添加随机数2或者4
check = checkOver()
while check:
k=2 if random.randrange(0,10)>1 else 4
s=divmod(random.randrange(0,16),4)
if matix[s[0]][s[1]] == 0:
matix[s[0]][s[1]] = k
break
display()
def checkOver():
check = False
for i in range(4):
for j in range(3):
if matix[i][j]==0 or matix[i][j]==matix[i][j+1] or matix[j][i]==matix[j+1][i]:
check = True
return check
def main():
init()
global flag
print(' \033[1;31;46m 方向键控制 退格键退出:\033[0m')
while flag :
str = pygame.key.get_pressed()
if str[pygame.K_LEFT]:
print('ininini')
moveLeft()
if not checkOver():
print('游戏结束')
flag = False
elif str[pygame.K_DOWN]:
moveDown()
if not checkOver():
print('游戏结束')
flag = False
elif str[pygame.K_UP]:
moveUp()
if not checkOver():
print('游戏结束')
flag = False
elif str[pygame.K_RIGHT]:
moveRight()
if not checkOver():
print('游戏结束')
flag = False
elif str[pygame.K_ESCAPE]:
break
else:
continue
if __name__=='__main__':
pygame.init()
main()
pygame.quit()