This repository has been archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
sprites.py
323 lines (267 loc) · 10.9 KB
/
sprites.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import pygame as pg
from collections import deque
import pytmx
import os, sys
is_frozen = getattr(sys, 'frozen', False)
frozen_temp_path = getattr(sys, '_MEIPASS', '')
# This is needed to find resources when using pyinstaller
if is_frozen:
basedir = frozen_temp_path
else:
basedir = os.path.dirname(os.path.abspath(__file__))
def getfilepath(fname):
return basedir + "/resources/" + fname
# define some colors (R, G, B)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
DARKGREY = (40, 40, 40)
LIGHTGREY = (100, 100, 100)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
# game settings
WIDTH = 1024 # 16 * 64 or 32 * 32 or 64 * 16
HEIGHT = 768 # 16 * 48 or 32 * 24 or 64 * 12
FPS = 60
TITLE = "quantum freeze block"
BGCOLOR = DARKGREY
TILESIZE = 64
GRIDWIDTH = WIDTH / TILESIZE
GRIDHEIGHT = HEIGHT / TILESIZE
class Circuit(object):
def __init__(self):
self.qubit_operation=[0,0,0] # used to build up the circuit
class TiledMap:
def __init__(self, filename):
tm = pytmx.load_pygame(filename, pixelalpha=True)
self.width = tm.width * tm.tilewidth
self.height = tm.height * tm.tileheight
self.tmxdata = tm
def render(self, surface):
ti = self.tmxdata.get_tile_image_by_gid
for layer in self.tmxdata.visible_layers:
if isinstance(layer, pytmx.TiledTileLayer):
for x, y, gid, in layer:
tile = ti(gid)
if tile:
surface.blit(tile, (x * self.tmxdata.tilewidth,
y * self.tmxdata.tileheight))
def make_map(self):
temp_surface = pg.Surface((self.width, self.height))
self.render(temp_surface)
return temp_surface
class Player(pg.sprite.Sprite):
# sprite for play button and penguin
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.players
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.image.load(getfilepath('penguin.png'))
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
self.rect = self.image.get_rect()
self.x = x
self.dx = 0
self.y = y
self.dy = 0
self.xorj, self.yorj = x, y
self.last_update = pg.time.get_ticks()
self.off_lake = 0 # controls is on lake= 0 if yes?
self.win = False
self.moving = False # used by the update loop to know what objects to move?
self.position_targets = deque()
self.load = False
self.dots = 0
def update(self):
self.x += self.dx
self.y += self.dy
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
def add_target(self, x, y):
self.position_targets.append((x+self.xorj, y+self.yorj))
def add_target_walk(self, targets):
for target in targets:
self.add_target(*target)
def kill(self):
pg.sprite.Sprite.kill(self)
class Play_button(pg.sprite.Sprite):
# sprite for play button and penguin
def __init__(self, game, x, y):
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.image.load(getfilepath('play_but.png'))
half_size= int(1.5*TILESIZE)
self.image = pg.transform.scale(self.image,(half_size,half_size))
self.rect = self.image.get_rect()
self.x = x
self.y = y
def update(self):
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
def kill(self):
pg.sprite.Sprite.kill(self)
class Gaps(pg.sprite.Sprite):
def __init__(self,game,x,y,id):
self.groups = game.all_sprites, game.gaps_group
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.image.load(getfilepath('gap_gate.png'))
self.image = pg.transform.scale(self.image, ( TILESIZE, TILESIZE))
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.clicked = False # controls if clicked
self.game = game
self.id = id
def update(self):
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
def kill(self):
pg.sprite.Sprite.kill(self)
class Gates(pg.sprite.Sprite):
# sprite for the gates
def __init__(self,game,type,x,y):
# load gates according to their type
if type=="I":
self.groups = game.all_sprites, game.Igroup, game.gate_group
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.image.load(getfilepath('Igate.png'))
self.image = pg.transform.scale(self.image, ( TILESIZE, TILESIZE))
self.rect = self.image.get_rect()
self.type = "I"
elif type=="X":
self.groups = game.all_sprites, game.Xgroup, game.gate_group
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.image.load(getfilepath('X_gate.png'))
self.image = pg.transform.scale(self.image, ( TILESIZE, TILESIZE))
self.rect = self.image.get_rect()
self.type = "X"
elif type=="H":
self.groups = game.all_sprites, game.Hgroup, game.gate_group
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.image.load(getfilepath('H_gate.png'))
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
self.rect = self.image.get_rect()
self.type = "H"
elif type=="K":
self.groups = game.all_sprites, game.Hgroup, game.gate_group
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.image.load(getfilepath('CX_gate.png'))
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
self.rect = self.image.get_rect()
self.type = "K"
self.clicked = False
self.click_time = 0
self.game = game
self.x = x
self.y = y
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
def update(self): # controls movement along wire
if self.clicked == True:
#print("hello")
self.image = pg.Surface((TILESIZE, TILESIZE ))
self.image.fill(WHITE)
class Qubits(pg.sprite.Sprite):
# Sprite for the moving qubit
def __init__(self,game,number,x,y):
self.groups = game.all_sprites, game.all_qubits
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.image.load(getfilepath('tesseract.png'))
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
#self.image = pg.transform.rotate(self.image,45)
self.rect = self.image.get_rect()
self.speedx = 0
self.number = number
self.x = x
self.y = y
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
self.end= False
self.step=0
def update(self): # controls movement along wire
self.rect.x += self.speedx
if self.rect.left>WIDTH-128:
self.speedx = 0
self.end = True
class circ(pg.sprite.Sprite): #wires along which qubits move
def __init__(self, game, x, y):
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.image.load(getfilepath('circuit background.png'))
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.bottomright = (WIDTH,HEIGHT)
class Wire(pg.sprite.Sprite): #wires along which qubits move
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.wires
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.image.load(getfilepath('wire.png'))
wire_width = int(TILESIZE/8)
self.image = pg.transform.scale(self.image,(TILESIZE,wire_width))
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x * TILESIZE
self.rect.y = y * TILESIZE+28
class Background2(pg.sprite.Sprite): #background tiles for frozen lake
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.backgrounds
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.image.load(getfilepath('ice_tile.png'))
self.image = pg.transform.scale(self.image, (2*TILESIZE, 2*TILESIZE))
self.rect = self.image.get_rect()
#self.image = pg.Surface((TILESIZE, TILESIZE ))
#self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x * TILESIZE
self.rect.y = y * TILESIZE
class Background(pg.sprite.Sprite): #background tiles for frozen lake
def __init__(self, game, x, y, w, h):
self.groups = game.all_sprites, game.backgrounds
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.rect = pg.Rect(x,y,w,h)
self.image = pg.image.load(getfilepath('ice_tile.png'))
#self.image = pg.Surface((TILESIZE, TILESIZE ))
#self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x
self.rect.y = y
class Hole(pg.sprite.Sprite): # background tiles for frozen lake
def __init__(self, game, x, y):
self.groups = game.holes
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.image.load(getfilepath('icy hole.png'))
self.image = pg.transform.scale(self.image, (2 * TILESIZE, 2 * TILESIZE))
self.rect = self.image.get_rect()
# self.image = pg.Surface((TILESIZE, TILESIZE ))
# self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x * TILESIZE - TILESIZE
self.rect.y = y * TILESIZE
self.hit = False
class Igloo(pg.sprite.Sprite): # victory endpoints
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.backgrounds, game.igloos
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.image.load(getfilepath('igloo.png'))
height_img = int(1.5*TILESIZE)
self.image = pg.transform.scale(self.image, (2*TILESIZE-16, height_img))
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x * TILESIZE
self.rect.y = y * TILESIZE
self.win = False