当前位置: 首页 > 知识库问答 >
问题:

为什么推杆文本会这样做?

史同化
2023-03-14

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

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                                                          P',
'P                        PP                                P',
'P                                                          P',
'P                                 PP                       P',
'P                                                          P',
'P                                                          P',
'P             PP                                           P',
'P                                                          P',
'P                                                          P',
'P                                                          P',
'P                                                          P',
'P                     PP                                   P',
'P                                                          P',
'P                                                          P',
'P                                                          P',
'P                                                   P      P',
'P                            PP                     P      P',
'P                                                   P      P',
'P                                                   P      P',
'P                                                   P      P',
'P                    PP                             P      P',
'P                                                   P      P',
'P                                                   P      P',
'P           PP                                      P      P',
'P                                                   P      P',
'P                      PPP                          P     PP',
'P                                                   P      P',
'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',
'P                                                          P',
'P                                                          P',
'P                                                          P',
'PP                                                         P',
'PS                                                         P',
'P                                                          P',
'P                                                          P',
'P                                                          P',
'PPS                                                        P',
'PS                                                         P',
'P                                                          P',
'P                                                          P',
'P                                                          P',
'PP                                                         P',
'P                                                          P',
'P                                                          P',
'P                                                          P',
'P                                                          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, down, left, right, running, 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, 0, 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, x, y):
        Entity.__init__(self)
        self.image = Surface((32, 32))
        self.image.convert()
        self.image.fill(Color('#DDDDDF'))
        self.rect = Rect(x, y, 32, 32)
    def update(self):
        pass
class Spike(pygame.sprite.Sprite):
    def __init__(self, x, y):
        Platform.__init__(self, x, y)
        self.image = Surface((32, 32))
        self.image.convert()
        self.image.fill(Color('#E70018'))
        self.rect = Rect(x, y, 32, 32)
class Another_Level(pygame.sprite.Sprite):
    def __init__(self, x, y):
        Platform.__init__(self, x, y)
        self.image = Surface((32, 32))
        self.image.convert()
        self.image.fill(Color('#8C563E'))
        self.rect = Rect(x, y, 32, 32)
if __name__ == '__main__':
    main()
pygame.quit()```

共有1个答案

万浩淼
2023-03-14

您将文本删除到bg表面。然后你将这个表面在屏幕上涂抹1024次,每个方向每32个像素:

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

或者使bg大到足以覆盖整个屏幕,然后将其破坏一次:

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存储到屏幕上,而不是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, True, (green)), (1, 1)
entities = pygame.sprite.Group()
...


# in the main loop:
    screen.fill('black')
    screen.blit(text_surf, (100, 100))
 类似资料:
  • 我已经花了一个多小时试图解决这个问题,但我什么都没做。 当我试图使用Maven编译我的项目时,我得到了以下错误: [错误]无法执行目标组织。阿帕奇。专家插件:maven assembly插件:2.2-beta-5:single(默认cli)在GankALane项目上:无法解析mojo org的配置。阿帕奇。专家插件:maven assembly插件:2.2-beta-5:单参数存档:在组织中找不到

  • 这是一个框架的源代码,当我使用convert()函数时 让我感到奇怪的是,返回类型是新类型变量R,他只是调用this.setRecords(collect);但是setRecords()函数只接收列表 ! 为了验证这一点,我自己编写了一个接口,但是编译失败了

  • 线上出现这种问题的,过了一会就好了

  • 我在中使用带有4.1版本模拟器的android最新版本sdk。一切都很好。但是在我的中,对于任何应用程序的每次运行,我都会得到以下语句。 即使在Hello world应用程序中,我也得到相同的logcat输出。我没有在我的应用程序中使用多线程。有人能告诉我为什么在我的logcat中得到这些日志。 这是我的密码 在我的异步任务中,我从服务器获取JSONArray,解析它并列出。

  • 我使用的是Java中的<code>或者</code>的本地实现,它有如下方法: 这两种方法可以编译并正常工作: 此方法不编译: 错误: (我去掉了包限定符,使错误更加易读) 我可以通过指定类型来编译它: 但我为什么需要这样做?我如何避免这种代码混乱?

  • 我的代码中有一个< code>JCombobox。我已经添加了< code>FocusLost事件。但它无论如何也没有被解雇。我已经尝试了很多次,但没有找到解决办法。 但控制台中没有打印任何内容。请告诉我我做错了什么。