I don’t what happened, it was working few minutes ago, but now it’s not. It’s crashing every time.
import pygame, sys WIDTH, HEIGHT = 800, 700 pygame.init() win = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Qix") clock = pygame.time.Clock() # Player Class class Player: def __init__(self, x, y): self.x = int(x) self.y = int(y) self.rect = pygame.Rect(self.x, self.y, 32, 32) self.color = (250, 120, 60) self.velX = 0 self.velY = 0 self.left_pressed = False self.right_pressed = False self.up_pressed = False self.down_pressed = False self.speed = 4 self.enterPush = False while self.enterPush is True: if self.left_pressed and not self.right_pressed: self.velX = -self.speed if self.right_pressed and not self.left_pressed: self.velX = self.speed self.x += self.velX self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32) def draw(self, win): pygame.draw.rect(win, self.color, self.rect) def push(self): if self.rect.x>760 or self.rect.y>560: self.enterPush = True def update(self): self.velX = 0 self.velY = 0 while self.enterPush is False: if self.left_pressed and not self.right_pressed: self.velX = -self.speed if self.right_pressed and not self.left_pressed: self.velX = self.speed if self.up_pressed and not self.down_pressed : self.velY = -self.speed if self.down_pressed and not self.up_pressed : self.velY = self.speed self.x += self.velX self.y += self.velY self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32) player = Player(WIDTH / 2, HEIGHT / 2) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player.left_pressed = True if event.key == pygame.K_RIGHT: player.right_pressed = True if event.key == pygame.K_UP: player.up_pressed = True if event.key == pygame.K_DOWN: player.down_pressed = True if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: player.left_pressed = False if event.key == pygame.K_RIGHT: player.right_pressed = False if event.key == pygame.K_UP: player.up_pressed = False if event.key == pygame.K_DOWN: player.down_pressed = False win.fill((12, 24, 36)) player.draw(win) player.update() pygame.display.flip() clock.tick(60) pygame.display.update()
I have to fix this in one hour.
I read other answers related to this but nothing seems to work.
Just adding details
Just adding details Just adding details
Just adding details
Just adding details
Just adding details
Advertisement
Answer
Here You go, fixed the code:
import pygame WIDTH, HEIGHT = 800, 700 pygame.init() win = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Qix") clock = pygame.time.Clock() # Player Class class Player: def __init__(self, x, y): self.x = int(x) self.y = int(y) self.rect = pygame.Rect(self.x, self.y, 32, 32) self.color = (250, 120, 60) self.velX = 0 self.velY = 0 self.left_pressed = False self.right_pressed = False self.up_pressed = False self.down_pressed = False self.speed = 4 self.enterPush = False while self.enterPush is True: if self.left_pressed and not self.right_pressed: self.velX = -self.speed if self.right_pressed and not self.left_pressed: self.velX = self.speed self.x += self.velX self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32) def draw(self, win): pygame.draw.rect(win, self.color, self.rect) def push(self): if self.rect.x>760 or self.rect.y>560: self.enterPush = True def update(self): self.velX = 0 self.velY = 0 # while self.enterPush is False: if self.left_pressed and not self.right_pressed: self.velX = -self.speed if self.right_pressed and not self.left_pressed: self.velX = self.speed if self.up_pressed and not self.down_pressed : self.velY = -self.speed if self.down_pressed and not self.up_pressed : self.velY = self.speed self.x += self.velX self.y += self.velY self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32) player = Player(WIDTH / 2, HEIGHT / 2) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player.left_pressed = True if event.key == pygame.K_RIGHT: player.right_pressed = True if event.key == pygame.K_UP: player.up_pressed = True if event.key == pygame.K_DOWN: player.down_pressed = True if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: player.left_pressed = False if event.key == pygame.K_RIGHT: player.right_pressed = False if event.key == pygame.K_UP: player.up_pressed = False if event.key == pygame.K_DOWN: player.down_pressed = False win.fill((12, 24, 36)) player.draw(win) player.update() # pygame.display.flip() clock.tick(60) pygame.display.update()
First of about formatting (suggestion according to PEP 8): 1 newline between class method definitions, import like in the code I edited (tho this is acceptable: from module import class, func, etc
).
Also I don’t see the point of both .flip()
and .update()
since they basically do the same.
About Your poblem:
while self.enterPush is False: if self.left_pressed and not self.right_pressed: self.velX = -self.speed if self.right_pressed and not self.left_pressed: self.velX = self.speed if self.up_pressed and not self.down_pressed : self.velY = -self.speed if self.down_pressed and not self.up_pressed : self.velY = self.speed self.x += self.velX self.y += self.velY self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
this was Your issue: the while loop
solution: not using it:
def update(self): self.velX = 0 self.velY = 0 # while self.enterPush is False: if self.left_pressed and not self.right_pressed: self.velX = -self.speed if self.right_pressed and not self.left_pressed: self.velX = self.speed if self.up_pressed and not self.down_pressed : self.velY = -self.speed if self.down_pressed and not self.up_pressed : self.velY = self.speed self.x += self.velX self.y += self.velY self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)