Código de Python - Vista 'grid' (demo)

Imágen de perfil
Val: 712
Bronce
Ha aumentado 1 puesto en Python (en relación al último mes)
Gráfica de Python

Vista 'grid' (demo)gráfica de visualizaciones


Python

Actualizado el 9 de Abril del 2024 por Antonio (75 códigos) (Publicado el 31 de Julio del 2023)
2.520 visualizaciones desde el 31 de Julio del 2023
El siguiente código muestra un grid en pantalla por el que se puede desplazar usando los botones de dirección:

Botón de dirección derecha: Desplazamiento hacia la derecha.
Botón de dirección izquierdo: Desplazamiento a la izquierda.
Botón de dirección superior: Desplazamiento hacia adelante.
Botón de dirección inferior: Desplazamiento hacia atrás.
Botones 'o', 'p', 'k' y 'l': Desplazamientos en diagonal.

grid

Requerimientos

Lenguaje: Python
Librerías y recursos: OpenGL, Pygame.

1.0

Actualizado el 19 de Noviembre del 2023 (Publicado el 31 de Julio del 2023)gráfica de visualizaciones de la versión: 1.0
1.013 visualizaciones desde el 31 de Julio del 2023

2.0

Actualizado el 9 de Abril del 2024 (Publicado el 30 de Diciembre del 2023)gráfica de visualizaciones de la versión: 2.0
1.508 visualizaciones desde el 30 de Diciembre del 2023
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

Esta versión permite controlar la dirección de una figura que se desplaza por el grid, seguida por la cámara. Pudiéndose aumentar y disminuir la velocidad relativa de la figura (con los botones "Z" y "X" respectivamente) así como la del movimiento de la cámara ( "B" para aumentar y "N" para disminuir). A su vez, el botón "C" iguala la velocidad de la figura a la de la cámara. El botón "H" permite ocultar y/o mostrar la información de la esquina superior izquierda.

my_gif
my_gif
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
 
verticies = [
    [1, 0, -1],
    [1, 1, -1],
    [-1, 1, -1],
    [-1, 0, -1],
    [1, 0, 1],
    [1, 1, 1],
    [-1, 0, 1],
    [-1, 1, 1]
    ]
 
surfaces = (
    (0,1,2,3),
    (3,2,7,6),
    (6,7,5,4),
    (4,5,1,0),
    (1,5,7,2),
    (4,0,3,6)
    )
 
edges = (
    (0,1),
    (0,3),
    (0,4),
    (2,1),
    (2,3),
    (2,7),
    (6,3),
    (6,4),
    (6,7),
    (5,1),
    (5,4),
    (5,7)
    )
 
# DIBUJA FIGURA
def Cube():
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_CULL_FACE)
    glCullFace(GL_FRONT)
    glBegin(GL_QUADS)
    glColor3f(0.0,0.0,0.1)
    for surface in surfaces:
        x=0
        for vertex in surface:
            x+=1
            glVertex3fv(verticies[vertex])
    glEnd()
 
    glLineWidth(2.0)
    glBegin(GL_LINES)
    glColor3f(1.0,0.0,0.0,)
    for edge in edges:
        x=0
        for vertex in edge:
            x+=1
            glVertex3fv(verticies[vertex])
    glEnd()
 
# DEFINE FORMA DE LA FIGURA SOBRE EL GRID
def cube_form(val_list):
    val_list = val_list
    for i in range(0,8):
        verticies[i][1] = val_list[i]
 
# DIBUJA GRID
def draw_grid():
    glBegin(GL_LINES)
    glColor3f(0.0,1.0,0.0)#(0.5, 0.5, 0.5)  # Color gris
 
    for x in range(-grid_size, grid_size + 1, grid_spacing):
        glVertex3f(x, 0, -grid_size)
        glVertex3f(x, 0, grid_size)
 
    #glColor3f(1.0,0.0,0.0)
    for z in range(-grid_size, grid_size + 1, grid_spacing):
        glVertex3f(-grid_size, 0, z)
        glVertex3f(grid_size, 0, z)
 
    glEnd()
 
# MOSTRAR TEXTO ESQUINA SUP. IZQUIERDA
def drawText(f, x, y, text, c, bgc):
    textSurface = f.render(text, True, c, bgc)
    textData = pygame.image.tostring(textSurface, "RGBA", True)
    glWindowPos2d(x, y)
    glDrawPixels(textSurface.get_width(), textSurface.get_height(), GL_RGBA, GL_UNSIGNED_BYTE, textData)
 
cube_speed = 0.050 #0.00
camera_speed = 0.050
grid_size = 120
grid_spacing = 1
hide_data = False
 
