Skip to content

Instantly share code, notes, and snippets.

@zillydev
Last active June 28, 2024 14:20
Show Gist options
  • Select an option

  • Save zillydev/190957e0142ef2b7db062a780fe3bb75 to your computer and use it in GitHub Desktop.

Select an option

Save zillydev/190957e0142ef2b7db062a780fe3bb75 to your computer and use it in GitHub Desktop.
Session 1 notes.md
  • screen = pygame.display.set_mode((width, height))
    • This function creates a window surface of a specified size.
    • It takes a tuple as a paramater, which must have the values for the width and height of the window.
    • We store this surface in the screen variable for future use.
    • The window screen is a basically a grid of pixels, onto which we can draw objects using the standard coordinate system of x-axis and y-axis and xy-coordinates.
  • pygame.display.set_caption('SST')
    • Sets the title of the window.
  • while True:
    • An infinite while loop is required because the game needs to constantly fetch inputs, process game logic, and output it on the screen.
  • for event in pygame.event.get():
    • This for loop gets every event that is occurring at the current frame, and loops through them.
  •    if event.type == QUIT:
      	pygame.quit()
      	sys.exit()
    

If there is a QUIT event detected (triggered when the 'Close (X)' button is pressed), quit pygame and exit the program.

  • keys = pygame.key.get_pressed()
    • Gets the keyboard keys that are being pressed in the current frame, and stores them in the keys variable.
    • This is an easier method to process keyboard events.
  • if (keys[pygame.K_w] or keys[pygame.K_UP]) and (y > 0):
    • The first condition checks if the 'W' or up-arrow key is being pressed.
    • The second condition checks if the rectangle's y-coordinate is greater than 0, to prevent it from going out of the boundary of the window.
    • If the above conditions pass, y -= velocity decreases the y-coordinate of the rectangle, which simulates a left movement.
    • The other three conditions are also similar but are for the other three directions.
  • screen.fill((0, 0, 0))
    • Fills the entire screen with a specified color. Takes a color tuple as a parameter.
    • Colors in pygame are stored as three-value tuples, with each value ranging from 0-255, and representing the RGB colors respectively.
    • Filling the screen is required before drawing anything, because the game draws new pixels every frame, while retaining the content from the previous frame.
  • pygame.draw.rect(screen, (255, 0, 0), (500, 500, 100, 100))
    • Draws a rectangle. Takes in three parameters.
      1. The surface to draw on.
      1. A color tuple.
      1. A tuple with the first two values representing the xy-coordinate position of the top-left corner of the rectangle, and the second two values representing the width and height of the rectangle.
  • screen.blit(image, (x, y))
    • Draws a loaded image to the surface, in our case, the image we loaded with pygame.image.load().
    • Takes two parameters, the image to draw, and a tuple representing the xy-coordinate values of the top-left corner of the image.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment