0% found this document useful (0 votes)
3 views

Rotaciones Python OpenGL

The document contains a Python script that uses OpenGL to create a 2D Tangram game. It defines various shapes (triangles, squares, and rhomboids) and their drawing functions, along with handling user input for shape rotation and movement. The main function initializes the OpenGL context and starts the rendering loop for the game.

Uploaded by

Emilio Chávez
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Rotaciones Python OpenGL

The document contains a Python script that uses OpenGL to create a 2D Tangram game. It defines various shapes (triangles, squares, and rhomboids) and their drawing functions, along with handling user input for shape rotation and movement. The main function initializes the OpenGL context and starts the rendering loop for the game.

Uploaded by

Emilio Chávez
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CÓDIGO FUENTE:

from OpenGL.GL import *


from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys

ShapeID = 0
jump = 0.1
rotation_jump = 45.0
rotations = {
1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0,
5: 0.0, 6: 0.0, 7: 0.0
}
rotation_direction = 1
positions = {
1: [0.0, 0.0], 2: [0.0, 0.0], 3: [0.25, 0.25],
4: [0.0, 0.0], 5: [0.0, 0.0], 6: [-0.25, -0.25], 7: [0.5, 0.0]
}

def BlueTriangle():
inc = 0.5
glColor3f(0.0, 1.0, 1.0)
glBegin(GL_TRIANGLES)
glVertex2f(0.0, 0.0)
glVertex2f(-inc, inc)
glVertex2f(inc, inc)
glEnd()
return [0.0, 1/3]

def RedTriangle():
inc = 0.5
glColor3f(1.0, 0.0, 0.0)
glBegin(GL_TRIANGLES)
glVertex2f(0.0, 0.0)
glVertex2f(-inc, -inc)
glVertex2f(-inc, inc)
glEnd()
return [-1/3, 0.0]
def GreenTriangle():
inc = 0.25
glColor3f(0.0, 1.0, 0.0)
glBegin(GL_TRIANGLES)
glVertex2f(0.0, 0.0)
glVertex2f(inc, inc)
glVertex2f(inc, -inc)
glEnd()
return [0.5/3, 0.0]

def YellowSquare():
inc = 0.25
glColor3f(1.0, 1.0, 0.0)
glBegin(GL_QUADS)
glVertex2f(0.0, 0.0)
glVertex2f(inc, inc)
glVertex2f(2.0 * inc, 0.0)
glVertex2f(inc, -inc)
glEnd()
return

def PinkTriangle():
inc = 0.25
glColor3f(1.0, 0.0, 0.5)
glBegin(GL_TRIANGLES)
glVertex2f(0.0, 0.0)
glVertex2f(inc, -inc)
glVertex2f(-inc, -inc)
glEnd()
return [0.0, -0.5/3]

def WhiteRhomboid():
inc = 0.25
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_QUADS)
glVertex2f(0.0, 0.0)
glVertex2f(2.0 * inc, 0.0)
glVertex2f(inc, -inc)
glVertex2f(-inc, -inc)
glEnd()
return [0.125, -0.125]

def NavyTriangle():
inc = 0.5
glColor3f(0.3, 0.6, 1.0)
glBegin(GL_TRIANGLES)
glVertex2f(0.0, 0.0)
glVertex2f(0.0, -inc)
glVertex2f(-inc, -inc)
glEnd()
return [-0.5/3, -1/3]

def GetCentroid(shape_id):
if shape_id == 1:
return BlueTriangle.centroid
elif shape_id == 2:
return RedTriangle.centroid
elif shape_id == 3:
return GreenTriangle.centroid
elif shape_id == 4:
return YellowSquare.centroid
elif shape_id == 5:
return PinkTriangle.centroid
elif shape_id == 6:
return WhiteRhomboid.centroid
elif shape_id == 7:
return NavyTriangle.centroid
return

BlueTriangle.centroid = [0.0, 1/3]


RedTriangle.centroid = [-1/3, 0.0]
GreenTriangle.centroid = [0.5/3, 0.0]
YellowSquare.centroid = [0.25, 0.0]
PinkTriangle.centroid = [0.0, -0.5/3]
WhiteRhomboid.centroid = [0.125, -0.125]
NavyTriangle.centroid = [-0.5/3, -1/3]

def DrawShape(shape_id):
if shape_id == 1:
BlueTriangle()
elif shape_id == 2:
RedTriangle()
elif shape_id == 3:
GreenTriangle()
elif shape_id == 4:
YellowSquare()
elif shape_id == 5:
PinkTriangle()
elif shape_id == 6:
WhiteRhomboid()
elif shape_id == 7:
NavyTriangle()

def DrawScene():
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
for shape_id, (x, y) in positions.items():
centroid_x, centroid_y = GetCentroid(shape_id)
glPushMatrix()
glTranslatef(x, y, 0.0)
glTranslatef(centroid_x, centroid_y, 0.0)
glRotatef(rotations[shape_id], 0.0, 0.0, 1.0)
glTranslatef(-centroid_x, -centroid_y, 0.0)
DrawShape(shape_id)
glPopMatrix()
glutSwapBuffers()

def handle_key(key, x, y):


global ShapeID, rotation_direction

if isinstance(key, bytes):
key = key.decode("utf-8")

if key.isdigit():
ShapeID = int(key)

if ShapeID in positions:
if key == 'r':
rotations[ShapeID] += rotation_direction * rotation_jump
elif key == 'd':
rotation_direction *= -1
glutPostRedisplay()

def handle_special_key(key, x, y):


global ShapeID
if ShapeID in positions:
x_pos, y_pos = positions[ShapeID]
if key == GLUT_KEY_UP:
y_pos += jump
elif key == GLUT_KEY_DOWN:
y_pos -= jump
elif key == GLUT_KEY_RIGHT:
x_pos += jump
elif key == GLUT_KEY_LEFT:
x_pos -= jump
positions[ShapeID] = [x_pos, y_pos]
glutPostRedisplay()

def init():
glClearColor(0.0, 0.0, 0.0, 1.0)
gluOrtho2D(-2, 2, -1.5, 1.5)

def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize(800, 600)
glutCreateWindow(b"Tangram 2D")
init()
glutDisplayFunc(DrawScene)
glutKeyboardFunc(handle_key)
glutSpecialFunc(handle_special_key)
glutMainLoop()

if __name__ == "__main__":
main()

You might also like