2

I followed a snake game tutorial but for some reason I can't get the QUIT to match:

class Game:

    ...

    def handle_keys(self):
        for event in pg.event.get():
            pressed = pg.key.get_pressed()
            if event.type == QUIT or pressed[QUIT]:
                pg.quit()
                sys.exit()
            elif event.type == pg.KEYDOWN:
                if pressed[pg.K_UP]:
                    self.snake.turn(self.UP)
                elif pressed[pg.K_DOWN]:
                    self.snake.turn(self.DOWN)
                elif pressed[pg.K_LEFT]:
                    self.snake.turn(self.LEFT)
                elif pressed[pg.K_RIGHT]:
                    self.snake.turn(self.RIGHT)

    def run(self):
        while True:
            self.clock.tick(10)
            self.handle_keys()

The snake moves fine but the QUIT button doesn't seem to be working no matter which method I try or even a combination of both as shown above. I'm on the latest Ubuntu 20.04 version not sure if that makes a difference. I noticed the values for event.TYPE and pressed[QUIT] are completely different from pg.QUIT and pg.locals.QUIT. This is my first attempt at pygame as well just not sure why not getting the same output as the guide I'm following.

EDIT:

None of the solutions I find online are working nor the ones "related" or "suggested" below. I'm just going to switch to Unity3D after struggling with this for over a week now.

Marco Fernandes
  • 326
  • 1
  • 4
  • 13

2 Answers2

2

Maybe try

if event.type == pg.QUIT:

removing the or pressed[QUIT].
Don't use pg.locals.QUIT, try pg.QUIT.

NrdyBhu1
  • 375
  • 4
  • 14
1

I added a few lines of code to your code, and this works:

import pygame as pg
import sys

pg.init()
wn = pg.display.set_mode((600, 600))

class Game:
    def __init__(self):
        self.UP = 0
        self.DOWN = 0
        self.LEFT = 0
        self.RIGHT = 0
        self.clock = pg.time.Clock()
        
    def turn(self, number):
        pass
    
    def handle_keys(self):
        for event in pg.event.get():
            pressed = pg.key.get_pressed()
            if event.type == pg.QUIT or pressed[pg.QUIT]:
                pg.quit()
                sys.exit()
            elif event.type == pg.KEYDOWN:
                if pressed[pg.K_UP]:
                    self.snake.turn(self.UP)
                elif pressed[pg.K_DOWN]:
                    self.snake.turn(self.DOWN)
                elif pressed[pg.K_LEFT]:
                    self.snake.turn(self.LEFT)
                elif pressed[pg.K_RIGHT]:
                    self.snake.turn(self.RIGHT)

    def run(self):
        while True:
            self.clock.tick(10)
            self.handle_keys()
            pg.display.update()

game = Game()
game.run()

I changed the QUIT to pg.QUIT.

Red
  • 26,798
  • 7
  • 36
  • 58
  • not working on my machine still. It's a very simple thing that's annoying me for not working at this point I'm just downloading unity3d – Marco Fernandes Feb 20 '21 at 14:02
  • 1
    @MarcoFernandes Perhaps uninstall and then reinstall pygame? The pygame you have now might be not fully installed. – Red Feb 20 '21 at 14:04
  • I've tried that already as well. Including installing it via `apt` and `pip`. I tried setting up a completely new Ubuntu environment virtually to test and still not working. At this point I'm assuming it's my laptop's manufacturers keyboard settings – Marco Fernandes Feb 20 '21 at 14:07