-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
73 lines (58 loc) · 1.8 KB
/
game.py
File metadata and controls
73 lines (58 loc) · 1.8 KB
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
import pygame, sys, math
def rotate2d(pos, rad):
x,y = pos
s,c = math.sin(rad),math.cos(rad)
return x*c-y*s,y*c+x*s
class Cam:
def __init__(self, pos=(0,0,0), rot=(0,0)):
self.pos = list(pos)
self.rot = list(rot)
def events(self,event):
if event.type == pygame.MOUSEMOTION:
x,y = event.rel
x/=200; y/=200
self.rot[0]+=y
self.rot[1]+=x
def update(self,dt,key):
s = dt*10
if key[pygame.K_q]: self.pos[1]-=s
if key[pygame.K_e]: self.pos[1]+=s
x, y = s*math.sin(self.rot[1]),s*math.cos(self.rot[1])
if key[pygame.K_w]: self.pos[0]+=x;self.pos[2]+=y
if key[pygame.K_s]: self.pos[0]-=x;self.pos[2]-=y
if key[pygame.K_a]: self.pos[0]-=y;self.pos[2]+=x
if key[pygame.K_d]: self.pos[0]+=y;self.pos[2]-=x
pygame.init()
w, h = 400, 400; cx, cy = w//2, h//2
screen = pygame.display.set_mode((w, h))
clock = pygame.time.Clock()
verts = (-1,-1,-1), (1,-1,-1), (1,1,-1), (-1,1,-1), (-1,-1,1), (1, -1, 1), (1,1,1), (-1,1,1)
edges = (0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),(0,4),(1,5),(2,6),(3,7)
cam = Cam((0,0,-5))
pygame.event.get()
pygame.mouse.get_rel()
pygame.mouse.set_visible(0)
pygame.event.set_grab(1)
while True:
dt = clock.tick()/1000
for event in pygame.event.get():
if event.type == pygame.QUIT: pygame.quit(); sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: pygame.quit(); sys.exit()
cam.events(event)
screen.fill((255,255,255))
for edge in edges:
points = []
for x,y,z in (verts[edge[0]],verts[edge[1]]):
x-=cam.pos[0]
y-=cam.pos[1]
z-=cam.pos[2]
x,z = rotate2d((x,z), cam.rot[1])
y,z = rotate2d((y,z), cam.rot[0])
f = 200/z
x,y = x*f,y*f
points+=[(cx+int(x), cy+int(y))]
pygame.draw.line(screen, (0,0,0), points[0], points[1], 1)
pygame.display.flip()
key = pygame.key.get_pressed()
cam.update(dt,key)