0

I was just starting a new game with pygame on python 3.6.5 on a Windows 10 PC, when I ended up with a really crazy glitch that I couldn't solve. This is my code, I'll explain my problem afterwards.

import pygame

pygame.init()

def create_window():
    window = pygame.display.set_mode((800,600),pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN)


create_window()

isRunning = True

while isRunning:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.type == K_a:
                isRunning = False


pygame.quit()

So when I ran this code, pygame ran as normal, creating a fullscreen black window. Then a mouse appeared and when I pressed on the A key. When I did this, I couldn't exit the pygame window. I went onto Control+Alt+Delete task manager and it showed me the desktop. The graphics had gone a bit less realistic and the screen had zoomed in. The only way to get out was to go onto the task manager and end the python task. Then it went back to the normal desktop. I looked at another project : pygame fullscreen mode exit I tried that solution but it didn't work. I kept on trying to find the solution to the strange desktop but couldn't. I'd appreciate some help!

martineau
  • 119,623
  • 25
  • 170
  • 301
Guydangerous99
  • 147
  • 1
  • 12

2 Answers2

2

You get the black screen because you didn't draw anything.

The code is still running in an infinite loop that's why you have to finish it in task manager.

Pressing A does nothing because your code doesn't check it correctly:

if event.type == K_a:

should be

if event.key == pygame.K_a:

Your desktop is weird because you made pygame change your video card to the 800x600 resolution, which is not your native desktop resolution

Everything is correct and the code is behaving just as you wrote it.

I run the code with the change above and it correctly exited when I pressed A

nosklo
  • 217,122
  • 57
  • 293
  • 297
  • Literally writing this in the strange fullscreen mode. That's how you know this didn't work. Sorry! – Guydangerous99 Jul 04 '18 at 18:27
  • 1
    @Guydangerous99 everything seems to be correct. The behavior you describe is exactly how the code you wrote should behave, so there isn't anything that is **not working**. – nosklo Jul 04 '18 at 18:28
  • @Guydangerous99 writing code is dangerous! - there was this guy which wrote a code to mess with monitor frequency and the monitor exploded! – nosklo Jul 04 '18 at 18:32
  • lol, anyway, the guy above solved the problem. At least I know what to do when I troll my friends! ;) – Guydangerous99 Jul 04 '18 at 18:34
  • @Guydangerous99 his answer is exactly the same as mine though, and I answered first - oh well – nosklo Jul 04 '18 at 18:37
  • oh right wait a sec :) Didn't notice you put `pygame.K_a` – Guydangerous99 Jul 04 '18 at 18:38
2

I found multiple problems with your code. Here are some solutions:

The first problem is that you are using event.type where you need to be using event.key.

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.type == pygame.K_a:
            isRunning = False

The second event.type needs to be changed to an event.key.

The second problem is that K_a needs to be prefixed with pygame., like this:

if event.key == pygame.K_a:

Your desktop looked all fuzzy because the PyGame application ran in 800x600 resolution, which is less than most monitors. The PyGame window was still open until you closed it. As long as the window is open, the custom resolution remains.

I hope I've helped you out!

python-b5
  • 73
  • 2
  • 8