-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameKinnect.py
More file actions
executable file
·158 lines (125 loc) · 5.13 KB
/
Copy pathGameKinnect.py
File metadata and controls
executable file
·158 lines (125 loc) · 5.13 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
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
'''
pygamegame.py
created by Lukas Peraza
for 15-112 F15 Pygame Optional Lecture, 11/11/15
use this code in your term project if you want
- CITE IT
- you can modify it to your liking
- BUT STILL CITE IT
- you should remove the print calls from any function you aren't using
- you might want to move the pygame.display.flip() to your redrawAll function,
in case you don't need to update the entire display every frame (then you
should use pygame.display.update(Rect) instead)
'''
import pygame
from pykinect2 import PyKinectV2, PyKinectRuntime
from pykinect2.PyKinectV2 import*
import ctypes
import _ctypes
import sys
import math
class GameKinnect(object):
cur_right_hand_x = 0
delta = 0
def init(self):
pass
def mousePressed(self, x, y):
pass
def mouseReleased(self, x, y):
pass
def mouseMotion(self, x, y):
pass
def mouseDrag(self, x, y):
pass
def keyPressed(self, keyCode, modifier):
pass
def keyReleased(self, keyCode, modifier):
pass
def timerFired(self, dt):
pass
def redrawAll(self, screen):
pass
def isKeyPressed(self, key):
''' return whether a specific key is being held '''
return self._keys.get(key, False)
def __init__(self, width=600, height=400, fps=50, title="112 Pygame Game"):
self.width = width
self.height = height
self.fps = fps
self.title = title
self.bgColor = (255, 255, 255)
self.kinect=PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Color | PyKinectV2.FrameSourceTypes_Body)
self.frame_surface=pygame.Surface((self.kinect.color_frame_desc.Width, self.kinect.color_frame_desc.Height),0,32)
self.bodies = None
self.prev_right_hand_x = 0
pygame.init()
def draw_color_frame(self,frame,target_surface):
target_surface.lock()
address=self.kinect.surface_as_array(target_surface.get_buffer())
ctypes.memmove(address,frame.ctypes.data,frame.size)
del address
target_surface.unlock()
def run(self):
clock = pygame.time.Clock()
screen = pygame.display.set_mode((self.width, self.height))
# set the title of the window
pygame.display.set_caption(self.title)
# stores all the keys currently being held down
self._keys = dict()
# call game-specific initialization
self.init()
playing = True
while playing:
time = clock.tick(self.fps)
self.timerFired(time)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
self.mousePressed(*(event.pos))
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
self.mouseReleased(*(event.pos))
elif (event.type == pygame.MOUSEMOTION and
event.buttons == (0, 0, 0)):
self.mouseMotion(*(event.pos))
elif (event.type == pygame.MOUSEMOTION and
event.buttons[0] == 1):
self.mouseDrag(*(event.pos))
elif event.type == pygame.KEYDOWN:
self._keys[event.key] = True
self.keyPressed(event.key, event.mod)
elif event.type == pygame.KEYUP:
self._keys[event.key] = False
self.keyReleased(event.key, event.mod)
elif event.type == pygame.QUIT:
playing = False
# for Kinect
if self.kinect.has_new_body_frame():
self.bodies=self.kinect.get_last_body_frame()
if self.bodies is not None:
for i in range(0,self.kinect.max_body_count):
body=self.bodies.bodies[i]
if not body.is_tracked:
continue
joints=body.joints
if joints[PyKinectV2.JointType_HandRight].TrackingState == PyKinectV2.TrackingState_Tracked:
GameKinnect.cur_right_hand_x=joints[PyKinectV2.JointType_HandRight].Position.x
GameKinnect.delta = (self.prev_right_hand_x- GameKinnect.cur_right_hand_x)
print(GameKinnect.delta)
self.prev_right_hand_x =GameKinnect.cur_right_hand_x
if self.kinect.has_new_color_frame():
frame=self.kinect.get_last_color_frame()
self.draw_color_frame(frame,self.frame_surface)
frame=None
screen.fill(self.bgColor)
h_to_w=float(self.frame_surface.get_height()/self.frame_surface.get_width())
target_height=int(h_to_w*self.width)
surface_to_draw=pygame.transform.scale(self.frame_surface,(screen.get_width(),target_height))
screen.blit(surface_to_draw,(0,0))
self.redrawAll(screen)
pygame.display.flip()
self.kinect.close()
pygame.quit()
def main():
game = GameKinnect()
game.run()
if __name__ == '__main__':
main()