我正在从一些开源网站编写这个2048程序。 然而,经过一段时间的比赛,“32”和“64”这样的数字就消失了!我似乎无法找到发生这种情况的原因,我已经将我的代码与源代码进行了比较。他们看起来一样:(
有谁知道这个问题?提前谢谢!
用于组合数字:
def combine(L):
global score
ans = [0,0,0,0]
num = []
for i in L:
if i != 0:
num.append(i)
length = len(num)
if length == 4:
if num[0] == num[1]:
ans[0] = num[0]+num[1]
score += ans[0]
if num[2] == num[3]:
ans[1] = num[2] + num[3]
score += ans[1]
else:
ans[1] = num[2]
ans[2] = num[3]
elif num[1] == num[2]:
ans[0] = ans[0]
ans[1] = num[1] + num[2]
ans[2] = num[3]
score += ans[1]
elif num[2] == num[3]:
ans[0] = num[0]
ans[1] = num[1]
ans[2] = num[2] + num[3]
score += ans[2]
else:
for i in range(length):
ans[i] = num[i]
elif length == 3:
if num[0] == num[1]:
ans[0] = num[0] + num[1]
ans[1] = num[2]
score += ans[0]
elif num[1] == num[2]:
ans[0] = num[0]
ans[1] = num[1] + num[2]
score += ans[1]
else:
for i in range(length):
ans[i] = num[i]
elif length == 2:
if num[0] == num[1]:
ans[0] = num[0] + num[1]
score += ans[0]
else:
for i in range(length):
ans[i] = num[i]
elif length == 1:
ans[0] = num[0]
else:
pass
return ans
箭头键控制:
def left():
#global score
for i in range(4):
temp = combine(board[i])
for j in range(4):
board[i][j] = temp[j]
def up():
for i in range(4):
to_comb = []
for j in range(4):
to_comb.append(board[j][i])
temp = combine(to_comb)
for k in range(4):
board[k][i] = temp[k]
def right():
for i in range(4):
temp = combine(board[i][::-1])
for j in range(4):
board[i][3-j] = temp[j]
def down():
for i in range(4):
to_comb =[]
for j in range(4):
to_comb.append(board[3-j][i])
temp = combine(to_comb)
for k in range(4):
board[3-k][i] = temp[k]
我的整个代码是:
import random
from sys import exit
from copy import deepcopy
import pygame
from pygame.locals import *
board = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
pygame.init()
box_size = 100
box_gap = 10
top_of_screen = 200
bottom_of_screen = 60
left_of_screen = 40
screen_width = box_size*4 + box_gap *5 +left_of_screen*2
screen_height = box_size*4 + box_gap*5 + top_of_screen+ left_of_screen+bottom_of_screen
screen = pygame.display.set_mode((screen_width, screen_height), 0, 32)
pygame.display.set_caption("2048")
background = pygame.image.load("background.png").convert()
score = 0
OLDLACE = pygame.color.THECOLORS["oldlace"]
IVORY = pygame.color.THECOLORS["ivory3"]
BLACK = pygame.color.THECOLORS["black"]
RED = pygame.color.THECOLORS["red"]
RED2 = pygame.color.THECOLORS["red2"]
DARKGOLD = pygame.color.THECOLORS["darkgoldenrod1"]
GOLD = pygame.color.THECOLORS["gold"]
GRAY = pygame.color.THECOLORS["gray41"]
CHOCOLATE = pygame.color.THECOLORS["chocolate"]
CHOCOLATE1 = pygame.color.THECOLORS["chocolate1"]
CORAL = pygame.color.THECOLORS["coral"]
CORAL2 = pygame.color.THECOLORS["coral2"]
ORANGED = pygame.color.THECOLORS["orangered"]
ORANGED2 = pygame.color.THECOLORS["orangered2"]
DARKORANGE = pygame.color.THECOLORS["darkorange"]
DARKORANGE2 = pygame.color.THECOLORS["darkorange2"]
FORESTGREEN = pygame.color.THECOLORS['forestgreen']
class Box:
def __init__(self, topleft, text, color):
self.topleft = topleft
self.text = text
self.color = color
def render(self, surface):
x,y=self.topleft
#rect(Surface, color, Rect, width=0) -> Rect
#where Rect is (x,y coordinate for topleft pixel, width, length)
pygame.draw.rect(surface, self.color, (x,y,box_size,box_size))
text_height = int(box_size * 0.35)
font_obj = pygame.font.Font("FreeSansBold.ttf", text_height)
#render(): draw text on a new surface
#render(text, antialias, color, background=None) -> Surface
text_surface = font_obj.render(self.text, True, BLACK)
text_rect = text_surface.get_rect()
text_rect.center = (x + box_size/2, y+box_size/2)
surface.blit(text_surface, text_rect)
def draw_box():
global board
# colors = {0:GRAY, 2:(239, 233, 182), 4:(239, 228, 151), 8:(243, 212, 77), 16:(239, 206, 25),
# 32:(242, 157, 12), 64:(214, 214, 42), 128:(239, 207, 108), 256:(239, 207, 99),
# 512:(239, 203, 82), 1024:(239, 199, 57), 2048:(239, 195, 41), 4096:(255, 60, 57)}
colors = {0:(192, 192, 192), 2:(176, 224, 230), 4:(127, 255, 212), 8:(135, 206, 235), 16:(64, 224, 208),
32:(0, 255, 255), 64:(0, 201, 87), 128:(50, 205, 50), 256:(34, 139, 34),
512:(0, 255, 127), 1024:(61, 145, 64), 2048:(48, 128, 20), 4096:(65, 105, 255),
8192:(8, 46, 84), 16384:(11, 23, 70), 32768:(25, 25, 112), 65536:(0, 0, 255)}
x,y = left_of_screen, top_of_screen
size = box_size*4 + box_gap *5
pygame.draw.rect(screen, BLACK, (x,y,size,size))
x,y = x + box_gap, y + box_gap
for i in range(4):
for j in range(4):
idx = board[i][j]
if idx == 0:
text = ""
else:
text = str(idx)
if idx> 65536: idx = 65536
color = colors[idx]
box = Box((x,y),text,color)
box.render(screen)
x += box_size + box_gap
x = left_of_screen + box_gap
y += box_size + box_gap
def set_random_number():
pool = []
# These 2 for loops will filter out all indexes of boxes that is 0
# Choose a random 0-box and save the index, then remove
# Decide 2 or 4, then save it in board, at the index of that particular 0-box
for i in range(4):
for j in range(4):
if board[i][j] == 0:
pool.append((i,j))
m = random.choice(pool)
pool.remove(m)
value = random.uniform(0,1)
if value < 0.1:
value = 4
else:
value = 2
board[m[0]][m[1]] = value
def init_board():
# At the start of the game, two random boxes will be selected to host values
for i in range(2):
set_random_number()
def combine(L):
global score
ans = [0,0,0,0]
num = []
for i in L:
if i != 0:
num.append(i)
length = len(num)
if length == 4:
if num[0] == num[1]:
ans[0] = num[0]+num[1]
score += ans[0]
if num[2] == num[3]:
ans[1] = num[2] + num[3]
score += ans[1]
else:
ans[1] = num[2]
ans[2] = num[3]
elif num[1] == num[2]:
ans[0] = ans[0]
ans[1] = num[1] + num[2]
ans[2] = num[3]
score += ans[1]
elif num[2] == num[3]:
ans[0] = num[0]
ans[1] = num[1]
ans[2] = num[2] + num[3]
score += ans[2]
else:
for i in range(length):
ans[i] = num[i]
elif length == 3:
if num[0] == num[1]:
ans[0] = num[0] + num[1]
ans[1] = num[2]
score += ans[0]
elif num[1] == num[2]:
ans[0] = num[0]
ans[1] = num[1] + num[2]
score += ans[1]
else:
for i in range(length):
ans[i] = num[i]
elif length == 2:
if num[0] == num[1]:
ans[0] = num[0] + num[1]
score += ans[0]
else:
for i in range(length):
ans[i] = num[i]
elif length == 1:
ans[0] = num[0]
else:
pass
return ans
def left():
#global score
for i in range(4):
temp = combine(board[i])
for j in range(4):
board[i][j] = temp[j]
def up():
for i in range(4):
to_comb = []
for j in range(4):
to_comb.append(board[j][i])
temp = combine(to_comb)
for k in range(4):
board[k][i] = temp[k]
def right():
for i in range(4):
temp = combine(board[i][::-1])
for j in range(4):
board[i][3-j] = temp[j]
def down():
for i in range(4):
to_comb =[]
for j in range(4):
to_comb.append(board[3-j][i])
temp = combine(to_comb)
for k in range(4):
board[3-k][i] = temp[k]
def write(msg="POWERED BY PYGAME", color=BLACK, height = 14):
myfont = pygame.font.Font("FreeSansBold.ttf", height)
mytext = myfont.render(msg, True, color)
mytext = mytext.convert_alpha()
return mytext
def is_over():
for i in range(4):
for j in range(4):
if board[i][j] == 0:
return False
for i in range(4):
for j in range(3):
if board[i][j] == board[i][j+1]:
return False
for i in range(3):
for j in range(4):
if board[i][j] == board[i+1][j]:
return False
return True
def read_best():
try:
f = open('best.rec','r')
best = int(f.read())
f.close()
except:
best = 0
return best
def write_best(best):
try:
f = open('best.rec','w')
f.write(str(best))
f.close()
except IOError:
pass
def main():
global score
screen.blit(background, (0,0))
init_board()
newboard = deepcopy(board)
gameover = is_over()
draw_box()
screen.blit(write("2048",height = 40, color = GOLD),(left_of_screen, left_of_screen//2))
screen.blit(write("SCORE", height = 14, color = FORESTGREEN),(left_of_screen, left_of_screen//2))
rect1 = pygame.draw.rect(screen, FORESTGREEN, (left_of_screen+100, left_of_screen//2 +30, 60, 20))
text1 = write(str(score),height = 14, color=GOLD)
text1_rect = text1.get_rect()
text1_rect.center = (left_of_screen+100+30, left_of_screen//2+40)
screen.blit(text1, text1_rect)
screen.blit(write("BEST", height = 14, color = FORESTGREEN),(left_of_screen+175, left_of_screen//2+5))
rect2 = pygame.draw.rect(screen, FORESTGREEN,(left_of_screen+165,left_of_screen//2+30,60,20))
best = read_best()
if best < score:
best = score
text2 = write(str(best), height=14, color=GOLD)
text2_rect = text2.get_rect()
text2_rect.center = (left_of_screen+165+30, left_of_screen//2+40)
screen.blit(text2,text2_rect)
screen.blit(write("Use LEFT, RIGHT, UP, DOWN", height =16, color=GRAY),(left_of_screen, screen_height-bottom_of_screen))
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type ==KEYUP and event.key == K_ESCAPE):
write_best(best)
pygame.quit()
exit()
elif not gameover:
if event.type == KEYUP and event.key == K_UP:
up()
elif event.type == KEYUP and event.key == K_DOWN:
down()
elif event.type == KEYUP and event.key == K_RIGHT:
right()
elif event.type == KEYUP and event.key == K_LEFT:
left()
if newboard != board:
set_random_number()
newboard = deepcopy(board)
draw_box()
gameover = is_over()
rect1 = pygame.draw.rect(screen, FORESTGREEN, (left_of_screen+100, left_of_screen//2+30,60,20))
text1 = write(str(score),height=14,color=GOLD)
text_rect = text1.get_rect()
text_rect.center = (left_of_screen+100+30,left_of_screen//2 + 40)
screen.blit(text1, text_rect)
rect2 = pygame.draw.rect(screen, FORESTGREEN, (left_of_screen+165, left_of_screen//2+30,60,20))
if best<score:
best = score
text2 = write(str(best),height=14,color=GOLD)
text2_rect = text2.get_rect()
text2_rect.center = (left_of_screen+165+30, left_of_screen//2+40)
screen.blit(text2, text2_rect)
else:
write_best(best)
screen.blit(write("GAME OVER", height = 50, color = FORESTGREEN), (left_of_screen, screen_height//2))
pygame.display.update()
if __name__ == "__main__":
main()
可以找到源代码here。