简体   繁体   中英

What would be an easier way to update a Pygame display?

I've recently tried teaching myself Python and I've made a app using Pygame to generate random stats for D and D enemies. 2 Questions. What would be a cleaner or better solution be for updating the Pygame display after clicking a button? Because currently I have the loop redefining the main window variable after every click in an attempt to get the stats to stay on the display.

if zombie.isOver(pos):
     win.fill((255, 255, 255))
     menu.draw(win, (0, 0, 0))
     Zombie()
     pygame.display.update()
     x -= 1
     def drawWindow():
         again.draw(win, (0, 0, 0))

After the button is pressed this gets redefined.

def drawWindow():
    win.fill((255, 255, 255))
    bandit.draw(win, (0, 0, 0))
    zombie.draw(win, (0, 0, 0))
    rat.draw(win, (0, 0, 0))

I did this because the win.fill would cover up the values printed and I couldn't get them to stay any other way.

2nd question is when I mash the buttons in the display, 2 out of the 3 current ones work fine, however the 3rd button's values stutter and it is not smooth like the other 2. I think this is being caused by the location of the values in the code, they are the last ones I added putting them further down in the code, but honestly I have no idea.

if rat.isOver(pos):
     win.fill((255, 255, 255))
     menu.draw(win, (0, 0, 0))
     Rat()
     pygame.display.update()
     x /= 2
     def drawWindow():
         again.draw(win, (0, 0, 0))

if again.isOver(pos) and x == 5:
    win.fill((255, 255, 255))
    menu.draw(win, (0, 0, 0))
    again.draw(win, (0, 0, 0))
    Rat()
    pygame.display.update()
    def drawWindow():
        again.draw(win, (0, 0, 0))

The button works the same as the other 2 but the values stutter. If anyone has an idea that would help it would be greatly appreciated. Thank You.

Ok, not sure if i completely understand how you are doing it but on way to do it is in the main loop, call drawWindow() . Which is this:

def drawWindow():
    win.fill((255, 255, 255))
    bandit.draw(win, (0, 0, 0))
    zombie.draw(win, (0, 0, 0))
    rat.draw(win, (0, 0, 0)) 

I'm assuming that that is drawing images of them and not the stats. have a bool that controls whether to show the stats like show_zombie_stats for each of them. Then in the main loop or drawWindow() , you can do an if statement like

if show_zombie_stats:
     zombie.draw_stats()

then call pygame.display.update() at the end of the main loop.

Now you can change the show_zombie stats with the button with show_zombie stats = not show_zombie stats

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM