-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSPGL_Demo_Import_Into_Namespace.py
149 lines (117 loc) · 4.22 KB
/
SPGL_Demo_Import_Into_Namespace.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
# SPGL Game Demo by /u/wynand1004 AKA @TokyoEdTech
# Requires SPGL Version 0.8
# SPGL Documentation on Github: https://wynand1004.github.io/SPGL
#
# 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
from spgl import *
import random
# Create Classes
class Player(Sprite):
def __init__(self, shape, color, x, y):
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(Sprite):
def __init__(self, shape, color, x, y):
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)
# Initial Game setup
game = Game(800, 600, "black", "SPGL Game Demo by /u/wynand1004 AKA @TokyoEdTech", 5)
# 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 Labels
score_label = Label("Score: 0 Highscore: {}".format(game.highscore), "white", -380, 280)
# Create Buttons
# Set Keyboard Bindings
game.set_keyboard_binding(KEY_UP, player.accelerate)
game.set_keyboard_binding(KEY_DOWN, player.decelerate)
game.set_keyboard_binding(KEY_LEFT, player.rotate_left)
game.set_keyboard_binding(KEY_RIGHT, player.rotate_right)
game.set_keyboard_binding(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")
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()