-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSPGL_Demo2.py
193 lines (151 loc) · 5.57 KB
/
SPGL_Demo2.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# SPGL Game Demo 2 by /u/wynand1004 AKA @TokyoEdTech
# Requires SPGL Version 0.8
# SPGL Documentation on Github: https://wynand1004.github.io/SPGL
# This Demo includes a simple particle system
#
# How to Play
# Navigate using the arrow keys
# Green objects are worth 10 points
# Yellow objects are worth 5 points
# Red objects are worth -10 points
#Import SPGL
import spgl
import random
# Create Classes
class Player(spgl.Sprite):
def __init__(self, shape, color, x, y):
spgl.Sprite.__init__(self, shape, color, x, y)
self.speed = 3
self.score = 0
def tick(self):
self.move()
def move(self):
self.fd(self.speed)
if self.xcor() > game.SCREEN_WIDTH / 2:
self.goto(-game.SCREEN_WIDTH / 2, self.ycor())
if self.xcor() < -game.SCREEN_WIDTH /2 :
self.goto(game.SCREEN_WIDTH / 2, self.ycor())
if self.ycor() > game.SCREEN_HEIGHT / 2:
self.goto(self.xcor(), -game.SCREEN_HEIGHT / 2)
if self.ycor() < -game.SCREEN_HEIGHT / 2:
self.goto(self.xcor(), game.SCREEN_HEIGHT / 2)
def rotate_left(self):
self.lt(30)
def rotate_right(self):
self.rt(30)
def accelerate(self):
self.speed += 1
def decelerate(self):
self.speed -= 1
if self.speed < 0:
self.speed = 0
class Orb(spgl.Sprite):
def __init__(self, shape, color, x, y):
spgl.Sprite.__init__(self, shape, color, x, y)
self.speed = 2
self.setheading(random.randint(0,360))
self.turn = 0
def tick(self):
self.move()
if random.randint(0, 100) < 5:
self.clear()
def move(self):
self.rt(random.randint(-10, 10))
self.fd(self.speed)
if self.xcor() > game.SCREEN_WIDTH / 2:
self.goto(-game.SCREEN_WIDTH / 2, self.ycor())
if self.xcor() < -game.SCREEN_WIDTH / 2:
self.goto(game.SCREEN_WIDTH / 2, self.ycor())
if self.ycor() > game.SCREEN_HEIGHT / 2:
self.goto(self.xcor(), -game.SCREEN_HEIGHT / 2)
if self.ycor() < -game.SCREEN_HEIGHT / 2:
self.goto(self.xcor(), game.SCREEN_HEIGHT / 2)
class Particle(spgl.Sprite):
def __init__(self, spriteshape, color):
spgl.Sprite.__init__(self, shape, color, 1000, 1000)
self.shapesize(stretch_wid=0.1, stretch_len=0.1, outline=None)
self.goto(-1000,-1000)
self.frame = 0.0
self.max_frame = random.randint(5, 20)
def tick(self):
if self.frame != 0:
self.fd(self.myspeed)
self.frame += 1
if self.frame > self.max_frame:
self.goto(1000, 1000)
self.frame = 0
def explode(self, startx, starty):
self.goto(startx,starty)
self.setheading(random.randint(0,360))
self.frame = 1.0
self.myspeed = random.randint(3, 10)
class Explosion(object):
def __init__(self):
self.particles = []
for _ in range(30):
color = random.choice(["red", "yellow", "orange"])
self.particles.append(Particle("circle", color))
def explode(self, x, y):
for particle in self.particles:
particle.explode(x, y)
# Initial Game setup
game = spgl.Game(800, 600, "black", "SPGL Game Demo by /u/wynand1004 AKA @TokyoEdTech", 0)
# Game attributes
game.highscore = 0
# Load high score
highscore = game.load_data("highscore")
if highscore:
game.highscore = highscore
else:
game.highscore = 0
# Create Sprites
# Create Player
player = Player("triangle", "white", -400, 0)
# Create Orbs
for i in range(100):
color = random.choice(["red", "yellow", "green"])
shape = random.choice(["circle", "square", "triangle", "arrow"])
orb = Orb(shape, color, 0, 0)
speed = random.randint(1, 5)
orb.speed = speed
# Create Explosion
explosion = Explosion()
# Create Labels
score_label = spgl.Label("Score: 0 Highscore: {}".format(game.highscore), "white", -380, 280)
# Create Buttons
# Set Keyboard Bindings
game.set_keyboard_binding(spgl.KEY_UP, player.accelerate)
game.set_keyboard_binding(spgl.KEY_DOWN, player.decelerate)
game.set_keyboard_binding(spgl.KEY_LEFT, player.rotate_left)
game.set_keyboard_binding(spgl.KEY_RIGHT, player.rotate_right)
game.set_keyboard_binding(spgl.KEY_ESCAPE, game.exit)
while True:
# Call the game tick method
game.tick()
# Put your game logic here
for sprite in game.sprites:
# Check collisions with Orbs
if sprite.state and isinstance(sprite, Orb):
if game.is_collision(sprite, player):
game.play_sound("collision.wav")
middle_x = (sprite.xcor() + player.xcor()) / 2
middle_y = (sprite.ycor() + player.ycor()) / 2
explosion.explode(middle_x, middle_y)
sprite.destroy()
# Update Score
if sprite.pencolor() == "red":
player.score -= 10
if sprite.pencolor() == "green":
player.score += 10
if sprite.pencolor() == "yellow":
player.score += 5
# Update High Score
if player.score > game.highscore:
game.highscore = player.score
game.save_data("highscore", game.highscore)
# Update the Game Score, High Score, and Player Speed
speed_string = "-" * int(player.speed)
score_label.update("Score: {} High Score: {} Speed: {}".format(player.score, game.highscore, speed_string))
# Show game info in terminal
game.clear_terminal_screen()
game.print_game_info()