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 # 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 begin(self): # Set fb device for Raspberry pi os.environ["SDL_FBDEV"] = Config.fb_dev if (Config.detect_screen_size): self.screen_size = (pygame.display.Info().current_w, pygame.display.Info().current_h) # Set the resolution and the frames per second Program.set_mode(self.screen_size, self.full_screen, False) Program.set_fps(self.fps) Program.set_title(self.window_title) # set mouse invisible pygame.mouse.set_visible(False) self.init_joystick() self.font = Game.load_fnt("c64.ttf", 16) self.big_font = Game.load_fnt("c64.ttf", 50) # run scene scene = Main(self); scene.lives = Config.lives scene.bullets = Config.bullets while True: # This is the main loop # Simple input check if Program.key(K_ESCAPE) or (K_ESCAPE in self.read_joystick()): Program.exit() yield
def begin(self, game): self.x, self.y = 400, 500 self.graph = game.g_player while True: # Processes have many member variables, one of the most common is x and y which # directly map to the process' coordinates on the screen. They, like all member # variables, can be altered at any time. # Here we check if the ship should move left/right and change the x (horisontal) # coordinate appropriately. if Program.key(K_LEFT): self.x -= 4 if Program.key(K_RIGHT): self.x += 4 # The final bit of input is shooting. Again we pass in the game object so we can # access graphics and honour the convention set for ourselves when creating the # player and enemies. if Program.key_released(K_SPACE): Bullet(game, self) yield
def begin(self): # set up the screen Program.set_mode((640,480)) Program.set_fps(30) # Create the player Guy() # game loop while True: # Check for pressing escape if Program.key(K_ESCAPE): Program.quit() # Leave frame 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): # set up the screen Program.set_mode((640, 480)) Program.set_fps(30) font = Program.load_fnt("gfx/font.ttf", 20) cheri_font = Program.load_fnt("gfx/cheri.ttf", 30) # Create the text objects text_guys = [] text_guys.append(Program.write(font, 100, 100, 4, "Texts")) text_guys.append(Program.write(font, 200, 150, 4, "Are")) text_guys.append(Program.write(font, 400, 200, 4, "Processes")) text_guys.append(Program.write(font, 550, 250, 4, "Too!")) # This is our scrolly message message = ( "This is the Fenix Pygame library! Who needs the goddamn amiga?" + " Weeee! I'm having far too much fun with this! You'll have to try out the" + " library for yourself. :D Fiona made this in the last bit of 2008. For realsies." + " OMG I think it's gonna loop now..... " + " so i herd you like mudkips..?" ) scroll_text = [] # game loop while True: # strobe the message along the top for single in text_guys: single.colour = (randrange(50, 200), randrange(50, 200), randrange(50, 200)) text_guys[3].angle += 3000 # if we don't have a scrolly message, create it if len(scroll_text) < 1: a, b = 650, 0 for x in message: a += 25 b += 20000 guy = Program.write(cheri_font, a, 420, text=x) guy.mystery_angle = b guy.init_x = a guy.colour = (randrange(50, 200), randrange(50, 200), randrange(50, 200)) scroll_text.append(guy) # loop through the scrolly characters and move them and stuff for single in scroll_text: single.x -= 5 single.y = 420 + Program.get_disty(single.mystery_angle, 30) single.mystery_angle += 20000 if single.x <= -25: scroll_text.remove(single) single.signal(S_KILL) # Check for pressing escape if Program.key(K_ESCAPE): Program.quit() # Leave frame yield
def begin(self, level, x = None, y = None): if (x != None and y != None): self.x = x self.y = y else: self.x = 200 self.y = 200 self.graph = level.g_ship1 self.level = level animate = False while True: if (animate): self.graph = level.g_ship2 else: self.graph = level.g_ship1 animate = not animate if (x == None and y == None): if Program.key(K_UP) or (K_UP in level.game.read_joystick()): self.y -= self.speed if Program.key(K_DOWN) or (K_DOWN in level.game.read_joystick()): self.y += self.speed if Program.key(K_LEFT) or (K_LEFT in level.game.read_joystick()): self.x -= self.speed if Program.key(K_RIGHT) or (K_RIGHT in level.game.read_joystick()): self.x += self.speed # bounds if (self.y < self.graph.get_height()/2): self.y = self.graph.get_height()/2 if (self.x < self.graph.get_width()/2): self.x = self.graph.get_width()/2 if (self.x > self.level.game.screen_size[0] - self.graph.get_width()/2): self.x = self.level.game.screen_size[0] - self.graph.get_width()/2 if (self.y > self.level.game.screen_size[1] - self.graph.get_height()/2): self.y = self.level.game.screen_size[1] - self.graph.get_height()/2 e = self.collision("Enemy1") if e and level.game.millis() - self.last_enemy_collision > 500: self.last_enemy_collision = level.game.millis() level.sound(level.s_ship_collision) self.health -= 10 e = self.collision("Enemy2") if e and level.game.millis() - self.last_enemy_collision > 500: self.last_enemy_collision = level.game.millis() level.sound(level.s_ship_collision) self.health -= 20 e = self.collision("Enemy3") if e and level.game.millis() - self.last_enemy_collision > 500: self.last_enemy_collision = level.game.millis() level.sound(level.s_ship_collision) self.health -= 30 e = self.collision("Enemy4") if e and level.game.millis() - self.last_enemy_collision > 500: self.last_enemy_collision = level.game.millis() level.sound(level.s_ship_collision) self.health -= 50 e = self.collision("Enemy5") if e and level.game.millis() - self.last_enemy_collision > 500: self.last_enemy_collision = level.game.millis() level.sound(level.s_ship_collision) self.health -= 10 e = self.collision("Enemy6") if e and level.game.millis() - self.last_enemy_collision > 500: self.last_enemy_collision = level.game.millis() level.sound(level.s_ship_collision) self.health -= 50 e = self.collision("Enemy7") if e and level.game.millis() - self.last_enemy_collision > 500: self.last_enemy_collision = level.game.millis() level.sound(level.s_ship_collision) self.health -= 20 e = self.collision("Enemy8") if e and level.game.millis() - self.last_enemy_collision > 500: self.last_enemy_collision = level.game.millis() level.sound(level.s_ship_collision) self.health -= 20 if Program.key_released(K_SPACE) or (K_SPACE in level.game.read_joystick() and level.game.millis() - level.game.last_joystick_btn > Config.joystick_button_delay): level.game.last_joystick_btn = level.game.millis() self.fire() yield
def begin(self): # Set fb device for Raspberry pi #os.environ["SDL_FBDEV"] = Config.fb_dev "Ininitializes a new pygame screen using the framebuffer" # Based on "Python GUI in Linux frame buffer" # http://www.karoltomala.com/blog/?p=679 disp_no = os.getenv("DISPLAY") if disp_no: print "I'm running under X display = {0}".format(disp_no) # Check which frame buffer drivers are available # Start with fbcon since directfb hangs with composite output drivers = ['fbcon', 'directfb', 'svgalib'] found = False for driver in drivers: # Make sure that SDL_VIDEODRIVER is set if not os.getenv('SDL_VIDEODRIVER'): os.putenv('SDL_VIDEODRIVER', driver) try: pygame.display.init() except pygame.error: print 'Driver: {0} failed.'.format(driver) continue found = True break if not found: raise Exception('No suitable video driver found!') if (Config.detect_screen_size): self.screen_size = (pygame.display.Info().current_w, pygame.display.Info().current_h) # Set the resolution and the frames per second Program.set_mode(self.screen_size, self.full_screen, False) Program.set_fps(self.fps) #Program.set_title(self.window_title) # set mouse invisible pygame.mouse.set_visible(False) # load playlist self.playlist = Playlist() self.playlist.load(Config.playlist) # init mpd self.mpd = MPDClient() self.mpd.connect(Config.mpd_host, Config.mpd_port) if (Config.mpd_password is not None): self.mpd.password(Config.mpd_password) # collect mpd playlist self.mpd.stop() self.mpd.clear() for item in self.playlist.list: self.mpd.add(item.url) # get active song from saved state self.state = State() self.active_song = self.state.load() self.last_active_song = self.active_song # init serial encoder instance self.encoder = Encoder(self.active_song, 0, len(self.playlist.list)-1) # play self.mpd.play(self.active_song) # run scene scene = Main(self); while True: # This is the main loop # Simple input check if (Program.key(K_ESCAPE)): Program.exit() change_scene_request = self.encoder.try_read() if (self.millis() - self.last_changed >= Config.save_timeout and self.last_active_song != self.active_song): self.last_active_song = self.active_song self.mpd.play(self.active_song) self.state.save(self.active_song) if (Program.key_released(K_SPACE) or Program.key_released(K_RETURN) or change_scene_request == True): change_scene_request = False if (isinstance(scene, Menu)): scene.signal(S_KILL, True) scene = Main(self) elif (isinstance(scene, Main)): scene.signal(S_KILL, True) scene = Menu(self) # 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