-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_game_pyglet.py
148 lines (120 loc) · 3.9 KB
/
simple_game_pyglet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import pyglet
from pyglet.window import key
import random
import copy
""" Ideas
Beep: print('\a')
PNGs: https://www.pngall.com/
"""
window = pyglet.window.Window()
global_score = 0
global_move_speed = 4.5
global_asteroid_speed = 3
global_shooting_speed = 0.3
# let's use set instead of list. set can have only one unique value in the same time.
list_of_keys = set()
list_of_bullets = []
last_fire = 10
def asteriod_doomed():
global global_score
global global_move_speed
global global_asteroid_speed
asteroid.y = window.height
asteroid.x = random.randint(0, window.width)
global_score += 1
global_asteroid_speed += 1
global_move_speed += 1
label.text = f'Score: {global_score}'
beep1.play()
def tick_bullet(t):
global list_of_bullets
global last_fire
global global_shooting_speed
if key.SPACE in list_of_keys and last_fire > global_shooting_speed:
new_strela = pyglet.text.Label('l', font_size = 20)
new_strela.x = ship.x + ship.width / 2 - 2
new_strela.y = ship.y + ship.height - 10
new_strela.visible = True
list_of_bullets.append(new_strela)
last_fire = 0
else:
last_fire += t
for bullet in list_of_bullets:
# Height of the bullet is not available thus using constant
if bullet.y + 20 >= asteroid.y and \
bullet.x > asteroid.x and \
bullet.x < asteroid.x + asteroid.width:
asteriod_doomed()
list_of_bullets.remove(bullet)
def tick(t):
global global_score
global global_move_speed
global global_asteroid_speed
asteroid.y -= global_asteroid_speed
for bullet in list_of_bullets:
bullet.y += 10
if bullet.y > window.height:
list_of_bullets.remove(bullet)
if key.A in list_of_keys:
ship.x -= global_move_speed
if key.D in list_of_keys:
ship.x += global_move_speed
if ship.x < 0:
ship.x = window.width - ship.width
if ship.x + ship.width > window.width:
ship.x = 1
if detect_touching(ship, asteroid):
asteriod_doomed()
if asteroid.y <= 0:
asteroid.y = window.height
asteroid.x = random.randint(0, window.width)
global_score = 0
global_move_speed = 4.5
global_asteroid_speed = 3
label.text = f'Score: {global_score}'
beep2.play()
def detect_touching(sprite1, sprite2):
return sprite1.x + sprite1.width >= sprite2.x \
and sprite1.x <= sprite2.x + sprite2.width \
and sprite1.y + sprite1.height >= sprite2.y \
and sprite1.y <= sprite2.y + sprite2.height
def zpracuj_stisk_klavesy(symbol, modifier):
global list_of_keys
list_of_keys.add(symbol)
def zpracuj_pusteni_klavesy(symbol, modifier):
global list_of_keys
list_of_keys.discard(symbol)
def klik(x, y, tlacitko, mod):
pass
def vykresli():
window.clear()
background_img.blit(0, 0)
label.draw()
ship.draw()
asteroid.draw()
for bullet in list_of_bullets:
bullet.draw()
window.push_handlers(
on_key_press=zpracuj_stisk_klavesy,
on_key_release=zpracuj_pusteni_klavesy,
on_draw=vykresli,
on_mouse_press=klik,
)
spaceship_img = pyglet.image.load('spaceship.png')
asteroind_img = pyglet.image.load('asteroid.png')
background_img = pyglet.image.load('background.png')
ship = pyglet.sprite.Sprite(spaceship_img)
asteroid = pyglet.sprite.Sprite(asteroind_img)
beep1 = pyglet.resource.media('beep-07a.wav')
beep2 = pyglet.resource.media('beep-10.wav')
# Very start of the program, initialization
ship.x = window.width / 2
ship.scale = 0.2
asteroid.scale = 0.3
label = pyglet.text.Label(f'Score: {global_score}',
font_name='Times New Roman',
font_size=36,
x=0, y=window.height-36)
pyglet.clock.schedule_interval(tick, 1/30)
pyglet.clock.schedule_interval(tick_bullet, 1/30)
pyglet.app.run()