3
\$\begingroup\$

How does this Pygame code look? Is the run loop usually in a class? I'm a beginner and was wondering what the best format and structure is for using OOP in Pygame.

import pygame
pygame.init()
win = pygame.display.set_mode((500, 500)) 
pygame.display.set_caption("Game")
class Game:
 def __init__(self):
 self.x = 250
 self.y = 250
 self.width = 50
 self.height = 50
 self.vel = 5
 self.isJump = False
 self.jumpCount = 10
 def run(self):
 run = True
 while run:
 pygame.time.delay(30)
 for event in pygame.event.get():
 if event.type == pygame.QUIT:
 run = False
 keys = pygame.key.get_pressed()
 if keys[pygame.K_LEFT] and self.x > 0:
 self.x -= self.vel
 if keys[pygame.K_RIGHT] and self.x < 500 - self.width:
 self.x += self.vel
 if not self.isJump:
 if keys[pygame.K_UP] and self.y > 0:
 self.y -= self.vel
 if keys[pygame.K_DOWN] and self.y < 500 - self.height:
 self.y += self.vel
 if keys[pygame.K_SPACE]:
 self.isJump = True
 else:
 if self.jumpCount >= -10:
 self.y -= (self.jumpCount * abs(self.jumpCount)) * 0.5
 self.jumpCount -= 1
 else:
 self.jumpCount = 10
 self.isJump = False
 win.fill((0, 0, 0))
 self.draw()
 pygame.quit()
 def draw(self):
 pygame.draw.rect(win, (0, 0, 255), (self.x, self.y, self.width, self.height))
 pygame.display.update()
g = Game()
g.run()
Sᴀᴍ Onᴇᴌᴀ
29.5k16 gold badges45 silver badges201 bronze badges
asked Nov 4, 2019 at 2:03
\$\endgroup\$
1

2 Answers 2

2
\$\begingroup\$

This answer pertains to using while run vs while True.

Lets say you have this code:

run = True
 count = 0
 while run:
 if count == 5:
 run = False
 count += 1
 print(count)

Simple enough code. When viewing it as this, a programmer might see that the code will stop when count reaches 5. Lets look at the output:

1
2
3
4
5
6

Woah, whats this? 6 was printed even though the program was supposed to stop at 5. This happens because even though run is now False, it doesn't immediately exit the loop. This prevents the next iteration from running. So, any code in the rest of the loop will run regardless of what run is.

Now lets look at using while True:

count = 0
while True:
 if count == 5:
 break
 count += 1
 print(count)

And the output

1
2
3
4
5

The break immediately exits the loop, preventing count from being incremented a final time.

answered Nov 5, 2019 at 1:45
\$\endgroup\$
2
\$\begingroup\$

Few areas to improve:

  • self.vel. When I first saw this field/variable I thought: "What is that?".
    I guess many other people may have the same first feeling.
    I would rename it to an explicit and clear name: self.velocity

  • self.isJump and self.jumpCount violate Python naming conventions (instance variable names should be all lower case, words in an instance variable name should be separated by an underscore). Therefore, rename them to:

    ...
    self.is_jump
    self.jump_count`
    
  • run = True flag. That flag for just controlling the while loop flow is redundant.
    We're just starting the loop with while True: and breaking it with break.
    So it becomes:

    def run(self):
     while True:
     pygame.time.delay(30) 
     keys = pygame.key.get_pressed()
     # handling pressed keys
     if keys ...
     win.fill((0, 0, 0))
     self.draw()
     for event in pygame.event.get():
     if event.type == pygame.QUIT:
     break
     pygame.quit()
    
Ben A
10.7k5 gold badges37 silver badges101 bronze badges
answered Nov 4, 2019 at 7:38
\$\endgroup\$
2
  • \$\begingroup\$ I get your last point. In my opinion, having while run: makes it easier to understand what the loop is doing. \$\endgroup\$ Commented Nov 4, 2019 at 22:49
  • 1
    \$\begingroup\$ @TommyShelburne I can understand that, but you're creating an unnecessary variable to hold a boolean. With a while True:, you can simply break when needed, instead of setting the flag to False and maybe allowing code after it to run. \$\endgroup\$ Commented Nov 5, 2019 at 1:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.