【问题标题】:How can i optimize my code so that it can run much more efficiently?How can i optimize my code so that it can run much more effciently?
【发布时间】:2022-12-02 03:27:42
【问题描述】:

i am sorry if this this is the wrong type of question to ask here because its mostly like "pls help me fix bug" but if someone is willing to help that would be nice! so basiclly i am making a small game where at the current stage i click somewhere and color will spread out like a wave. currently it does that, but i am bad at life, so my system is that it checks each and every squares up down left and right square to see if its red. if it is, make that square red. then it saves that x,y and does it a bunch more, then it prints all x,y values to the screen all at once. the problem is, every time it does this it keeps all the values in it and has to do more if checks and slows down a lot. is there a better system i can implement that would be better? thank you

import pygame
import sys
width=300
height=300
pygame.init()
surface = pygame.display.set_mode( (500, 500) )


size=1

tfx=1
tfy=1
increment=1
red=[]
surface.fill( (255,255,255) )
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            pygame.draw.rect( surface, (255,0,0), (pos[0],pos[1], size, size) )
        
   
    #pygame.draw.rect( surface, (255,0,0), (width/2-tfx/2, height/2-tfy/2, size, size) )
    

   #!!!!!! this block of code checks every single pixel on the screen to see if the pixel
    #to the right is red and so on. Alternative method in progress that checks in a 1x1 3x3 5x5 square

    
    for x in range(width):
        for y in range(height):
            
            if x+size <=width-1   :
               color = surface.get_at((x+size,y))
               if  color[1]==0:
                   if  surface.get_at((x,y))[1] !=0:
                    red.append((x,y))
                   #pygame.display.update()

            if x-1 >=0:
               color = surface.get_at((x-1,y))
               if color[1]==0:
                   if  surface.get_at((x,y))[1] !=0:
                       red.append((x,y))
                   
            if y-1 >=0:
               color = surface.get_at((x,y-1))
               if color[1]==0:
                   if  surface.get_at((x,y))[1] !=0:
                        red.append((x,y))
            if y+1 <=height:
               color = surface.get_at((x,y+1))
               if color[1]==0:
                   if  surface.get_at((x,y))[1] !=0:
                        red.append((x,y))


    
    








    
    for i in range(len(red)):
        
        
        pygame.draw.rect( surface, (255,0,0), (red[i][0], red[i][1], size, size) )


    pygame.display.update()

    #print( surface.get_at((97,99)))

pygame.quit()

【问题讨论】:

  • @Rabbid76 mb, thanks. can i just copy and paste my whole question or? ive never been there before
  • @MattDMo fax, sorry
  • @MattDMo, the question needs work before it's suited to Code Review. You should have pointed the asker at A guide to Code Review for Stack Overflow users, as some things are done differently over there - e.g. we need a good description of thepurposeof the code to give context, and question titles should simply say what the codedoes(the question is always, "How can I improve this?"). It's important that the code works correctly; include the unit tests if possible.
  • @MattDMo Don't migrate crap. Don't redirect it either, please.
  • @TobySpeight thanks for the link. I may have read it years ago, but I didn't save it at the time. I typically tell people to read the site's help center first, but I guess I forgot to here (no sure, comment's been deleted). This link will help a lot.

标签: python pygame


【解决方案1】:

Probably the only way to get acceptable performance (with pygame) is to use pygame.mask.Mask and convolve(). Create a mask size of the screen:

mask = pygame.mask.Mask(screen.get_size())

Create a convolution mask with the following pattern:

[False, True,  False]
[True,  True,  True ]
[False, True,  False]
convolution_mask = pygame.mask.Mask((3, 3))
convolution_mask.set_at(p) for p in [(0, 1), (1, 1), (2, 1), (1, 0), (1, 2)]]

Set a bit in the mask on mouse click:

if event.type == pygame.MOUSEBUTTONDOWN:
    mask.set_at(event.pos)

Create a convolution with the convolution mask in each frame:

mask = mask.convolve(convolution_mask , offset=(-1, -1))

Convert the mask into a surface and blit it on the screen:

surface = mask.to_surface(setcolor = (255, 0, 0), unsetcolor = (255, 255, 255))
screen.blit(surface, (0, 0))

import pygame
import sys

pygame.init()
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

mask = pygame.mask.Mask(screen.get_size())
convolution_mask = pygame.mask.Mask((3, 3))
[convolution_mask.set_at(p) for p in [(0, 1), (1, 1), (2, 1), (1, 0), (1, 2)]]

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            mask.set_at(event.pos)

    mask = mask.convolve(convolution_mask, offset=(-1, -1))

    surface = mask.to_surface(setcolor = (255, 0, 0), unsetcolor = (255, 255, 255))
    screen.blit(surface, (0, 0))
    pygame.display.update()

pygame.quit()
sys.exit()

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 2022-12-26
    • 2022-12-01
    • 2022-12-02
    • 2022-12-27
    • 2023-01-31
    • 2022-12-02
    • 2022-11-20
    • 2022-12-02
    相关资源
    最近更新 更多