А very simple game¶
For this project, let us try to make one very simple game.
Images that we are going to use for our simple game.




Loading and showing the rocket¶
As the first step, just load the image of the rocket and show it on the screen.
1
import pygame as pg
2
import pygamebg as pgbg
3
4
(width, height) = (800, 500)
5
window = pgbg.open_window(width, height, "Rocket")
6
7
# clear screen
8
window.fill(pg.Color("black"))
9
10
# load image
11
???
12
13
# draw rocket
14
???
15
pgbg.wait_loop()
16
(rocket1)
Show the rocket on the bottom of the screen¶
Now adapt the program so that the rocket is shown in the bottom of the screen.
16
1
import pygame as pg
2
import pygamebg as pgbg
3
4
(width, height) = (800, 500)
5
window = pgbg.open_window(width, height, "Rocket")
6
7
# clear screen
8
window.fill(pg.Color("black"))
9
10
# load image
11
rocket = pg.image.load("rocket.png")
12
13
# draw rocket
14
window.blit(rocket, (0, ???))
15
pgbg.wait_loop()
16
(rocket2)
Draw the planets¶
Let us draw a row of planets at the top of the screen. Hint: use a loop to make the code shorter.
20
1
import pygame as pg
2
import pygamebg as pgbg
3
4
(width, height) = (800, 500)
5
window = pgbg.open_window(width, height, "Rocket")
6
7
# load images
8
rocket = pg.image.load("rocket.png")
9
planet = pg.image.load("planet.png")
10
11
# clear screen
12
window.fill(pg.Color("black"))
13
14
# draw rocket
15
window.blit(rocket, (0, height - rocket.get_height()))
16
17
# draw planets
18
???
19
pgbg.wait_loop()
20
(rocket3)
Animate the martian¶
Let us now animate the martian that moves under the planets, from the left to the right of the screen and back.
36
1
import pygame as pg
2
import pygamebg as pgbg
3
4
(width, height) = (800, 500)
5
window = pgbg.open_window(width, height, "Rocket")
6
7
# load images
8
rocket = pg.image.load("rocket.png")
9
planet = pg.image.load("planet.png")
10
martian = pg.image.load("martian.png")
11
12
# martian parameters - horizontal position
13
martian_x = 0
14
15
# draw game objects
16
def draw():
17
# these variables are changed during animation
18
global martian_x
19
20
# clear screen
21
window.fill(pg.Color("black"))
22
23
# draw rocket
24
window.blit(rocket, (0, height - rocket.get_height()))
25
26
# draw planets
27
for i in range(8):
28
window.blit(planet, (100*i, 0))
29
30
# draw martian
31
???
32
33
# move martian
34
???
35
pgbg.frame_loop(100, draw)
36
(rocket4)
React to user commands¶
Now enable moving the rocket to the left or to the right using the two arrow keys.
54
1
import pygame as pg
2
import pygamebg as pgbg
3
4
(width, height) = (800, 500)
5
window = pgbg.open_window(width, height, "Rocket")
6
7
pg.key.set_repeat(10,10)
8
9
# load images
10
rocket = pg.image.load("rocket.png")
11
martian = pg.image.load("martian.png")
12
planet = pg.image.load("planet.png")
13
14
# martian parameters - position and speed
15
martian_x = 0
16
martian_y = planet.get_height()
17
martian_speed = 3
18
19
# rocket position
20
???
21
22
# handle events
23
def keydown(e):
24
# variable that is changed
25
global rocket_x
26
# right and left arrow moves the rocket by 5 pixels
27
if e.key == pg.K_RIGHT:
28
???
29
30
# draw game objects
31
def draw():
32
# variables that are changed in this function
33
global martian_x, martian_speed
34
35
# clear screen
36
window.fill(pg.Color("black"))
37
38
# draw planets
39
for i in range(8):
40
window.blit(planet, (100*i, 0))
41
42
# draw martian
43
window.blit(martian, (martian_x, martian_y))
44
45
# draw rocket
46
???
47
48
# move martian
49
martian_x += martian_speed
50
# change direction if it falls of screen
51
if martian_x < 0 or martian_x > width - martian.get_width():
52
martian_speed = -martian_speed
53
pgbg.frame_loop(100, draw, {pg.KEYDOWN: keydown})
54
(rocket5)