1
import pygame
import sys
pygame.init()
window = pygame.display.set_mode([600,600])

playing = True
blue = True
x = 30 # Initial
y = 30 # Position

while playing:
    for event in pygame.event.get():
        if event.type == pygame.quit():
            playing = False

            #The above 4 lines are basically a exit function for the program that you can use in pretty much any pygame program to allow the user to exit the program
            # The below is anything else you want to happen constantly while the program is running
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:  # This program says that if a key is pressed down and that event is K_SPACE (K stands for keyboard and
                #space is basically the key on the keyboard) then the following underneath happens
            blue = not blue # makes blue false, you can also use blue == False which would be a much better way but i'm keeping this this way in case it helps with another
                #program in the future
        pressed = pygame.key.get_pressed() # this gets what key has been pressed so key.get_pressed() get's the key that has been pressed

        if pressed[pygame.K_UP]: y += 3
        if pressed[pygame.K_DOWN]: y -= 3
        if pressed[pygame.K_LEFT]: x -= 3
        if pressed[pygame.K_RIGHT]: x += 3
            
         #these function change the y and x values according to the keys pressed
        if blue:
            colour = (0,128,255) #This changes the colour of the rectangle depending on whether or not blue is true or false as dictated by the space key in the code before the up down mechanism
        else:
            colour = (255,100,0)
        pygame.draw.rect(window,colour, pygame.Rect(x,y),60,60) # this makes it draw a rectangle in the window, of the colour specified and makes the rectangle start at the x and y coordinates and makes the rectangle unable to get any bigger than 60/60 pixels
        pygame.display.flip()


sys.exit()

I have no clue what I have done wrong, any help will be much appreciated - Thanks!

ps. I am learning pygame so all the notes are for my learning process, ignore them.

  • In addition to the answer below (`pygame.quit()` vs `pygame.QUIT`), you need to change your draw function call to `pygame.draw.rect(window,colour, pygame.Rect(x,y,60,60))`. You will probably also want to fill the screen with a background colour every loop, just before the draw, e.g. `window.fill(pygame.Color("black"))`. – import random Jul 19 '21 at 14:06
  • also why do You use `sys.exit()` there is no need for that, You can already use the built-in `exit()` that will do the same just kinda better for the process, it also means that You can remove an extra line from Your imports making it more compact and stuff – Matiiss Jul 19 '21 at 14:14

1 Answers1

2

The mistake you made is accidently calling pygame.quit() instaed of refrencing the pygame.QUIT cosntant in your event loop. Basically change this: "if event.type == pygame.quit()" to this: if event.type == pygame.QUIT

CryptoNoob
  • 171
  • 5