/
a2048.py
170 lines (134 loc) · 5.89 KB
/
a2048.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
import numpy as np
import pygame as pg
from pygame.locals import *
from pygame import gfxdraw
GAME_TITLE = '2048 by Haawron'
up = 0
down = 1
left = 2
right = 3
FPS = 60
WINDOW_WIDTH = 560 # 4의 배수
WINDOW_HEIGHT = 700
PLAIN_MARGIN = 60 # 좌/우/하단 마진, 4의 배수
NUMBLOCK_MARGIN = 10
PLAIN_SIZE = WINDOW_WIDTH - 2 * PLAIN_MARGIN
OBJSIZE = PLAIN_SIZE // 4 - 2 * NUMBLOCK_MARGIN # size of number boxes in pixels
UPPER_MARGIN = WINDOW_HEIGHT - PLAIN_SIZE - PLAIN_MARGIN
WHITE = (0, 0, 0)
COLOR_BG = None
############################################################################
############################### Rounded Rect ###############################
############################################################################
# https://github.com/Mekire/rounded-rects-pygame/blob/master/roundrects/roundrects.py
def aa_round_rect(surface, rect, color, rad=20, border=0, inside=(0,0,0)):
"""
Draw an antialiased rounded rect on the target surface. Alpha is not
supported in this implementation but other than that usage is identical to
round_rect.
"""
rect = pg.Rect(rect)
_aa_render_region(surface, rect, color, rad)
if border:
rect.inflate_ip(-2*border, -2*border)
_aa_render_region(surface, rect, inside, rad)
def _aa_render_region(image, rect, color, rad):
"""Helper function for aa_round_rect."""
corners = rect.inflate(-2*rad-1, -2*rad-1)
for attribute in ("topleft", "topright", "bottomleft", "bottomright"):
x, y = getattr(corners, attribute)
gfxdraw.aacircle(image, x, y, rad, color)
gfxdraw.filled_circle(image, x, y, rad, color)
image.fill(color, rect.inflate(-2*rad,0))
image.fill(color, rect.inflate(0,-2*rad))
############################################################################
############################################################################
############################################################################
class Animator:
def __init__(self):
self.path = []
def move(self, rect, direction=up):
# 진짜 움직이기만 해줌
pass
def calc(self, current, target, time, function='linear'):
'''
current에서 target까지 time만에 갈 수 있도록 계산
time을 FPS에 맞춰 frame으로 변환 후 function에 따라
각 frame에서의 위치를 list로 반환
구간은 0~1로 변환 후 계산 뒤 다시 scaling
Args:
current:
target:
time: animate 하는 데에 걸리는 시간
function: 'linear', ...
:return:
'''
# https://gist.github.com/gre/1650294
easing_functions = {
'linear': lambda x: x,
'easeInQuad': lambda x: x**2,
'easeOutQuad': lambda x: x*(2-x),
'easeInOutQuad': lambda x: np.where(x<.5, 2*x**2, -1+(4-2*x)*x),
'easeInCubic': lambda x: x**3,
'easeOutCubic': lambda x: (x-1)**3+1,
'easeInOutCubic': lambda x: np.where(x<.5, 4*x**3, (x-1)*(2*x-2)**2+1),
'easeInQuart': lambda x: x**4,
'easeOutQuart': lambda x: 1-(x-1)**4,
'easeInOutQuart': lambda x: np.where(x<.5, 8*x**4, 1-8*(x-1)**4),
'easeInQuint': lambda x: x**5,
'easeOutQuint': lambda x: 1+(x-1)**5,
'easeInOutQuint': lambda x: np.where(x<.5, 16*x**5, 1+16*(x-1)**5)
}
d = target - current # 음수여도 노상관
frames = time * FPS
x = np.linspace(0, 1, frames)
y = easing_functions[function](x)
y = d * y + current
self.path = y
class NumBlock(pg.sprite.Sprite):
colors = [
(0, 0, 0), (1, 2, 3)
]
def __init__(self, num=2, loc=(0, 0)):
'''
Args:
num:
loc: 생성되는 블럭의 위치, 칸 번호이고 왼쪽 위에서부터 오른쪽 밑으로 오름차순
'''
# 다이아몬드 상속시 맨 위 생성자 두 번 호출되는 걸 방지
# 파이썬 3은 그냥 super()만 쓰세욤.
super().__init__(self)
self.image = pg.Surface((OBJSIZE, OBJSIZE))
self.image.fill(self.colors[int(np.log2(num))])
self.rect = self.image.get_rect()
coordx = PLAIN_MARGIN + PLAIN_WIDTH // 4 * loc[0] + NUMBLOCK_MARGIN
coordy = UPPER_MARGIN + PLAIN_HEIGHT // 4 * loc[1] + NUMBLOCK_MARGIN
self.rect.x, self.rect.y = coordx, coordy
self.animator = Animator()
font = pg.font.Font('Menlo-Regular.ttf', 11).set_bold(True)
textSurface = font.render(num, True, WHITE, None)
textRect = textSurface.get_rect()
textRect.center = self.rect.center
self.image.blit(textSurface, textRect) # 이미지 위에 텍스트 올림
### 진짜 화면 위에 올라가는 건 pg.display.update할 때!!
def move(self, direction):
self.animator.move(self.rect, direction)
def draw(self):
# Group.draw가 sprite.draw를 쓰는가?
# 쓰는거면 걍 오버라이딩하면 댐
# Group.draw(surface)하면 for surface.blit(sprite_of_Group.image, )함
# 그럼 이 메소드는 필요가 없자나?
# 나 왜 이 메소드 만들라 했지?
# todo : 아 텍스트가 이미지 위에 올라가 있으면 되겠다.
# blit은 이미지(pg.Surface)위에 이미지를 올리는 작업이다.
pass
def main():
########################################################################
global FPSCLOCK, DISPLAYSURF # 그냥 전역 변수 선언
pg.init()
FPSCLOCK = pg.time.Clock()
DISPLAYSURF = pg.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pg.display.set_caption(GAME_TITLE)
DISPLAYSURF.fill(COLOR_BG)
########################################################################
plain_surf = pg.Surface((PLAIN_SIZE, PLAIN_SIZE))