-
Notifications
You must be signed in to change notification settings - Fork 0
/
thegame.py
120 lines (99 loc) · 4.39 KB
/
thegame.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
import sys
import pygame
import twozerofoureight as tzfe
if __name__ == "__main__":
# Parse the grid dimension from the command line
# And use that to create a game window to
# house that size of the grid. Here we assume 4
grid_size = 4
# Create a game of that dimension
game = tzfe.TwoZeroFourEight(grid_size)
# If we paint a rectangle cell of size 64 pixels
# Also include a header height to display score
header_height = 48
tile_size = 96
width, height = tile_size * grid_size, tile_size * grid_size + header_height
# Initialize pygame
pygame.init()
# Create a screen
screen = pygame.display.set_mode((width, height))
screen_rect = screen.get_rect()
# Create a font object
font = pygame.font.Font(None, 24)
while True:
game_over = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN:
# Now figure out what key it is
# run the game
if event.key == pygame.K_LEFT:
game.slide_left()
game_over, new_tile = game.insert_new_tile()
elif event.key == pygame.K_UP:
game.slide_up()
game_over, new_tile = game.insert_new_tile()
elif event.key == pygame.K_RIGHT:
game.slide_right()
game_over, new_tile = game.insert_new_tile()
elif event.key == pygame.K_DOWN:
game.slide_down()
game_over, new_tile = game.insert_new_tile()
# Some kinda gray
kinda_gray = (150,150,150)
# Kinda orange
kinda_orange = dict()
kinda_orange[0] = (238, 228, 218, 0.35)
kinda_orange[2] = (238, 228, 218)
kinda_orange[4] = (237, 224, 200)
kinda_orange[8] = (242, 177, 121)
kinda_orange[16] = (245, 149, 99)
kinda_orange[32] = (246, 124, 95)
kinda_orange[64] = (246, 94, 59)
kinda_orange[128] = (237, 207, 114)
kinda_orange[256] = (237, 204, 97)
kinda_orange[512] = (237, 200, 80)
kinda_orange[1024] = (237, 197, 63)
kinda_orange[2048] = (237, 194, 46)
# Blinding_white
blinding_white = (255, 255, 255)
# Blacky black
blacky_black = (0, 0, 0)
# Chocolate_brown
chocolate_brown = (119, 110, 101)
# Booooo Red
booooo_red = (200, 25, 25)
# Fill the screen
screen.fill(kinda_gray)
# Score string
score_string = "Score: {0:0>-06d}".format(game.get_score())
# Write out score
score_surface = font.render(score_string, True, blacky_black)
score_rect = score_surface.get_rect()
score_rect.topright = screen_rect.topright
screen.blit(score_surface, score_rect)
# Now draw the game onto the screen
for i in range(grid_size):
for j in range(grid_size):
tile_top = i * tile_size + header_height
tile_left = j * tile_size
tile_value = game.get_tile_value(i, j)
tile_rect = pygame.draw.rect(screen, blacky_black, (tile_left, tile_top, tile_size, tile_size), 1)
screen.fill(kinda_orange[tile_value], tile_rect)
if tile_value > 0:
# draw a square tile
tile_value_string = "{0:^4d}".format(tile_value)
tile_value_surface = font.render(tile_value_string, True, chocolate_brown)
tile_value_surface_rect = tile_value_surface.get_rect()
tile_value_surface_rect.center = tile_rect.center
screen.blit(tile_value_surface, tile_value_surface_rect)
# check game over and paint it to top left in RED
if game_over:
game_over_surface = font.render("Game Over", True, booooo_red)
game_over_rect = game_over_surface.get_rect()
game_over_rect.topleft = screen_rect.topleft
screen.blit(game_over_surface, game_over_rect)
print("Game Over")
# Update the display
pygame.display.update()