Mingming Li โ Welcome! Click any heading below to expand the content.
There are many ways to write and run Python code. In this course, we will use Thonny (great for beginners).
We will write, understand, and modify this code. It produces a simple jumping game.
โฌ๏ธ Download game-simple.py
1 import pygame
2 pygame.init()
3 screen = pygame.display.set_mode((800, 400))
4 clock = pygame.time.Clock()
5
6 player = pygame.Rect(80, 300, 40, 40)
7 cactus = pygame.Rect(800, 300, 30, 40)
8 gravity = 0
9
10 while True:
11 for event in pygame.event.get():
12 if event.type == pygame.QUIT:
13 pygame.quit()
14 exit()
15
16 keys = pygame.key.get_pressed()
17 if keys[pygame.K_SPACE] and player.bottom >= 400:
18 gravity = -20
19
20 gravity += 1
21 player.y += gravity
22 if player.bottom > 400:
23 player.bottom = 400
24
25 cactus.x -= 5
26 if cactus.x < -50:
27 cactus.x = 900
28
29 screen.fill((255,255,255))
30 pygame.draw.rect(screen, (0,255,0), player)
31 pygame.draw.rect(screen, (255,0,0), cactus)
32 pygame.display.flip()
33 clock.tick(60)
cactus.x -= 5 to cactus.x -= 10 โ What happens to the speed?gravity = -20 to gravity = -30 โ How does the jump change?player = pygame.Rect(80, 300, 40, 40) width to 60 โ Does the game become easier or harder?(800, 400) to (1000, 500) โ What happens?Write down your observations! Experimenting is the best way to learn.
Pygame is a Python package for making games.
Key concepts: Game Loop, Events, Rectangles, Coordinate System, Collision Detection
You also need to download these two images:
โฌ๏ธ Download player.png โฌ๏ธ Download cactus.pngMake sure to put the code and images in the same folder.
1 import pygame
2 from sys import exit
3 from random import randint
4
5 pygame.init()
6 screen = pygame.display.set_mode((800, 400))
7 pygame.display.set_caption('Game')
8 clock = pygame.time.Clock()
9
10 game_active = False
11 gravity = 0
12
13 # Player image
14 player_img = pygame.image.load('player.png').convert_alpha()
15 player_img = pygame.transform.rotozoom(player_img, 0, 0.5)
16 player_rect = player_img.get_rect(midbottom=(80, 400))
17
18 # Cactus image
19 cactus_img = pygame.image.load('cactus.png').convert_alpha()
20 cactus_rect = cactus_img.get_rect(midbottom=(randint(900, 1100), 400))
21
22 while True:
23 # ... event handling ...
24 if game_active:
25 screen.blit(player_img, player_rect)
26 screen.blit(cactus_img, cactus_rect)
27 # ... rest of game loop ...
Note: Code-2 adds a game over screen, start screen, and collision detection!
Instead of one cactus, we use a LIST to store multiple cacti!
โฌ๏ธ Download game-more-cactus.py
22 # List to store active cacti
23 active_cacti = []
24 last_spawn_time = pygame.time.get_ticks()
25
54 # Update all cacti
55 for cactus_rect in active_cacti[:]:
56 cactus_rect.x -= 6
57 if cactus_rect.x <= -100:
58 active_cacti.remove(cactus_rect)
59
60 # Spawn new cactus every 1 second
61 if current_time - last_spawn_time > 1000:
62 last_spawn_time = current_time
63 new_cactus = cactus_img.get_rect(midbottom=(900, 400))
64 active_cacti.append(new_cactus)
Now that you understand the basic game, here are some ways to make it more exciting!
| What | Code |
|---|---|
| Import Pygame | import pygame |
| Initialize | pygame.init() |
| Create window | screen = pygame.display.set_mode((800, 400)) |
| Load image | img = pygame.image.load('file.png').convert_alpha() |
| Draw image | screen.blit(img, rect) |
| Get rectangle | rect = img.get_rect(center=(x,y)) |
| Check key pressed | pygame.key.get_pressed() |
| Rectangle collision | rect1.colliderect(rect2) |
| Draw rectangle | pygame.draw.rect(screen, (R,G,B), rect) |
| Fill screen | screen.fill((R,G,B)) |
| Update display | pygame.display.flip() |
| Control FPS | clock.tick(60) |
| Quit properly | pygame.quit(); exit() |
pygame.display.flip()?pip install pygame in terminal.clock.tick(60). Smaller number = slower.pygame.Rect(80, 300, 40, 40) mean?gravity += 1 do?clock.tick(60)?screen.fill((255,255,255))?pygame.image.load() and .convert_alpha()?active_cacti = []) for multiple cacti?colliderect() checks if two rectangles overlap.convert_alpha() preserves transparency for smoother rendering