Skip to content

Commit d0b2430

Browse files
committed
Merge branch 'master' of github.com:CVUT-FS-12110/Python-for-Scientific-Computations-and-Control
2 parents 72c413a + a361235 commit d0b2430

File tree

6 files changed

+301
-194
lines changed

6 files changed

+301
-194
lines changed

course-E375004.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
6. **Simple 2D game using pygame**
4646

4747
- Source code: [rocket game](courses/E375004/pygame)
48-
- task: TBA
48+
- task: Continue on our rocket pygame implementation, located [here](courses/E375004/pygame)
4949
- responsible person: adam.peichl@fs.cvut.cz
5050

5151
7. **Control simulation**

courses/E375004/pygame/langtons_ant.py

Lines changed: 0 additions & 102 deletions
This file was deleted.

courses/E375004/pygame/pygame_bouncy_balls.py

Lines changed: 0 additions & 91 deletions
This file was deleted.

courses/E375004/pygame/rocket.png

18.9 KB
Loading

courses/E375004/pygame/rocket.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import pygame
2+
3+
from utils import boundaries
4+
from utils import box
5+
from utils import box_vendor
6+
7+
pygame.init()
8+
9+
# BASIC COLOURS (sometimes, it's usefull to define them as constants)
10+
black = (0,0,0)
11+
white = (255,255,255)
12+
red = (255,0,0)
13+
14+
# FONTS
15+
pygame.font.init()
16+
myfont = pygame.font.SysFont('Arial',50)
17+
18+
# IMPORTANT GAME CONSTANTS
19+
WIDTH = 800 # display widht
20+
HEIGHT = 600 # display height
21+
TICK = 100 # clock tick
22+
23+
GameDisplay = pygame.display.set_mode((WIDTH, HEIGHT))
24+
pygame.display.set_caption('Space Rocket')
25+
clock = pygame.time.Clock()
26+
27+
# LOADING OUR IMAGE OF ROCKET AND RESIZING IT
28+
RESIZED = 300 # resized pixels of rocket img
29+
30+
rocketIMG = pygame.image.load('rocket.png')
31+
rocketIMG = pygame.transform.scale(rocketIMG,(RESIZED, RESIZED))
32+
33+
# INITIALIZATION AND CALCULATION OF IMPORTANT VARIABLES
34+
# POSITION OF A ROCKET
35+
x = int((WIDTH - RESIZED)/2) # initial coordinate x
36+
y = int(HEIGHT - RESIZED + 50) # initial coordinate y + 50 pixels because of "good looking" grey trail
37+
x_change = 0 # change of coordinate x in the beggining
38+
39+
# BOX VENDOR
40+
SPEED = 1
41+
VENDOR = box_vendor(
42+
num_boxes = 4,
43+
width = WIDTH,
44+
height = HEIGHT,
45+
a = 60,
46+
padding = 5
47+
)
48+
49+
crashed = False
50+
while not crashed:
51+
for event in pygame.event.get():
52+
# print(event)
53+
if event.type == pygame.QUIT:
54+
crashed = True # if we pressed cross, game will end
55+
56+
if event.type == pygame.KEYDOWN:
57+
if event.key == pygame.K_LEFT:
58+
x_change = -5
59+
elif event.key == pygame.K_RIGHT:
60+
x_change = 5
61+
62+
if event.type == pygame.KEYUP:
63+
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
64+
x_change = 0
65+
66+
67+
68+
69+
x = boundaries(x, x_change, WIDTH, RESIZED) # updatting position of a rocket (axis x)
70+
rocket_hitbox_x = (
71+
int(x + RESIZED / 2) - 25,
72+
int(x + RESIZED / 2) + 25
73+
) # calculating new rocket hitbox x
74+
rocket_hitbox_y = (
75+
y + 10,
76+
y + 170
77+
) # calculating new rocket hitbox y
78+
79+
# moving boxes with SPEED
80+
VENDOR.move_all(0, SPEED)
81+
VENDOR.update()
82+
83+
if VENDOR.colision(rocket_hitbox_x, rocket_hitbox_y):
84+
print('Colision!')
85+
86+
# GRAPHIC
87+
# fill screen with white colour
88+
GameDisplay.fill(white)
89+
90+
# boxes
91+
VENDOR.draw(GameDisplay)
92+
93+
# hitbox of a rocket
94+
# pygame.draw.rect(
95+
# GameDisplay,
96+
# red,
97+
# pygame.Rect((
98+
# rocket_hitbox_x[0],
99+
# rocket_hitbox_y[0],
100+
# rocket_hitbox_x[1] - rocket_hitbox_x[0],
101+
# rocket_hitbox_y[1] - rocket_hitbox_y[0]
102+
103+
# ))
104+
# )
105+
106+
# show image of rocket in position x,y
107+
GameDisplay.blit(rocketIMG,(x,y))
108+
109+
pygame.display.update()
110+
clock.tick(TICK)
111+
112+
pygame.quit()

0 commit comments

Comments
 (0)