# FUNCIÓN PRINCIPAL
def main():
    global cube_speed, camera_speed, hide_data, display_help
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
    font = pygame.font.SysFont('arial', 15)
    font2 = pygame.font.SysFont('arial', 20)
    direction = None
 
    glClearColor(0.0, 0.0, 0.0, 1.0)
    gluPerspective(45, (display[0] / display[1]), 0.1, 100.0)
    glTranslatef(0.0, 0.0, -7.0)
    glRotatef(7, 1, 0, 0)
 
    running = True
    while (running):
 
        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d:
                    if hide_data == False:
                        hide_data = True
                    else:
                        hide_data = False
 
                elif event.key == pygame.K_RIGHT:
                    if direction != "Right":
                        direction = "Right"
                        print("definiendo derecha")
                        cube_form([0.0,0.5,1.0,0.0,0.0,0.5,0.0,1.0])
 
                elif event.key == pygame.K_LEFT:
                    if direction != "Left":
                        direction = "Left"
                        print("definiendo izquierda")
                        cube_form([0.0,1.0,0.5,0.0,0.0,1.0,0.0,0.5])
 
                elif event.key == pygame.K_UP:
                    if direction != "Forward":
                        direction = "Forward"
                        print("definiendo adelante")
                        cube_form([0.0,0.5,0.5,0.0,0.0,1.0,0.0,1.0])
 
                elif event.key == pygame.K_DOWN:
                    if direction != "Backward":
                        direction = "Backward"
                        print("Definiendo atras")
                        cube_form([0.0,1.0,1.0,0.0,0.0,0.5,0.0,0.5])
 
        key = pygame.key.get_pressed()
 
        # CONTROL DE DIRECCIÓN
        if key[pygame.K_LEFT]:
            #direction = "Left"
            glTranslatef(camera_speed, 0.0, 0.0)
            verticies[0][0] -= cube_speed
            verticies[1][0] -= cube_speed
            verticies[2][0] -= cube_speed
            verticies[3][0] -= cube_speed
            verticies[4][0] -= cube_speed
            verticies[5][0] -= cube_speed
            verticies[6][0] -= cube_speed
            verticies[7][0] -= cube_speed
 
        if key[pygame.K_RIGHT]:
            #direction = "Right"
            glTranslatef(-camera_speed, 0.0, 0.0)
            verticies[0][0] += cube_speed
            verticies[1][0] += cube_speed
            verticies[2][0] += cube_speed
            verticies[3][0] += cube_speed
            verticies[4][0] += cube_speed
            verticies[5][0] += cube_speed
            verticies[6][0] += cube_speed
            verticies[7][0] += cube_speed
 
        if key[pygame.K_UP]:
            #direction = "Forward"
            glTranslatef(0.0, 0.0, camera_speed)
            verticies[0][2] -= cube_speed
            verticies[1][2] -= cube_speed
            verticies[2][2] -= cube_speed
            verticies[3][2] -= cube_speed
            verticies[4][2] -= cube_speed
            verticies[5][2] -= cube_speed
            verticies[6][2] -= cube_speed
            verticies[7][2] -= cube_speed
 
        if key[pygame.K_DOWN]:
            #direction = "Backward"
            glTranslatef(0.0, 0.0, -camera_speed)
            verticies[0][2] += cube_speed
            verticies[1][2] += cube_speed
            verticies[2][2] += cube_speed
            verticies[3][2] += cube_speed
            verticies[4][2] += cube_speed
            verticies[5][2] += cube_speed
            verticies[6][2] += cube_speed
            verticies[7][2] += cube_speed
 
 
        if key[pygame.K_z]:
            cube_speed += 0.002
        if key[pygame.K_x]:
            cube_speed -= 0.002
 
        if key[pygame.K_c]:
            cube_speed = camera_speed
 
        if key[pygame.K_b]:
            camera_speed += 0.002
        if key[pygame.K_n]:
            camera_speed -= 0.002
 
        # ROTACIONES
        if key[pygame.K_q]:
            glRotatef(1, 0, 1, 0)
 
        if key[pygame.K_w]:
            glRotatef(1, 0, -1, 0)
        if key[pygame.K_r]:
            glRotatef(0.1, 1, 0, 0)
        if key[pygame.K_g]:
            glRotatef(-0.1, 1, 0, 0)
 
        if key[pygame.K_y]:
            glRotatef(0.1, 0, 0, 1)
        if key[pygame.K_u]:
            glRotatef(0.1, 0, 0, -1)
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        draw_grid()
        Cube()
        if not hide_data:
            drawText(font, 20, 570, f'cube speed: {cube_speed:.3f}',(0, 0, 255, 255),(0,0,0))#######################
            drawText(font, 20, 554, f'camera speed: {camera_speed:.3f}',(0, 0, 255, 255),(0,0,0))##########
            drawText(font, 20, 538, f'direction: {direction}',(0, 0, 255, 255),(0,0,0))
 
        #direction = "None"
        pygame.display.flip()
        pygame.time.wait(10)
    pygame.quit()
 
main()



Comentarios sobre la versión: 2.0 (0)


No hay comentarios
 

Comentar la versión: 2.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s7403