-
Notifications
You must be signed in to change notification settings - Fork 1
/
beOGL_init.py
executable file
·166 lines (129 loc) · 4.67 KB
/
beOGL_init.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
import sys
import OpenGL.GL as GL
import OpenGL.GLUT as GLUT
import OpenGL.GLU as GLU
import math
import numpy as np
import random
from Ship import Ship
from Snake import Snake
from Element import Element
# fonction 'init' regroupant des appels de fonction a n'executer qu'une fois au
# demarrage du programme.
def init():
# nettoyage du buffer image :
GL.glClearColor(0.0, 0.0, 0.0, 0.0)
# on autorise l'interpolation de couleur :
GL.glShadeModel(GL.GL_SMOOTH)
# on autorise le test en profondeur :
GL.glEnable(GL.GL_DEPTH_TEST)
timerFunc()
key_update()
# fonction reshape de GLUT. Executee a chaque modification de la fenetre de rendu.
def reshape(width, height):
# parametrage du viewport :
GL.glViewport(0, 0, width, height)
# reinitialisation de la projection :
GL.glMatrixMode(GL.GL_PROJECTION)
GL.glLoadIdentity()
# projection perspective :
fAspect = width / height
# utilisation de la fonction gluPerspective de GLU (voir documentation).
# Les deux derniers parametres sont les plans de clipping (near, far) et sont
# des valeurs positives. Note: 'near' ne doit pas etre nul.
GLU.gluPerspective(60.0, fAspect, 1.0, 2000.0)
# placement de la camera :
# on utilise la fonction gluLookAt de GLU.
GLU.gluLookAt(0, 0.0, 180.0, 0.0, 0.0, 0.0, 0, 1, 0)
# fonction display de GLUT. Contient le code a executer pour faire un nouveau rendu.
def display():
# nettoyage des buffers d'image et de profondeur :
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
############################## A COMPLETER ################################
myShip.place_camera()
myShip.draw()
for enemy in enemies:
enemy.draw()
###########################################################################
# on force OpenGL a executer toutes les commandes :
GL.glFlush()
# ligne suivante a decommenter pour echanger les deux buffers d'image
GLUT.glutSwapBuffers()
# on force la fonction display a etre executee a nouveau
GLUT.glutPostRedisplay()
def keyboard_up(key, x, y):
global pressed_key
key = key.decode('utf-8')
pressed_key.remove(key)
# fonction keyboard pour la gestion des evenements clavier.
def keyboard(key, x, y):
key = key.decode('utf-8')
global myShip
pressed_key.add(key)
if key == chr(27):
exit(0)
if key == 'r':
myShip = Ship(center=[0.0 for i in range(4)], speed=0, bottom_color=(1, 0, 0), head_color=(1, 1, 1))
def key_update(dummy = None):
global da
x = y = 0
if 'o' in pressed_key:
da += 1.0
if 'p' in pressed_key:
da -= 1.0
if 'z' in pressed_key or 'w' in pressed_key:
x = da
if ' ' in pressed_key:
myShip.speed += 2
if 's' in pressed_key:
x = -da
if 'd' in pressed_key:
y = da
if 'q' in pressed_key or 'a' in pressed_key:
y = -da
if x != 0 or y != 0:
myShip.update_mat(x, y)
GLUT.glutTimerFunc(10, key_update, None)
def timerFunc(dummy = None):
# on modifie les variables de position
myShip.update_center()
myShip.speed = myShip.speed*0.90 if myShip.speed > 1 else myShip.speed - 0.001 if myShip.speed > 0 else 0
# Move enemies
for enemy in enemies: # type:Ship
enemy.update_center()
enemy.update_mat(3, random.randint(-5, 5))
enemy.speed += max(-enemy.speed, random.randint(-1, 1))
# on lance a nouveau un timer
GLUT.glutTimerFunc(10, timerFunc, 10)
if __name__ == "__main__":
pressed_key = set()
da = 5.0
myShip = Snake(center=np.zeros(4), speed=0, head_color=(0, 1, 1))
enemies = []
num_e = 100
for i in range(num_e):
enemies.append(Ship())
initWindowWidth = 1200 # unit: pixels
initWindowHeight = 1000 # unit: pixels
Element.screen_width = initWindowWidth
Element.screen_height = initWindowHeight
initWindowPosition_X = 100 # unit: pixels
initWindowPosition_Y = 100 # unit: pixels
GLUT.glutInit(sys.argv)
# Caracterisation de la fenetre d'affichage
GLUT.glutInitDisplayMode(GLUT.GLUT_DOUBLE | GLUT.GLUT_RGBA | GLUT.GLUT_DEPTH)
# Taille de la fenetre de rendu
GLUT.glutInitWindowSize(initWindowWidth, initWindowHeight)
# Position de la fenetre
GLUT.glutInitWindowPosition(initWindowPosition_X, initWindowPosition_Y)
# creation de la fenetre
GLUT.glutCreateWindow('BE OpenGL Supelec')
# appel de la fonction d'initialisation
init()
# declaration des fonctions GLUT :
GLUT.glutDisplayFunc(display)
GLUT.glutReshapeFunc(reshape)
GLUT.glutKeyboardFunc(keyboard)
GLUT.glutKeyboardUpFunc(keyboard_up)
# Boucle infinie d'interaction
GLUT.glutMainLoop()