-
Notifications
You must be signed in to change notification settings - Fork 4
/
leapong.py
174 lines (131 loc) · 4.77 KB
/
leapong.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
171
172
173
174
""" Leapong """
import sys
sys.path.insert(0, "lib")
import Leap
import math
import pygame
from pygame.locals import QUIT, KEYDOWN, K_ESCAPE
from pygame import sprite
from pygame.locals import *
from leapong.basesprite import BaseSprite
from leapong.paddle import Paddle
from leapong.ball import Ball
from leapong.goal import Goal
from leapong.border import Border
from leapong.collisionfunctions import collision_functions
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import time
SCREEN_SIZE = (800, 600)
WHOLE_SCREEN_SIZE = (800, 650)
def glut_print(x, y, font, text, r, g, b, a):
blending = False
if glIsEnabled(GL_BLEND) :
blending = True
glColor3f(1,1,1)
glRasterPos2f(x,y)
glutInit(1, "Something")
for ch in text :
glutBitmapCharacter( font , ctypes.c_int( ord(ch) ) )
if not blending :
glDisable(GL_BLEND)
def enable2D():
glViewport (0, 0, WHOLE_SCREEN_SIZE[0], WHOLE_SCREEN_SIZE[1]);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho (0, WHOLE_SCREEN_SIZE[0], WHOLE_SCREEN_SIZE[1], 0, -10, 10);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT)
def main():
pygame.init()
screen = pygame.display.set_mode(WHOLE_SCREEN_SIZE, OPENGL|DOUBLEBUF)
clock = pygame.time.Clock()
elements_ball = []
elements_paddle = []
elements_goal = []
elements_border = []
margin = 20
left_paddle = Paddle((0 + margin, 20), (10, 80))
right_paddle = Paddle((SCREEN_SIZE[0] - 10 - margin, 20), (10, 80))
elements_paddle.append(left_paddle)
elements_paddle.append(right_paddle)
ball1 = Ball((50, 50), 10)
elements_ball.append(ball1)
goal_pl_1 = Goal((0, 0), (5, 600))
elements_goal.append(goal_pl_1)
goal_pl_2 = Goal((795, 0), (5, 600))
elements_goal.append(goal_pl_2)
border_top = Border((5, 0), (795, 5))
border_top.top = True
elements_border.append(border_top)
border_bottom = Border((5, 595), (795, 5))
elements_border.append(border_bottom)
going = True
controller = Leap.Controller()
while going:
frame = controller.frame()
if not frame.hands.is_empty and len(frame.hands) == 2:
hand_left = frame.hands[0]
hand_right = frame.hands[1]
hand_left_direction = hand_left.direction[1] if hand_left.direction[1] >= 0 else 0
hand_right_direction = hand_right.direction[1] if hand_right.direction[1] >= 0 else 0
hand_left_direction = hand_left_direction if hand_left_direction <= 0.6 else 0.6
hand_right_direction = hand_right_direction if hand_right_direction <= 0.6 else 0.6
left_paddle.set_position(
left_paddle.boundingbox.x1, (0.6 - hand_left_direction) * (1000)
)
right_paddle.set_position(
right_paddle.boundingbox.x1, (0.6 - hand_right_direction) * (1000)
)
for event in pygame.event.get():
if event.type == QUIT:
going = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
going = False
for element in elements_paddle:
for element2 in elements_border:
if element.collide(element2):
resolve_collision(element, element2)
for element in elements_ball:
for element2 in elements_border:
if element.collide(element2):
resolve_collision(element, element2)
for element in elements_ball:
for element2 in elements_goal:
if element.collide(element2):
resolve_collision(element, element2)
print "{0} - {1}".format(goal_pl_1.points, goal_pl_2.points)
for element in elements_paddle:
for element2 in elements_ball:
if element.collide(element2):
resolve_collision(element, element2)
enable2D()
ball1.render()
ball1.update()
left_paddle.render()
right_paddle.render()
goal_pl_1.render()
goal_pl_2.render()
border_top.render()
border_bottom.render()
glut_print(
50 , 630 , GLUT_BITMAP_9_BY_15 ,
"Player 1: {0}".format(goal_pl_2.points) ,
1.0 , 1.0 , 1.0 , 1.0
)
glut_print(
650 , 630 , GLUT_BITMAP_9_BY_15 ,
"Player 2: {0}".format(goal_pl_1.points) ,
1.0 , 1.0 , 1.0 , 1.0
)
pygame.display.flip()
pygame.quit()
def resolve_collision(element_1, element_2):
try:
collision_functions["%s-%s" % (element_1.__class__.__name__, element_2.__class__.__name__)](element_1, element_2)
except KeyError:
pass
if __name__ == '__main__':
main()