| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 474 人关注过本帖
标题:求助!编程2048时遇到错误,上网搜也搜不到怎么改。
只看楼主 加入收藏
小白在此
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2023-8-7
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
求助!编程2048时遇到错误,上网搜也搜不到怎么改。
程序代码:
import pygame,time,sys,threading,random
from pygame.locals import*
size=4
length=130
score_h=130
Default=(205,193,180)
CFont=(120,111,102)
C2=(238,228,218)
C4=(237,224,200)
C8=(242,177,121)
C16=(245,149,99)
C32=(246,124,95)
C64=(246,94,59)
C128=(237,207,114)
C256=(237,204,98)
C512=(237,200,80)
C1024=(237,197,63)
C2048=(225,187,0)
def get_color(n):
    n_t=0
    for i in range(1,12):
        if n >>i==1:
            n_t=i
    color=[Default,C2,C4,C8,C16,C32,C64,C128,C256,C512,C1024,C2048,CFont]
    return color[n_t]
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][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 failed(self):
        for i in self.map:
            for j in i:
                if j==0:
                    return False
        for i in range(0,self.size):
            for j in range(0,self.size):
                if (i-1 >=0 and self.map[i][j]==self.map[i-1][j])or(j-1>=0 and self.map[i][j]==self.map[i][j-1])or(j+1 <self.size and self.map[i][j]==self.map[i+1][j])or(i-1 >=0 and self.map[i][j]==self.map[i][j+1]):
                    return False
        return True
    def check(self,num):
        for i in self.map:
            for j in i:
                if j==num:
                    return True
        return False
    def movetoleft(self):
        changed=False
        for a in self.map:
            b=[]
            last=0
            for v in a:
                if v!=0:
                    if v!=last:
                        b.append(v)
                        last=v
                    else:
                        b.append(b.pop()*2)
                        last=0
            b+=[0]*(self.size-len(b))
            for i in range(0,self.size):
                if a[i]!=b[i]:
                    changed=True
                    a[:]=b
                return changed
    def change(self):
        self.map=[[self.map[i][j]for i in reversed(range(self.size))]for j in range(self.size)]
    def moveleft(self):
        if self.movetoleft():
            self.is_move=1
            self.add()
    def moveup(self):
        self.change()
        self.change()
        self.change()
        if self.movetoleft():
            self.is_move=1
            self.add()
        self.change()
    def moveright(self):
        self.change()
        self.change()
        if self.movetoleft():
            self.is_move=1
            self.add()
        self.change()
        self.change()
    def movedown(self):
        self.change()
        if self.movetoleft():
            self.is_move=1
            self.add()
        self.change()
        self.change()
        self.change()
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,CFont)
            font_rect=font_surf.get_rect()
            font_rect.center(j*length+length/2,length*i+length/2)
            screen.blit(block,(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,CFont)
    score_rect=score_surf.get_rect()
    score_rect.center=(length*size/2,length*size+score_h/2)
    screen.blit(score_surf,score_font)
    pygame.display.update()
def main():
    pygame.init()
    screen=pygame.display.set_mode((length*size,length*size+score_h))
    pygame.display.set_caption('2048')
    colck = pygame.time.Clock()
    map=Map(size)
    display(map,screen)
    while not map.failed():
        colck.tick(5)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                keys=pygame.key.get_pressed()
                map.is_move=0
                if keys[K_UP]:
                    map.moveup()
                elif keys[K_DOWN]:
                    map.movedown()
                elif keys[K_RIGHT]:
                    map.moveright()
                elif keys[K_LEFT]:
                    map.moveleft()
            elif event.type==KEYUP:
                t=threading.Thread(target=display,arms=(map.screen))
                t.setDaemon(True)
                t.start()
        if map.is_move==1:
            if map.check(2048):
                break
            time.sleep(0.01)
    result='You Failed!'
    if map.check(2048):
        result='You Win!'
    screen.fill(Default)
    map_font=pygame.font.Font(None,86)
    font_surf=map_font.render(resuult,True,CFont)
    font_rect=font_surf.get_rect()
    font_rect.center=(size*length/2,size*length/2)
    screen.blit(font_surf,font_rect)
    pygame.display.update()
if __name__ == '__main__':
    main()   

然后就一直提示TypeError: 'tuple' object is not callable
出错的地方就是加了下划线的那一段。
求大佬出手相助。
搜索更多相关主题的帖子: size Map for if pygame 
2023-08-31 12:15
yiyanxiyin
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:9
帖 子:242
专家分:1856
注 册:2023-6-29
收藏
得分:14 
rect的center是属性不是方法, 改成:
font_rect.center=(j*length+length/2,length*i+length/2)
2023-08-31 14:36
yiyanxiyin
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:9
帖 子:242
专家分:1856
注 册:2023-6-29
收藏
得分:0 
还有一个blit的第二个参数也不对,应该是rect, 修改后的完整代码如下:
程序代码:
import pygame,time,sys,threading,random
from pygame.locals import*
size=4
length=130
score_h=130
Default=(205,193,180)
CFont=(120,111,102)
C2=(238,228,218)
C4=(237,224,200)
C8=(242,177,121)
C16=(245,149,99)
C32=(246,124,95)
C64=(246,94,59)
C128=(237,207,114)
C256=(237,204,98)
C512=(237,200,80)
C1024=(237,197,63)
C2048=(225,187,0)
def get_color(n):
    n_t=0
    for i in range(1,12):
        if n >>i==1:
            n_t=i
    color=[Default,C2,C4,C8,C16,C32,C64,C128,C256,C512,C1024,C2048,CFont]
    return color[n_t]
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][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 failed(self):
        for i in self.map:
            for j in i:
                if j==0:
                    return False
        for i in range(0,self.size):
            for j in range(0,self.size):
                if (i-1 >=0 and self.map[i][j]==self.map[i-1][j])or(j-1>=0 and self.map[i][j]==self.map[i][j-1])or(j+1 <self.size and self.map[i][j]==self.map[i+1][j])or(i-1 >=0 and self.map[i][j]==self.map[i][j+1]):
                    return False
        return True
    def check(self,num):
        for i in self.map:
            for j in i:
                if j==num:
                    return True
        return False
    def movetoleft(self):
        changed=False
        for a in self.map:
            b=[]
            last=0
            for v in a:
                if v!=0:
                    if v!=last:
                        b.append(v)
                        last=v
                    else:
                        b.append(b.pop()*2)
                        last=0
            b+=[0]*(self.size-len(b))
            for i in range(0,self.size):
                if a[i]!=b[i]:
                    changed=True
                    a[:]=b
                return changed
    def change(self):
        self.map=[[self.map[i][j]for i in reversed(range(self.size))]for j in range(self.size)]
    def moveleft(self):
        if self.movetoleft():
            self.is_move=1
            self.add()
    def moveup(self):
        self.change()
        self.change()
        self.change()
        if self.movetoleft():
            self.is_move=1
            self.add()
        self.change()
    def moveright(self):
        self.change()
        self.change()
        if self.movetoleft():
            self.is_move=1
            self.add()
        self.change()
        self.change()
    def movedown(self):
        self.change()
        if self.movetoleft():
            self.is_move=1
            self.add()
        self.change()
        self.change()
        self.change()
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,CFont)
            font_rect=font_surf.get_rect()
            font_rect.center=(j*length+length/2,length*i+length/2)
            screen.blit(block,(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,CFont)
    score_rect=score_surf.get_rect()
    score_rect.center=(length*size/2,length*size+score_h/2)
    screen.blit(score_surf,score_rect)
    pygame.display.update()
def main():
    pygame.init()
    screen=pygame.display.set_mode((length*size,length*size+score_h))
    pygame.display.set_caption('2048')
    colck = pygame.time.Clock()
    map=Map(size)
    display(map,screen)
    while not map.failed():
        colck.tick(5)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                keys=pygame.key.get_pressed()
                map.is_move=0
                if keys[K_UP]:
                    map.moveup()
                elif keys[K_DOWN]:
                    map.movedown()
                elif keys[K_RIGHT]:
                    map.moveright()
                elif keys[K_LEFT]:
                    map.moveleft()
            elif event.type==KEYUP:
                t=threading.Thread(target=display,arms=(map.screen))
                t.setDaemon(True)
                t.start()
        if map.is_move==1:
            if map.check(2048):
                break
            time.sleep(0.01)
    result='You Failed!'
    if map.check(2048):
        result='You Win!'
    screen.fill(Default)
    map_font=pygame.font.Font(None,86)
    font_surf=map_font.render(resuult,True,CFont)
    font_rect=font_surf.get_rect()
    font_rect.center=(size*length/2,size*length/2)
    screen.blit(font_surf,font_rect)
    pygame.display.update()
if __name__ == '__main__':
    main() 
2023-08-31 14:42
小白在此
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2023-8-7
收藏
得分:0 
感谢大佬的帮助!
但第155行就是t=threading.Thread(target=display,arms=(map.screen))那,
又提示AttributeError: 'Map' object has no attribute 'screen',
大佬能再帮帮我吗?感激不尽!Thanks♪(・ω・)ノ
2023-09-02 13:45
快速回复:求助!编程2048时遇到错误,上网搜也搜不到怎么改。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039223 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved