Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何高效地blit 72图像_Python_Pygame - Fatal编程技术网

Python 如何高效地blit 72图像

Python 如何高效地blit 72图像,python,pygame,Python,Pygame,dic_s与18个曲面的位置有关,尽管它实际上应该是72,dic_n与该区域被单击的次数有关 现在运行数字计数器的主代码是: 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.

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

现在运行数字计数器的主代码是:

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}

对72个表面进行切割不是问题

下面是一个移动和光刻72个曲面的示例。没有任何优化,但仍以数百FPS的速度运行:

                dic_n['1']
                text_surface, rect = gamefont.render(n,(4, 8, 18))
                screen.blit(board,(0,0))
                pygame.display.flip()


至于单击不同的曲面,您应该为每个曲面提供一个计算单击次数的状态,因此让我们继续使用
Sprite
类,事情就变得简单了:

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()

                dic_n['1']
                text_surface, rect = gamefont.render(n,(4, 8, 18))
                screen.blit(board,(0,0))
                pygame.display.flip()
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()
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()