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()
-
\$\begingroup\$ Welcome to Code Review! Please see What to do when someone answers. I have rolled back Rev 3 → 2 \$\endgroup\$Sᴀᴍ Onᴇᴌᴀ– Sᴀᴍ Onᴇᴌᴀ ♦2019年11月04日 23:16:47 +00:00Commented Nov 4, 2019 at 23:16
2 Answers 2
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.
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
andself.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 thewhile
loop flow is redundant.
We're just starting the loop withwhile True:
and breaking it withbreak
.
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()
-
\$\begingroup\$ I get your last point. In my opinion, having
while run:
makes it easier to understand what the loop is doing. \$\endgroup\$Tommy Shelburne– Tommy Shelburne2019年11月04日 22:49:36 +00:00Commented 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 simplybreak
when needed, instead of setting the flag toFalse
and maybe allowing code after it to run. \$\endgroup\$Ben A– Ben A2019年11月05日 01:34:03 +00:00Commented Nov 5, 2019 at 1:34
Explore related questions
See similar questions with these tags.