def begin(self, x, y): # Initialise some things self.x = x self.y = y self.graph = Program.load_png(os.path.join("gfx", "guy.png")) speed = 0.0 # In-game loop while True: # Do input if Program.key(K_LEFT): self.angle += 10000 if Program.key(K_RIGHT): self.angle -= 10000 if Program.key(K_UP): speed += 2.0 if Program.key(K_DOWN): speed -= 2.0 # normalise speed if speed < -10.0: speed = -10.0 if speed > 10.0: speed = 10.0 speed *= 0.9 # send the player forwards or backwards depending on speed self.advance(int(speed)) # leave frame yield
def begin(self, x, y): # Initialise some things self.x = x self.y = y self.z = -200 self.graph = Program.load_png(os.path.join("gfx", "guy.png")) while True: yield
def begin(self): # Set the resolution and the frames per second Program.set_mode((800, 600)) Program.set_fps(30) # load the graphics and store a pointer to the loaded surfaces. # Assigning them to the main game object is purely a convention # and is not required for pygame-fenix. They are set as members of # the game because it is a persistant process that lasts for the # length of the game. self.g_player = Program.load_png("player.png") self.g_enemy = Program.load_png("enemy.png") self.g_bullet = Program.load_png("bullet.png") # Because it in persistent and holds references to all our graphic # surfaces, it makes sense to pass it to all of our objects. # Again this is purely a convention. A recommended convention, but # entirely optional nontheless. Player(self) # We create the invaders along the top of the screen. for x in range(100, 601, 50): Enemy(self, x) # Note that we do not save references to the processes. Simply the act # of initialising one will cause it to exist. It's internal loop will # execute immediately and it can happily act independantly of everything # else. while True: # This is the main loop # Simple input check if Program.key(K_ESCAPE): Program.exit() # The yield statement signifies that this object is finished for the # current frame, on the next frame the loop will resume here until it # hits the yield statement again. All objects are designed to act this way. yield
def begin(self, x, y): # Initialise some things self.x = x self.y = y # Z order. The lower the Z the "closer" they are to the player. # Guy has a lower Z than Other_guy so will appear on top. self.z = -300 self.graph = Program.load_png(os.path.join("gfx", "guy.png")) speed = 0 other = Other_guy(320, 240) font = Program.load_fnt("gfx/font.ttf", 10) distance_text = Program.write(font, 0, 0) angle_text = Program.write(font, 0, 15) collision_text = Program.write(font, 0, 30) # In-game loop while True: # Do input if Program.key(K_LEFT): self.angle += 10000 if Program.key(K_RIGHT): self.angle -= 10000 if Program.key(K_UP): speed += 2.0 if Program.key(K_DOWN): speed -= 2.0 # normalise speed if speed < -10.0: speed = -10.0 if speed > 10.0: speed = 10.0 speed *= 0.9 # send the player forwards or backwards depending on speed self.advance(int(speed)) # Get distance from other guy distance_text.text = "Distance: "+ str(self.get_dist(other)) angle_text.text = "Angle to: "+ str(int(self.get_angle(other)/1000)) collision_text.text = "Colliding: "+ ("True" if self.collision(other) else "False") # leave frame yield
def load_png(self, filename): dirname, current_filename = os.path.split(os.path.abspath(__file__)) filename = os.path.join(dirname, 'gfx', filename) return Program.load_png(filename)
def __init__(self): # FONTS self.fonts['system'] = Program.load_fnt(os.path.join("fnt","system_font.ttf"), 10) self.fonts['vera'] = Program.load_fnt(os.path.join("fnt","vera.ttf"), 12) self.fonts['mini_vera'] = Program.load_fnt(os.path.join("fnt","vera.ttf"), 11) # GRAPHICS self.player_gfx['ship'] = Program.load_png(os.path.join("gfx", "player.png")) self.player_gfx['crosshair'] = Program.load_png(os.path.join("gfx", "crosshair.png")) self.player_gfx['laser'] = Program.load_png(os.path.join("gfx", "lazor.png")) self.player_gfx['tractor'] = Program.load_png(os.path.join("gfx", "tractor.png")) self.player_gfx['wormhole'] = Program.load_png(os.path.join("gfx", "wormhole.png")) self.player_gfx['hub_arrow'] = Program.load_png(os.path.join("gfx", "hub_arrow.png")) self.gfx_landmarks['spacestation'] = Program.load_png(os.path.join("gfx", "landmark-spacestation.png")) self.gfx_landmarks['cruiser'] = Program.load_png(os.path.join("gfx", "landmark-cruiser.png")) self.gfx_exp = Program.load_png(os.path.join("gfx", "exp.png")) self.gfx_mine = Program.load_png(os.path.join("gfx", "mine.png")) self.gfx_turret = Program.load_png(os.path.join("gfx", "turret.png")) self.gfx_turret_laser = Program.load_png(os.path.join("gfx", "enemylazor.png")) self.gfx_title = Program.load_png(os.path.join("gfx", "title.png")) self.gfx_hud['hold'] = Program.load_png(os.path.join("gfx", "holdhud.png")) self.gfx_hud['hull'] = Program.load_png(os.path.join("gfx", "hullhud.png")) self.gfx_hud['invenbutton'] = self.load_tile_map(Program.load_png(os.path.join("gfx", "hudinvenbutton.png")), size = 105, height = 24) self.gfx_hud['inventory'] = Program.load_png(os.path.join("gfx", "hudinven.png")) self.gfx_hud['hub'] = Program.load_png(os.path.join("gfx", "hudhub.png")) self.gfx_hud['hub_launch'] = self.load_tile_map(Program.load_png(os.path.join("gfx", "hudhub_launch.png")), size = 105, height = 24) self.gfx_hud['hub_repair'] = self.load_tile_map(Program.load_png(os.path.join("gfx", "hudhub_repair.png")), size = 105, height = 24) self.gfx_hud['hub_upgradehull'] = self.load_tile_map(Program.load_png(os.path.join("gfx", "hudhub_upgradehull.png")), size = 156, height = 24) self.gfx_hud['hub_upgradehold'] = self.load_tile_map(Program.load_png(os.path.join("gfx", "hudhub_upgradehold.png")), size = 156, height = 24) self.gfx_hud['hub_upgradetractor'] = self.load_tile_map(Program.load_png(os.path.join("gfx", "hudhub_upgradetractor.png")), size = 174, height = 24) self.gfx_hud['hub_upgradeengine'] = self.load_tile_map(Program.load_png(os.path.join("gfx", "hudhub_upgradeengine.png")), size = 174, height = 24) self.gfx_hud['hub_sellstuff'] = self.load_tile_map(Program.load_png(os.path.join("gfx", "hudhub_sell.png")), size = 105, height = 24) self.gfx_hud['hub_sell'] = Program.load_png(os.path.join("gfx", "hudsell.png")) self.gfx_hud['hub_sell_cancel'] = self.load_tile_map(Program.load_png(os.path.join("gfx", "hudhub_cancel.png")), size = 60, height = 17) self.gfx_hud['hub_sell_sell'] = self.load_tile_map(Program.load_png(os.path.join("gfx", "hudhub_sellall.png")), size = 60, height = 17) self.gfx_debris_inventory = Program.load_png(os.path.join("gfx", "debris", "debris_inventory.png")) for a in xrange(1, DEBRIS_GFX_NUM+1): self.gfx_debris.append( Program.load_png(os.path.join("gfx", "debris", "debris" + str(a) + ".png")) ) self.gfx_debris_particles = self.load_tile_map( Program.load_png(os.path.join("gfx", "debris", "debris_particles.png")), size = 10 ) for a in xrange(1, 6): self.gfx_valuable_debris['tier'+str(a)] = self.load_tile_map( Program.load_png(os.path.join("gfx", "debris", "tier"+str(a)+"_valuables.png")), size = 30 ) self.player_gfx['indicator'] = self.load_tile_map( Program.load_png(os.path.join("gfx", "indicator.png")), size = 4 ) self.gfx_particles_trail = self.load_tile_map( Program.load_png(os.path.join("gfx", "particles", "trail.png")), size = 6 ) for a in xrange(1, 7): self.gfx_tutorial_messages[a] = Program.load_png(os.path.join("gfx", "tut"+str(a)+".png")) # SOUNDS self.sounds['collision'] = pygame.mixer.Sound(os.path.join("sounds", "collision.wav")) self.sounds['wormhole'] = pygame.mixer.Sound(os.path.join("sounds", "wormholeout.wav")) self.sounds['tractor'] = pygame.mixer.Sound(os.path.join("sounds", "tractor.wav")) self.sounds['pickup'] = pygame.mixer.Sound(os.path.join("sounds", "pickup.wav")) self.sounds['laser'] = pygame.mixer.Sound(os.path.join("sounds", "laser.wav")) self.sounds['explosion'] = pygame.mixer.Sound(os.path.join("sounds", "explosion.wav")) self.sounds['beep'] = pygame.mixer.Sound(os.path.join("sounds", "beep.wav")) self.sounds['moneyout'] = pygame.mixer.Sound(os.path.join("sounds", "moneyout.wav")) self.sounds['startfly'] = pygame.mixer.Sound(os.path.join("sounds", "startfly.wav")) self.sounds['flyloop'] = pygame.mixer.Sound(os.path.join("sounds", "flyloop.wav"))