forked from jrcurtis/block-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawing.py
131 lines (107 loc) · 3.3 KB
/
drawing.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
from ctypes import *
import pyglet
from pyglet.gl import *
from geom import *
def draw_block(block):
glColor3f(*block.color)
for square in block:
x1 = square[0]
x2 = square[0] + 1
y1 = -square[1]
y2 = -square[1] - 1
# glRectf(x1, y1, x2, y2)
glLineWidth(3)
glBegin(GL_LINES)
if not block.matrix.get((-y1 - 1, x1), False):
glVertex2f(x1, y1)
glVertex2f(x2, y1)
if not block.matrix.get((-y1, x1 + 1), False):
glVertex2f(x2, y1)
glVertex2f(x2, y2)
if not block.matrix.get((-y1 + 1, x1), False):
glVertex2f(x1, y2)
glVertex2f(x2, y2)
if not block.matrix.get((-y1, x1 - 1), False):
glVertex2f(x1, y1)
glVertex2f(x1, y2)
glEnd()
def draw_block_dump(dump):
y = dump.rows
x = 0
row = col = 0
for square in dump:
if x == dump.cols:
x = 0
y -= 1
row += 1
col = 0
if square == 0:
x += 1
col += 1
continue
x1 = x
x2 = (x + 1)
y1 = y
y2 = (y - 1)
w = 0.1
# glColor3f(*square)
glColor3f(0.0, 0.0, 0.0)
if not dump.get((row - 1, col), 0) == square:
glRectf(x1, y1, x2, y1 - w)
if not dump.get((row, col + 1), 0) == square:
glRectf(x2, y1, x2 - w, y2)
if not dump.get((row + 1, col), 0) == square:
glRectf(x1, y2, x2, y2 + w)
if not dump.get((row, col - 1), 0) == square:
glRectf(x1, y1, x1 + w, y2)
glColor4f(*(square + (0.5,)))
glRectf(x1, y1, x2, y2)
x += 1
col += 1
def draw_block_queue(queue):
glPushMatrix()
glTranslatef(1.0, -0.5, 0.0)
for block in queue:
d_block = Block(block.shape, block.color)
draw_block(d_block)
glTranslatef(block.matrix.cols + 2, 0, 0)
glPopMatrix()
def draw_border(color, width, height, thickness):
glColor3f(*color)
glLineWidth(thickness)
glBegin(GL_LINE_LOOP)
glVertex2f(0, 0)
glVertex2f(width, 0)
glVertex2f(width, height)
glVertex2f(0, height)
glEnd()
def draw_block_field(field):
width = field.w
height = field.h
thickness = 3
draw_border((1.0, 1.0, 0.0), width, height, thickness)
draw_block_dump(field.dump)
glPushMatrix()
glTranslatef(0.0, field.h, 0.0)
eqn = (c_double * 4)(0.0, -1.0, 0.0, 0.0)
glClipPlane(GL_CLIP_PLANE0, eqn)
glEnable(GL_CLIP_PLANE0)
if field.block:
glPushMatrix()
glTranslatef(field.block.x, -field.block.y, 0)
draw_block(field.block)
glPopMatrix()
glDisable(GL_CLIP_PLANE0)
glTranslatef(width, 0.0, 0.0)
max_block_size = 4
num_blocks = len(field.queue)
glScalef(0.7, 0.7, 1.0)
glTranslatef(0, -max_block_size, 0)
draw_border((1.0, 1.0, 0.0),
(max_block_size + 2) * num_blocks, max_block_size, thickness)
glTranslatef(0, max_block_size, 0)
draw_block_queue(field.queue)
glPopMatrix()
def draw_cursor_border(width, height, color):
glTranslatef(0, -height/2, 0)
draw_border(color, width, height, 2)