-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
154 lines (124 loc) · 5.36 KB
/
main.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
from pyglet.gl import *
from pyglet.window import key
from pyglet.window import mouse
from pyglet import image
import math
# comment code block crtl+k+c /uncomment code block crtl+k+u
class Model:
def __init__(self):
self.top = self.get_tex('grass_top.png')
self.side = self.get_tex('grass_side.png')
self.bottom = self.get_tex('dirt.png')
self.batch = pyglet.graphics.Batch()
tex_coords = ('t2f', (0, 0, 1, 0, 1, 1, 0, 1, ))
x, y, z = 0, 0, -1
X, Y, Z = x+1, y+1, z+1
for x in range(16):
x += 1
X = x+1
for z in range(16):
z += 1
Z = z + 1
self.batch.add(4, GL_QUADS, self.top, ('v3f',
(x, Y, Z, X, Y, Z, X, Y, z, x, Y, z, )), tex_coords)
self.batch.add(4, GL_QUADS, self.top, ('v3f',
(x, Y, Z, X, Y, Z, X, Y, z, x, Y, z, )), tex_coords)
self.batch.add(4, GL_QUADS, self.top, ('v3f',
(x+2, Y, Z+2, X+2, Y, Z+2, X+2, Y, z+2, x+2, Y, z+2, )), tex_coords)
self.batch.delete()
# self.batch.add(4, GL_QUADS, self.side, ('v3f',
# (x, y, z, x, y, Z, x, Y, Z, x, Y, z, )), tex_coords)
# self.batch.add(4, GL_QUADS, self.side, ('v3f',
# (X, y, Z, X, y, z, X, Y, z, X, Y, Z, )), tex_coords)
# self.batch.add(4, GL_QUADS, self.bottom, ('v3f',
# (x, y, z, X, y, z, X, y, Z, x, y, Z, )), tex_coords)
# self.batch.add(4, GL_QUADS, self.top, ('v3f',
# (x, Y, Z, X, Y, Z, X, Y, z, x, Y, z, )), tex_coords)
# self.batch.add(4, GL_QUADS, self.side, ('v3f',
# (X, y, z, x, y, z, x, Y, z, X, Y, z, )), tex_coords)
# self.batch.add(4, GL_QUADS, self.side, ('v3f',
# (x, y, Z, X, y, Z, X, Y, Z, x, Y, Z, )), tex_coords)
def draw(self):
self.batch.draw()
# self.vertex_list.draw(pyglet.gl.GL_QUADS)
def get_tex(self, file):
tex = image.load(file).get_texture()
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
return pyglet.graphics.TextureGroup(tex)
class Player:
def __init__(self, pos=(0, 0, 0), rot=(0, 0)):
self.pos = list(pos)
self.rot = list(rot)
def mouse_motion(self, x, y, dx, dy):
dx /= 8
dy /= 8
self.rot[0] += dy
self.rot[1] -= dx
if self.rot[0] > 90:
self.rot[0] = 90
elif self.rot[0] < -90:
self.rot[0] = -90
def update(self, dt, keys):
s = dt*10
rotY = -self.rot[1]/180*math.pi
dx, dz = s*math.sin(rotY), s*math.cos(rotY)
if keys[key.W]:
self.pos[0] += dx
self.pos[2] -= dz
if keys[key.S]:
self.pos[0] -= dx
self.pos[2] += dz
if keys[key.A]:
self.pos[0] -= dz
self.pos[2] -= dx
if keys[key.D]:
self.pos[0] += dz
self.pos[2] += dx
if keys[key.SPACE]:
self.pos[1] += s
if keys[key.LSHIFT]:
self.pos[1] -= s
class Window(pyglet.window.Window):
def push(self, pos, rot): glPushMatrix(
); glRotatef(-rot[0], 1, 0, 0); glRotatef(-rot[1], 0, 1, 0); glTranslatef(-pos[0], -pos[1], -pos[2],)
def Projection(self): glMatrixMode(GL_PROJECTION); glLoadIdentity()
def Model(self): glMatrixMode(GL_MODELVIEW); glLoadIdentity()
def set2d(self): self.Projection(); gluOrtho2D(
0, self.width, 0, self.height); self.Model()
def set3d(self): self.Projection(); gluPerspective(
70, self.width/self.height, 0.05, 1000); self.Model()
def setLock(self, state): self.lock = state; self.set_exclusive_mouse(state)
lock = False
mouse_lock = property(lambda self: self.lock, setLock)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_minimum_size(300, 200)
self.keys = key.KeyStateHandler()
self.push_handlers(self.keys)
pyglet.clock.schedule(self.update)
self.model = Model()
self.player = Player((0.5, 1.5, 1.5), (-30, 0))
def on_mouse_motion(self, x, y, dx, dy):
if self.mouse_lock:
self.player.mouse_motion(x, y, dx, dy)
def on_key_press(self, KEY, MOD):
if KEY == key.ESCAPE:
self.close()
elif KEY == key.E:
self.mouse_lock = not self.mouse_lock
def update(self, dt):
self.player.update(dt, self.keys)
def on_draw(self):
self.clear()
self.set3d()
self.push(self.player.pos, self.player.rot)
self.model.draw()
glPopMatrix()
if __name__ == '__main__':
window = Window(width=854, height=480, caption='Minecraft', resizable=True)
glClearColor(0.5, 0.7, 1, 1)
glEnable(GL_DEPTH_TEST)
window.set_exclusive_mouse(True)
# glEnable(GL_CULL_FACE)
pyglet.app.run()