×

Pygame Basics Reference Guide for CSUKs Pygame Implementation

Introduction to Pygame

Pygame is a set of Python modules designed for writing video games. It includes graphics and sound libraries designed to be used with the Python programming language. In CSUK's implementation, a subset of Pygame functionality is available for creating simple graphical applications directly in the browser.

Setting Up the Pygame Environment

Initialise Pygame and set up the display window:

import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))

This code initialises Pygame and sets up a window of size 640x480 pixels.

Handling the Game Loop

A game loop is essential for keeping your application running and updating. Here's how you can implement a basic game loop in CSUK's Pygame:

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

This loop will keep running until the user hits the Esc key.

Drawing Shapes

You can draw basic shapes like rectangles, circles, and lines.

Drawing a Rectangle

pygame.draw.rect(screen, (255, 0, 0), (50, 50, 150, 100))

This draws a red rectangle at position (50, 50) with width 150 and height 100.

[Add image of the rectangle here]

Drawing a Circle

pygame.draw.circle(screen, (0, 255, 0), (320, 240), 75)

This draws a green circle centered at (320, 240) with a radius of 75.

[Add image of the circle here]

Drawing a Line

pygame.draw.line(screen, (0, 0, 255), (0, 0), (640, 480), 5)

This draws a blue line from the top-left corner to the bottom-right corner with a thickness of 5 pixels.

[Add image of the line here]

Updating the Display

After drawing, you need to update the display to reflect the changes:

pygame.display.flip()

This updates the entire display.

Filling the Screen

You can fill the screen with a solid colour:

screen.fill((255, 255, 255))  # Fills the screen with white color

Handling Keyboard Input

You can handle keyboard events to make interactive applications.

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            running = False

This code checks if the Escape key is pressed to exit the application.

Handling Mouse Input

You can detect mouse clicks and movements.

for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:
        pos = pygame.mouse.get_pos()
        print("Mouse clicked at:", pos)

This prints the position where the mouse was clicked.

Animating Objects

You can create animations by updating object positions in each frame.

x = 0
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, (255, 0, 0), (x, 200, 50, 50))
    pygame.display.flip()
    pygame.time.wait(1)
    x += 1

This code moves a red square across the screen horizontally.

[Add image sequence or description of the animation here]

Congratulations! You've covered the fundamentals of programming CSUK's Pygame module.

Scroll to Top