def check_mx_range1(mx, my):
        switch1 = 0
        while switch1==0:
            for mx_range1 in mousedata.mx_ranges1:
                    if mx_range1[0] < mx < mx_range1[1] and 890 < my < 920 and switch1==0:
                        switch1+=1
                        a11=0
                        b11=0
                        a11=str(mx_range1[2])
                        b11=str(mx_range1[3])
                        mousedata.dic_n[a11]+=1
                        #print(mx_range1)
                        #print(a11)
                        #print(b11)
                        print(mousedata.dic_n[a11])
                        return True
                    if False:
                        break
            return False

可能很难阐明这一点......好吧,所以我正在制作“drunkopoly”,所以这些函数调用一个范围数组并将它们放入字典中。

字典如下:
dic_s={'101': [868.0, 905.0], '102': [827.0, 905.0], '103': [785.0, 905.0], '104': [743.0, 905.0], '105': [701.0, 905.0], '106': [659.0, 905.0], '107': [617.0, 905.0], '108': [575.0, 905.0], '109': [533.0, 905.0], '110': [491.0, 905.0], '111': [449.0, 905.0], '112': [407.0, 905.0], '113': [365.0, 905.0], '114': [323.0, 905.0], '115': [281.0, 905.0], '116': [239.0, 905.0], '117': [197.0, 905.0], '118': [155.0, 905.0]}
dic_n={'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9': 0, '10': 0, '11': 0, '12': 0, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, '20': 0, '21': 0, '22': 0, '23': 0, '24': 0, '25': 0, '26': 0, '27': 0, '28': 0, '29': 0, '30': 0, '31': 0, '32': 0, '33': 0, '34': 0, '35': 0, '36': 0, '37': 0, '38': 0, '39': 0, '40': 0, '41': 0, '42': 0, '43': 0, '44': 0, '45': 0, '46': 0, '47': 0, '48': 0, '49': 0, '50': 0, '51': 0, '52': 0, '53': 0, '54': 0, '55': 0, '56': 0, '57': 0, '58': 0, '59': 0, '60': 0, '61': 0, '62': 0, '63': 0, '64': 0, '65': 0, '66': 0, '67': 0, '68': 0, '69': 0, '70': 0, '71': 0, '72': 0}

dic_s 与 18 个表面的位置有关,尽管它实际上应该是 72 个,而 dic_n 与该区域被点击的次数有关。

现在运行数字计数器的主要代码是:
def run_game():

    pygame.init()
    #Font information
    gamefont=pygame.freetype.Font("OpenSans-Bold.ttf",12)
    #Number in font mx and my posion of mouse curser
    gamestage=0
    n=0
    text_surface, rect = gamefont.render("0",(4, 8, 18))
    text_surface1, rect = gamefont.render("0",(4, 8, 18))
    #Main screen display options
    ai_settings = Settings()

    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption("Drunkopoly")

    b=screen.blit(board,(0,0))
    #Beggining of the main game loop

    while True:
        #keyboard and mouse events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if gamestage == 0 and gamestage <= 1:
                gamestage += 1
                mx, my =pygame.mouse.get_pos()
                print(mx,my)

                mousedata.check_mx_range1(mx,my)

                    pygame.display.flip()


        if event.type == pygame.MOUSEBUTTONUP:
            if gamestage != 0:
                gamestage = 0


        screen.blit(text_surface, (mousedata.dic_s['101']))

        pygame.display.flip()
run_game()

所以这意味着我需要 72 个带数字的曲面。我该如何处理这个问题?看起来 pygame 可能无法处理抛出的 72 个图像。

请记住,这是我的第一周,尽管我每天都花 12 小时编码哈哈。因此,如果部分代码不好,请告诉我,并且我可以在样式和方法上更改某些内容也请告诉我,也欢迎提供一般性建议。

我能够让一个柜台为一个人工作,但对他们所有人来说这可能太难了。
                dic_n['1']
                text_surface, rect = gamefont.render(n,(4, 8, 18))
                screen.blit(board,(0,0))
                pygame.display.flip()

最佳答案

Blitting 72 个表面不是问题。

这是移动和 blitting 72 个表面的示例。没有任何优化,它仍然以数百 FPS 运行:

import pygame
import random

class Stuff(pygame.sprite.Sprite):
    def __init__(self, pos, color):
        super().__init__()
        self.image = pygame.Surface((30, 30))
        self.image.fill(color)
        self.rect = self.image.get_rect(center=pos)

    def update(self):
        self.rect.move_ip(random.randint(-1, 1), random.randint(-1, 1))

def main():
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    screen_rect = screen.get_rect()
    font = pygame.font.SysFont(None, 26)
    clock = pygame.time.Clock()
    sprites = pygame.sprite.Group()
    for _ in range(72):
        x, y = random.randint(0, 500), random.randint(0, 500)
        color = random.choice(['green', 'blue', 'yellow', 'white', 'orange'])
        sprites.add(Stuff((x, y), pygame.Color(color)))

    dt = 1
    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return

        sprites.update()
        screen.fill(pygame.Color('grey'))
        sprites.draw(screen)
        text = font.render(f'{clock.get_fps()} FPS', True, pygame.Color('black'))
        screen.blit(text, (20, 20))
        pygame.display.flip()
        dt = clock.tick(20000)

if __name__ == '__main__':
    main()

python - 如何有效地 blit 72 图像-LMLPHP

至于点击不同的表面,你应该给每个表面一个计数点击的状态,所以让我们继续使用 Sprite 类,事情变得简单:
import pygame
import random

class Stuff(pygame.sprite.Sprite):
    def __init__(self, pos, color, font):
        super().__init__()
        self.image = pygame.Surface((30, 30))
        self.image.fill(color)
        self.image.blit(font.render('0', True, (0, 0, 0)), (10, 10))
        self.rect = self.image.get_rect(center=pos)
        self.font = font
        self.hits = 0
        self.color = color

    def update(self, events):
        self.rect.move_ip(random.randint(-1, 1), random.randint(-1, 1))
        for event in events:
            if event.type == pygame.MOUSEBUTTONDOWN:
                if self.rect.collidepoint(event.pos):
                    self.hits += 1
                    self.image.fill(self.color)
                    self.image.blit(self.font.render(f'{self.hits}', True, (0, 0, 0)), (10, 10))

def main():
    pygame.init()
    screen = pygame.display.set_mode((1000, 1000))
    screen_rect = screen.get_rect()
    font = pygame.font.SysFont(None, 26)
    clock = pygame.time.Clock()
    sprites = pygame.sprite.Group()
    dic_s={'101': [868.0, 905.0], '102': [827.0, 905.0], '103': [785.0, 905.0], '104': [743.0, 905.0], '105': [701.0, 905.0], '106': [659.0, 905.0], '107': [617.0, 905.0], '108': [575.0, 905.0], '109': [533.0, 905.0], '110': [491.0, 905.0], '111': [449.0, 905.0], '112': [407.0, 905.0], '113': [365.0, 905.0], '114': [323.0, 905.0], '115': [281.0, 905.0], '116': [239.0, 905.0], '117': [197.0, 905.0], '118': [155.0, 905.0]}

    for key in dic_s:
        color = random.choice(['green', 'blue', 'yellow', 'white', 'orange'])
        sprites.add(Stuff(dic_s[key], pygame.Color(color), font))

    dt = 1
    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return

        sprites.update(events)
        screen.fill(pygame.Color('grey'))
        sprites.draw(screen)
        text = font.render(f'{clock.get_fps()} FPS', True, pygame.Color('black'))
        screen.blit(text, (20, 20))
        pygame.display.flip()
        dt = clock.tick(60)

if __name__ == '__main__':
    main()

python - 如何有效地 blit 72 图像-LMLPHP

关于python - 如何有效地 blit 72 图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55395523/

10-12 22:44