-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcandy3.py
72 lines (60 loc) · 1.75 KB
/
candy3.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
import random
# The size of the board in tiles
TILESW = 14
TILESH = 14
# The pixel size of the screen
WIDTH = TILESW * 40
HEIGHT = TILESH * 40
TITLE = 'Candy Crush'
last_dt = 0
cursor = Actor('selected', topleft=(0,0))
board = []
for row in range(TILESH):
# Make a list of 10 random tiles
tiles = [random.randint(1,8) for _ in range(TILESW)]
board.append(tiles)
def draw():
screen.clear()
for y in range(TILESH):
for x in range(TILESW):
tile = board[y][x]
if tile:
screen.blit(str(tile), (x * 40, y * 40))
cursor.draw()
def cursor_tile_pos():
return (int(cursor.x // 40)-1, int(cursor.y // 40))
def drop_tiles(x,y):
# Loop backwards through the rows from x,y to the top
for row in range(y,0,-1):
# Copy the tile above down
board[row][x] = board[row-1][x]
# Finally blank the tile at the top
board[0][x] = None
def check_matches():
for y in range(TILESH):
for x in range(TILESW-1):
if board[y][x] == board[y][x+1]:
board[y][x] = None
board[y][x+1] = None
def check_gaps():
# Work from the bottom up
for y in range(TILESH-1,-1,-1):
for x in range(TILESW):
if board[y][x] is None:
drop_tiles(x,y)
def on_key_up(key):
x, y = cursor_tile_pos()
if key == keys.LEFT and x > 0:
cursor.x -= 40
if key == keys.RIGHT and x < TILESW-2:
cursor.x += 40
if key == keys.UP and y > 0:
cursor.y -= 40
if key == keys.DOWN and y < TILESH-1:
cursor.y += 40
if key == keys.SPACE:
board[y][x], board[y][x+1] = board[y][x+1], board[y][x]
def every_second():
check_matches()
check_gaps()
clock.schedule_interval(every_second, 1.0)