用户:
射手座的丁丁然查看:0 回复:0 评论:0 创建时间:2021-01-28T15:49:08
【作品展示】

【作品介绍】
未完成品,勿喷。
【作品源代码】
import pygame,time,sys
from pygame.locals import *
import threading
import random
SIZE = 4
LENGTH = 130
SCORE_HEIGHT = 130
DEFAULT=(205, 193, 180)
C_FONT=(120,111,102)
C_2=(237,224,200)
C_4 = (242, 177, 121)
C_8 = (245, 149, 99)
C_16 = (246, 124, 95)
C_32 = (246, 94, 59)
C_喵 = (237, 207, 144)
C_128 = (237, 204, 98)
C_256 = (237, 200, 80)
C_512 = (237, 204, 80)
C_1024 = (237, 197, 63)
C_2048 = (225, 187, 0)
class Map:
def __init__(self,size):
self.SIZE = size
self.map = [[0 for i in range(size)]for i in range(size)]
self.score = 0
self.is_move = 0
self.add()
self.add()
def add(self):
while True:
pos = random.randint(0, self.SIZE*self.SIZE-1)
flag = self.map[pos//self.SIZE]
if flag == 0:
num = random.randint(0,3)
n=2
if num == 0:
n=4
self.map[pos//self.SIZE][pos%self.SIZE]=n
self.score+=n
break
def display(map,screen):
block_font=pygame.font.Font(None,86)
score_font = pygame.font.Font(None, 86)
screen.fill(DEFAULT)
for i in range(map.SIZE):
for j in range(map.SIZE):
block = pygame.Surface ((LENGTH,LENGTH))
block.fill(get_color(map.map[i][j]))
font_surf = block_font.render(str(map.map[i][j]),True,C_FONT)
font_rect=font_surf.get_rect()
font_rect.center=(j*LENGTH,i*LENGTH)
if map.map[i][j]!=0:
screen.blit(font_surf,font_rect)
score_surf=score_font.render('score:'+str(map.score),True,C_FONT)
score_rect=score_surf.get_rect()
score_rect.center=(LENGTH*SIZE/2,LENGTH*i+LENGTH/2)
screen.blit(font_surf,font_rect)
pygame.display.update()
def get_color(n):
n_t=0
for i in range(1,12):
if n >> i == 1:
n_t=i
color = [DEFAULT,C_2,C_4,C_8,C_16,C_32,C_喵,C_128,C_256,C_512,C_1024,C_2048,C_FONT]
return color[n_t]
def main():
pygame.init()
screen = pygame.display.set_mode((LENGTH*SIZE,LENGTH*SIZE+SCORE_HEIGHT))
pygame.display.set_caption("2048")
clock = pygame.time.Clock()
map = Map(SIZE)
display(map,screen)
while True:
clock.tick(5)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn