微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么放置文本会这样做?

如何解决为什么放置文本会这样做?

我想制作简单的平台游戏。一切正常,但是当我尝试在此处输入文本时,发生了一些奇怪的事情。我真的不知道为什么会这样,因为我和以前的项目一样 这是打印屏幕:

enter image description here

如果你知道为什么会这样,为什么还会这样,请告诉我 这是我的代码

from pygame import *
WIN_WIDTH = 1923
WIN_HEIGHT = 1000
disPLAY = (WIN_WIDTH,WIN_HEIGHT)
DEPTH = 32
FLAGS = 0
CAMERA_SLACK = 30
pygame.init()
green = (0,255,0)
screen = pygame.display.set_mode(disPLAY,FLAGS,DEPTH)
level = [
'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP','P                                                          P','P                        PP                                P','P                                 PP                       P','P             PP                                           P','P                     PP                                   P','P                                                   P      P','P                            PP                     P      P','P                    PP                             P      P','P           PP                                      P      P','P                      PPP                          P     PP','P                                                   P     LP','PSSSSSSSSSSSSSSSSSSSSsspPPSSSSSSSSSSSSSSSSSSSSSSSSsspsspPPPPPP',]
def main():
    pygame.display.set_caption('BlaBlabla!')
    timer = pygame.time.Clock()
    up = down = left = right = running = left_dash = right_dash = dashing = False
    bg = Surface((WIN_WIDTH,WIN_HEIGHT  // 30))
    bg.convert()
    bg.fill(Color('#000000'))
    text = 'score:'
    font = pygame.font.SysFont('Consolas',22)
    bg.blit(font.render(text,True,(green)),(1,1))
    entities = pygame.sprite.Group()
    platforms = []
    killing_entities = []
    another_level = []
    blockade = []
    player = Player(767,900)
    x = y = 0
    global level
    for row in level:
        for col in row:
            if col == 'P':
                p = Platform(x,y)
                platforms.append(p)
                entities.add(p)
            if col == 'E':
                e = Block(x,y)
                platforms.append(e)
                entities.add(e)
            if col == 'S':
                s = Spike(x,y)
                killing_entities.append(s)
                entities.add(s)
            if col == 'L':
                l = Another_Level(x,y)
                another_level.append(l)
                entities.add(l)
            x += 32
        y += 32
        x = 0
    entities.add(player)
    run = True
    while run:
        timer.tick(65)
        for e in pygame.event.get():
            if e.type == QUIT:
                run = False
            if e.type == KEYDOWN and e.key == K_SPACE:
                up = True
            if e.type == KEYDOWN and e.key == K_s:
                down = True
            if e.type == KEYDOWN and e.key == K_a:
                left = True
            if e.type == KEYDOWN and e.key == K_d:
                right = True
            if e.type == KEYDOWN and e.key == K_q:
                running = True              
            if e.type == KEYUP and e.key == K_SPACE:
                up = False
            if e.type == KEYUP and e.key == K_s:
                down = False
            if e.type == KEYUP and e.key == K_d:
                right = False
            if e.type == KEYUP and e.key == K_a:
                left = False
            if e.type == KEYUP and e.key == K_d:
                right = False
        if pygame.sprite.spritecollideany(player,killing_entities):
            main()
        if pygame.sprite.spritecollideany(player,another_level):
            level = [
'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP','P             S           S            S             S     P','P                   S           S             S            P','P         PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP  P','PP        P   P   P     SS      SS         S     S         P','PS        P P S P S     SS  SS  SS  SS     S  S  S  S      P','P         P S P S P     SS  SS  SS  SS     S  S  S  S      P','P         P P   P     S     SS      SS        S     S      P','P         P PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPssp','PP                                                       EEP','PS                                                         P','PP                                                         P','PPS                                                        P','P         P                                                P','PSSSSSSSSSSSSSSSSSSSSsspPPSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsspPPP',]
            main()
        for y in range(32):
            for x in range(32):
                screen.blit(bg,(x * 32,y * 32))
        player.update(up,down,left,right,left_dash,right_dash,running,dashing,platforms)
        entities.draw(screen)
        pygame.display.flip()
        pygame.display.update()
class Entity(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
class Player(Entity):
    def __init__(self,x,y):
        Entity.__init__(self)
        self.xvel = 0
        self.yvel = 0
        self.onGround = False
        self.image = Surface((32,32))
        self.image.fill(Color('#0000FF'))
        self.image.convert()
        self.rect = Rect(x,y,32,32)
    def update(self,up,platforms):
        if up:
            if self.onGround: self.yvel -= 9
        if down:
            pass
        if running:
            self.xvel = 12
        if left:
            self.xvel = -8
        if right:
            self.xvel = 8
        if not self.onGround:
            self.yvel += 0.25
            if self.yvel > 100: self.yvel = 100
        if not(left or right):
            self.xvel = 0        
        self.rect.left += self.xvel
        self.collide(self.xvel,platforms)
        self.rect.top += self.yvel
        self.onGround = False
        self.collide(0,self.yvel,platforms)
    def collide(self,xvel,yvel,platforms):
        for p in platforms:
            if pygame.sprite.collide_rect(self,p):
                if isinstance(p,Block):
                    pygame.event.post(pygame.event.Event(QUIT))
                if xvel > 0:
                    self.rect.right = p.rect.left
                if xvel < 0:
                    self.rect.left = p.rect.right
                if yvel > 0:
                    self.rect.bottom = p.rect.top
                    self.onGround = True
                    self.yvel = 0
                if yvel < 0:
                    self.rect.top = p.rect.bottom
                    self.onGround = False
class Platform(Entity):
    def __init__(self,y):
        Entity.__init__(self)
        self.image = Surface((32,32))
        self.image.convert()
        self.image.fill(Color('#ddddDF'))
        self.rect = Rect(x,32)
    def update(self):
        pass
class Spike(pygame.sprite.Sprite):
    def __init__(self,y):
        Platform.__init__(self,y)
        self.image = Surface((32,32))
        self.image.convert()
        self.image.fill(Color('#E70018'))
        self.rect = Rect(x,32)
class Another_Level(pygame.sprite.Sprite):
    def __init__(self,32))
        self.image.convert()
        self.image.fill(Color('#8C563E'))
        self.rect = Rect(x,32)
if __name__ == '__main__':
    main()
pygame.quit()```

解决方法

您将文本 blit 到 bg 表面。然后你将该 Surface 传送到屏幕 1024 次,每个方向上每 32 个像素:

for y in range(32):
    for x in range(32):
        screen.blit(bg,(x * 32,y * 32))

要么使 bg 大到足以覆盖整个屏幕,然后 blit 一次

bg = Surface(DISPLAY)
bg.convert()
bg.fill(Color('#000000'))
text = 'Score:'
font = pygame.font.SysFont('Consolas',22)
bg.blit(font.render(text,True,(green)),(100,100))

...

# in the main loop
    screen.blit(bg,(0,0))

或存储文本表面并将其每帧一次 blit 到 screen 而不是 bg

bg = Surface((WIN_WIDTH,WIN_HEIGHT  // 30))
bg.convert()
bg.fill(Color('#000000'))
text = 'Score:'
font = pygame.font.SysFont('Consolas',22)
text_surf,text_rect = font.render(text,(1,1)
entities = pygame.sprite.Group()
...


# in the main loop:
    screen.fill('black')
    screen.blit(text_surf,100))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。