如何有效地对72张图像进行翻转

时间:2019-03-28 10:39:07

标签: python pygame

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

1 个答案:

答案 0 :(得分:4)

对72个表面进行刮印不是问题。

这里是移动和涂抹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()

enter image description here


对于点击不同的表面,您应该为每个表面提供一个统计点击次数的状态,因此让我们继续使用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()

enter image description here

相关问题