def setup_graphics(self, resolution): self.camera = graphics.Camera(self.window) self.scene = graphics.Scene(self.window) graphics.Layer(self.scene, cocos.layer.ColorLayer(0, 0, 0, 1)) for i in range(8): graphics.Layer( self.scene, graphics.BackgroundCocosLayer(self.camera, 10.0 + 0.5 * i, "starfield{0}.png".format(i))) self.hud_width = 170 self.main_layer = graphics.Layer(self.scene) self.main_layer.cocos_layer.position = self.camera.offset() self.main_layer.size = (self.window.width - self.hud_width, self.window.height) self.hud_cocos_layer = graphics.BlankingCocosLayer() self.hud_cocos_layer.clear_colour = (0, 40, 40, 255) self.hud_cocos_layer.clear_rect = ((self.window.width - self.hud_width, 0), (self.window.width, 0), (self.window.width, self.window.height), (self.window.width - self.hud_width, self.window.height)) self.hud_layer = graphics.Layer(self.scene, cocos_layer=self.hud_cocos_layer) self.hud_layer.cocos_layer.position = 0, 0 self.gem_images = pyglet.image.ImageGrid( pyglet.image.load("gems3.png"), 4, 4) self.atlas = Atlas("atlas.generated") self.object_psys = bsgl.System( texture_id=self.atlas.sheet.get_texture().id) self.hud_psys = bsgl.System( texture_id=self.atlas.sheet.get_texture().id)
def __init__(self): self.seed = 0 self.player = None self.running = True self.level = 0 self.height_map = [] self.creatures = [] self.items = [] self.bullets = [] # List of lists indicating positions self.clock = pygame.time.Clock() self.highscore = 0 self.camera = graphics.Camera() self.transition_period = False self.transition_counter = 0 self.time_of_day = "day" self.period_countdown = 0 self.level_transition = False self.level_transition_countdown = 0
ax.set_xlim3d([x_middle - plot_radius, x_middle + plot_radius]) ax.set_ylim3d([y_middle - plot_radius, y_middle + plot_radius]) ax.set_zlim3d([z_middle - plot_radius, z_middle + plot_radius]) if __name__ == '__main__': import graphics # define object obj = graphics.load_cube() # define points and cam cam_pos = (-3, -2, 0) look_at = (0, 0, 0) up = (0, 0, 1) kwargs = dict(position=cam_pos, mode='orthographic') cam = graphics.Camera(100, up, look_at, **kwargs) # define world world = graphics.World([obj]) # creat figure fig = plt.figure() fig.set_size_inches([10, 8]) World = fig.add_subplot(221, projection=Axes3D.name) Eye = fig.add_subplot(222, projection=Axes3D.name) Clip = fig.add_subplot(223, projection=Axes3D.name) Persp = fig.add_subplot(224, projection=Axes3D.name) # draw obj_col = 'r' vert_col = 'b' cam_col = 'g' # create edges to draw world_edges = cam.view_world(world)
def main(): #initialize pygame module and set window pygame.init() #DISPLAY WINDOW SIZE. CHANGE TO SUIT YOUR SCREEN IF NECESSARY width, height = 1800,900 pygame.display.set_icon(pygame.image.load('res/gameicon.jpg')) screen = pygame.display.set_mode((width,height), HWSURFACE|OPENGL|DOUBLEBUF) pygame.display.set_caption("Pylot") glViewport(0,0,width,height) glEnable(GL_DEPTH_TEST) default_view = np.identity(4) #boolean variables for camera view, lose screen, and pause FPV = True LOSE = False PAUSE = False KEYBOARD = False DATA = True #SIMULATION FRAMERATE #pygame limits framerate to this value. Increasing framerate improves accuracy of simulation (max limit = 60) but may cause some lag in graphics target_framerate = 60 #initialize graphics objects #loading screen is rendered and displayed while the rest of the objects are read and prepared glClearColor(0.,0.,0.,1.0) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) loading = graphics.Text(150) loading.draw(-0.2,-0.05,"Loading...",(0,255,0,1)) pygame.display.flip() #initialize game over screen gameover = graphics.Text(150) #initialize graphics aircraft graphics_aircraft = graphics.Mesh("res/Cessna.obj","shaders/aircraft.vs","shaders/aircraft.fs","res/cessna_texture.jpg",width,height) graphics_aircraft.set_orientation([0.,0.,0.,0.]) graphics_aircraft.set_position([0.,0.,-500.]) #initialize HUD HUD = graphics.HeadsUp(width, height) #initialize flight data overlay data = graphics.FlightData() #initialize field field = graphics.Mesh("res/field.obj","shaders/field.vs","shaders/field.fs","res/field_texture.jpg",width,height) field.set_position(np.array([0.,0.,0.])) #initialize camera object cam = graphics.Camera() a_obj = aircraft.Aircraft() dt = simulation.load_file('11.24_input.json', a_obj) simulation.initialize(a_obj) #initialize other pygame elements if pygame.joystick.get_count()>0.: joy = pygame.joystick.Joystick(0) joy.init() else: KEYBOARD = True thr = 0. UP = False DOWN = False RIGHT = False LEFT = False WW = False SS = False AA = False DD = False #DEFLECTION LIMITS FOR CONTROL SURFACES d_ail = 15. d_ele = 15. d_rud = 15. #clock object for tracking frames and timestep clock = pygame.time.Clock() #ticks clock before starting game loop clock.tick_busy_loop() #game loop while True: #event loop checks for game inputs such as joystick and keyboard commands for event in pygame.event.get(): if event.type == QUIT: return if KEYBOARD == False: if event.type == pygame.JOYBUTTONDOWN: if event.button ==2: FPV = not FPV cam.pos_storage.clear() cam.up_storage.clear() cam.target_storage.clear() else: if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: UP = True if event.key == pygame.K_DOWN: DOWN = True if event.key == pygame.K_LEFT: LEFT = True if event.key == pygame.K_RIGHT: RIGHT = True if event.key == pygame.K_w: WW = True if event.key == pygame.K_s: SS = True if event.key == pygame.K_a: AA = True if event.key == pygame.K_d: DD = True if event.key == pygame.K_SPACE: FPV = not FPV cam.pos_storage.clear() cam.up_storage.clear() cam.target_storage.clear() if event.type == pygame.KEYUP: if event.key == pygame.K_UP: UP = False if event.key == pygame.K_DOWN: DOWN = False if event.key == pygame.K_LEFT: LEFT = False if event.key == pygame.K_RIGHT: RIGHT = False if event.key == pygame.K_w: WW = False if event.key == pygame.K_s: SS = False if event.key == pygame.K_a: AA = False if event.key == pygame.K_d: DD = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_i: DATA = not DATA #pause simulation if event.key == pygame.K_p: PAUSE = not PAUSE #quit game if event.key == pygame.K_q: return #maintains framerate even if sim is paused if PAUSE == True: clock.tick(target_framerate) #if game is not paused, runs sim if PAUSE == False: #set default background color for sky glClearColor(0.65,1.0,1.0,1.0) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #timestep for simulation is based on framerate t= clock.tick(target_framerate)/1000. # if joystick is being used, gets joystick input and creates control_state dicitonary if KEYBOARD == False: control_state = { "aileron": (joy.get_axis(0)**3)*-d_ail, "elevator": (joy.get_axis(1)**3)*-d_ele, "rudder": (joy.get_axis(3)**3)*-d_rud, "throttle": (-joy.get_axis(2)+1.)*0.5, "flaps": 0. } # if joystick is not being used, gets keyboard input and creates control_state dictionary else: if UP == True and DOWN == False: ele = 1. elif UP == False and DOWN == True: ele = -1. else: ele = 0. if LEFT == True and RIGHT == False: ail = 1. elif LEFT == False and RIGHT == True: ail = -1. else: ail = 0. if AA == True and DD == False: rud = 1. elif AA == False and DD == True: rud = -1. else: rud = 0. if WW == True and SS == False and thr<=1.0: thr += 0.05 elif WW == False and SS == True and thr>=0.0: thr -= 0.05 control_state = { "aileron": ail*d_ail, "elevator": ele*d_ele, "rudder": rud*d_rud, "throttle": thr, "flaps": 0. } #SIMULATION CALCULATIONS GO BELOW HERE FOR EACH TIME STEP #IT IS RECOMMENDED THAT YOU MAKE AN OBJECT FOR THE SIMULATION AIRCRAFT AND CREATE A FUNCTION IN SAID OBJECT TO CALCULATE THE NEXT TIME STEP. #THIS FUNCTION CAN THEN BE CALLED HERE simulation.run_sim(a_obj, dt) #INPUT POSITION, ORIENTATION, AND VELOCITY OF AIRCRAFT INTO THIS DICTIONARY WHICH WILL THEN UPDATE THE GRAPHICS aircraft_condition = { "Position":a_obj.state_vars[6:9],#input position of form [x,y,z] "Orientation":a_obj.state_vars[-4:],#input orientation in quaternion form [e0,ex,ey,ez] "Velocity":a_obj.state_vars[:4] #input Velocity of form [u,v,w] } flight_data = { "Graphics Time Step": t,#sec "Physics Time Step": dt,#sec "Airspeed": a_obj.V_now,#feet/sec "AoA":a_obj.alpha_now ,#deg "Sideslip": a_obj.beta_now ,#deg "Altitude":-a_obj.state_vars[8] ,#feet "Latitude":a_obj.state_vars[6] ,#deg "Longitude":a_obj.state_vars[7] ,#deg "Time":0. ,#sec "Bank":a_obj.bank ,#deg "Elevation":a_obj.elevation ,#deg "Heading":a_obj.heading ,#deg "Gnd Speed":a_obj.V_now ,#feet/sec "Gnd Track":0. ,#deg "Climb":a_obj.climb, #feet/min # "Throttle":control_state["throttle"]*100 ,#% # "Elevator":control_state["elevator"] ,#deg # "Ailerons":control_state["aileron"] ,#deg # "Rudder":control_state["rudder"] ,#deg # "Flaps":control_state["flaps"] ,#deg "Throttle":a_obj.tau_o ,#% "Elevator":a_obj.de_o ,#deg "Ailerons":a_obj.da_o ,#deg "Rudder":a_obj.dr_o ,#deg "Flaps":0.,#deg "Axial G-Force":0. ,#g's "Side G-Force":0. ,#g's "Normal G-Force":0. ,#g's "Roll Rate": a_obj.p_o ,#deg/s "Pitch Rate":a_obj.q_o ,#deg/s "Yaw Rate":a_obj.r_o #deg/s } #apply position and orientation to graphics graphics_aircraft.set_orientation(graphics.swap_quat(aircraft_condition["Orientation"])) graphics_aircraft.set_position(aircraft_condition["Position"]) #test game over conditions if graphics_aircraft.position[2]>0.: LOSE = True #if you get a game over, display lose screen if LOSE == True: glClearColor(0,0,0,1.0) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) gameover.draw(-0.2,-0.05,"Game Over",(0,255,0,1)) PAUSE = True #otherwise, render graphics #Third person view elif FPV == False: #get view matrix and render scene view = cam.third_view(graphics_aircraft) graphics_aircraft.set_view(view) field.set_view(view) graphics_aircraft.render() field.render() if DATA == True: data.render(flight_data) #cockpit view elif FPV == True: view = cam.cockpit_view(graphics_aircraft) field.set_view(view) field.render() if DATA == True: data.render(flight_data) HUD.render(aircraft_condition,view) #update screen display pygame.display.flip()
""" import pygame import sys import graphics from transform import rotz_matrix_3 from transform import rotx_matrix_3 if __name__ == '__main__': # Camera args field_of_view_y = 45 up_vector = (0, 0, 1) pos_target = (0, 0, 0) pos_camera = (0, 4, 0) kwargs = dict(position=pos_camera, mode='perspective') # Define all world objects cam = graphics.Camera(field_of_view_y, up_vector, pos_target, **kwargs) # Place Identity Cube at Origin of World transformation = { 'scaling': (1, 1, 1), 'rotation': 0, 'translation': (0, 0, 0) } obj = graphics.load_cube(transformation=transformation) # Create the world world = graphics.World(scene_objects=[obj]) # define colors black = (0, 0, 0) # run the game pygame.init() w, h = 512, 512 screen = pygame.display.set_mode((w, h))