Created
June 26, 2018 06:37
-
-
Save VeggieVampire/8b7579052a37ce2615c87ce076de0d06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
from pygame.locals import * | |
from OpenGL.GL import * | |
from OpenGL.GLU import * | |
#X , Y, Z units ofr each point | |
vertices = ( | |
(1, -1, -1), | |
(1, 1, -1), | |
(-1, 1, -1), | |
(-1, -1, -1), | |
(1, -1, 1), | |
(1, 1, 1), | |
(-1, -1, 1), | |
(-1, 1, 1) | |
) | |
# Points to points array= edges. Given as vertices list number 0 th11 | |
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) | |
) | |
def Cube(): | |
glBegin(GL_LINES) | |
for edge in edges: | |
for vertex in edge: | |
# OpenGL code | |
glVertex3fv(vertices[vertex]) | |
glEnd() | |
def main(): | |
pygame.init() | |
try: | |
j = pygame.joystick.Joystick(0) # create a joystick instance | |
j.init() # init instance | |
print("Enabled joystick: {0} with {1} buttons".format(j.get_name(), j.get_numbuttons())) | |
# print("{0} has {1} buttons".format(j.get_name(), j.get_numbuttons() ) ) | |
except pygame.error: | |
print("no joystick found.") | |
display_width = 800 | |
display_height = 600 | |
display = (display_width,display_height) | |
pygame.display.set_mode((display), DOUBLEBUF|OPENGL) | |
viewing_distance = 0.1 | |
clipping_plan = 50.0 | |
gluPerspective(45, (display[0] / display[1]), viewing_distance, clipping_plan) | |
# Viewing the object XYZ point & Zoom direction -5 for zooming out | |
glTranslatef(0.0, 0.0, -5) | |
# Moving the object XYZ -5 | |
#glRotatef(0, 0, 0, 0) | |
posX = 0 | |
posY = 0 | |
posZ = 0 | |
while True: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
print("END OF LINE!") | |
quit() | |
#sys.exit() | |
if event.type == pygame.JOYAXISMOTION: # Joystick | |
if j.get_axis(0) >= 0.5: | |
# print ("right has been pressed") # Right | |
message = "Right has been pressed" | |
posX = 1 | |
# posY = 0 | |
if j.get_axis(0) <= 0.5 and j.get_axis(0) >= -1: # resets back to center for X | |
posX = 0 | |
if j.get_axis(1) <= 0.5 and j.get_axis(1) >= -1: # resets back to center for Y | |
posY = 0 | |
if j.get_axis(0) <= -1: | |
# print ("left has been pressed") # Left | |
message = "Left has been pressed" | |
posX = -1 | |
# posY = 0 | |
if j.get_axis(1) >= 0.5: | |
# print ("Down has been pressed") # Down | |
message = "Down has been pressed" | |
posY = 1 | |
# posX = 0 | |
if j.get_axis(1) <= -1: | |
message = "Up has been pressed" | |
# print ("Up has been pressed") # Up | |
posY = -1 | |
# posX = 0 | |
if j.get_button(1) == 1: | |
message = "A button" | |
if j.get_button(2) == 1: | |
message = "B button" | |
if j.get_button(3) == 1: | |
message = "Y button" | |
if j.get_button(4) == 1: | |
message = "Left bumper" | |
posY = -1 | |
posX = -1 | |
if j.get_button(5) == 1: | |
message = "Right bumper" | |
posY = 1 | |
posX = 1 | |
if j.get_button(6) == 1: | |
message = "unknown button 6" | |
if j.get_button(7) == 1: | |
message = "unknown button 7" | |
if j.get_button(8) == 1: | |
message = "Select button" | |
if j.get_button(9) == 1: | |
message = "Start button" | |
#print (posX, posY ) | |
glRotatef(0.5, posY, posX, posZ) | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
Cube() | |
pygame.display.flip() | |
pygame.time.wait(5) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment