I'm doing a project in which I used pygame to design a rather simple snake game, but I've run into a small error:
When I threw the code in an event loop so it was able to be played again after you crashed your worm, the movement options no longer worked, I'm wondering if someone could help me with this problem...
This is what I used for the event loop
def game(): running = True #Code Here if worm.crashed: restart = input("Play Again?(Y/N) ") if restart == "Y" game() else: print("Thank you for playing") break
That works perfectly fine
Then we come to the keyboard events/motion, it worked before placing the event loop in but now..
def event(self, event): #Handles keyboard events if event.key == pygame.K_UP: if self.vy == -1: return self.vx = 0 self.vy = -1 elif event.key == pygame.K_DOWN: if self.vy == -1: return self.vx = 0 self.vy = 1 elif event.key == pygame.K_LEFT: if self.vx == 1: return self.vx = -1 self.vy = 0 elif event.key == pygame.K_RIGHT: if self.vx == -1: return self.vx = 1 self.vy = 0 def move(self): #moving the worm self.x+= self.vx self.y += self.vy if (self.x, self.y) in self.body: self.crashed = True self.body.insert(0, (self.x, self.y)) if (self.grow_to > self.length): self.length += 1 if len(self.body) > self.length: self.body.pop()