def main(): pygame.init() clock = pygame.time.Clock() running = True commandHandler = CommandHandler() #Constantes REPEAT_DELAY = 30 #milisseconds between each KEYDOWN event (when repeating) KEY_TIMEOUT = 185 #MAX milisseconds between key pressings SCREEN_WIDTH, SCREEN_HEIGHT = (640, 256) screen_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Good Intentions") img_fatguy = pygame.image.load('./gato.png') fatguy = Caracter("jonatas", img_fatguy, 5, 32, 32) parser = sax.make_parser() tmxhandler = TMXHandler() parser.setContentHandler(tmxhandler) parser.parse("fase1.tmx") key_timeout = -1 pygame.key.set_repeat(REPEAT_DELAY*3, REPEAT_DELAY) while running: clock.tick(30) # pygame.draw.circle(screen_surface, color, (400,300) , 30) # screen_surface.fill((255,255,255)) screen_surface.blit(tmxhandler.image, (0,0)) # pygame.display.flip() screen_surface.blit(fatguy.image, fatguy.get_pos()) fatguy.update(pygame.time.get_ticks(), SCREEN_WIDTH, SCREEN_HEIGHT) if key_timeout >= 0: if (pygame.time.get_ticks() - key_timeout) > KEY_TIMEOUT: commandHandler.actual_state = 0 key_timeout = -1 for e in pygame.event.get(): if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE): running = False elif e.type == KEYDOWN: key_timeout = pygame.time.get_ticks() commandHandler.refresh_state(e.key) pygame.display.update() pygame.time.delay(10)
def __init__(self,stage_path,fase_timeout, fatguy_x, fatguy_y, music): pygame.init() self.clock = pygame.time.Clock() self.initial_clock = pygame.time.get_ticks() self.running = True self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) self.fase_timeout = fase_timeout self.shake = (0,0) self.shake_amplitude = 10 # Loading content... pygame.display.set_caption("Good Intentions") self.font = pygame.font.Font(os.path.join('', 'data', 'actionj.ttf'), 70) self.font_small = pygame.font.Font(os.path.join('', 'data', 'actionj.ttf'), 28) self.km_label_text = self.font_small.render('Km/h', 1, (200,205,215)) self.time_display = pygame.image.load(os.path.join('', 'images', 'display.png')) self.speed_display = pygame.image.load(os.path.join('', 'images', 'display_speed.png')) self.img_fatguy = pygame.image.load(os.path.join('', 'images', 'sprite.png')) #Sounds self.bg_music = Music() self.bg_music.load_music(music) self.beep = SoundEffect() # Loading Rolando self.fatguy = Caracter("Rolando", self.img_fatguy, 10, 115, 115, 25) self.commandHandler = CommandHandler(self.fatguy) self.fatguy_x = fatguy_x%5120 self.fatguy_y = fatguy_y self.fatguy.real_x = fatguy_x self.fatguy.x = ROLANDO_DISTANCE self.fatguy.y = self.fatguy_y # Initial Speed of the camera self.cam_speed = (6,0) # Loading the stage self.world_map = TileMapParser().parse_decode(stage_path) self.world_map.load(ImageLoaderPygame()) # Creating list of collision objects self.ground_objects = get_ground_objects(self.world_map) self.killer_objects = get_killer_objects(self.world_map) self.checkpoint_objects = get_checkpoint_objects(self.world_map) self.goal = get_goal_objects(self.world_map) self.slices = set_slices(self.world_map, SLICE_SIZE, SLICE_SIZE_PIXEL) self.key_timeout = -1 # Create sprites groups for collision detection self.playerGroup = pygame.sprite.RenderUpdates() self.playerGroup.add(self.fatguy) #self.objectGroup = pygame.sprite.Group() #self.enemyGroup = pygame.sprite.Group() #self.sceneGroup = pygame.sprite.Group() # Controlling the movement of the background self.offset = self.initialize_slices() - ROLANDO_DISTANCE self.secs = int((self.fase_timeout) / 1000) self.transition = False self.checkpoint_clock = fase_timeout if(self.offset + SCREEN_WIDTH) > SLICE_SIZE_PIXEL: self.actual_slice = self.slices.pop(0) self.past_slice = self.actual_slice self.actual_slice = self.slices.pop(0) self.transition = True else: self.actual_slice = self.slices.pop(0) self.past_slice = self.actual_slice # Controlling the reset self.reset_delay = RESET_DELAY
class Game: def __init__(self,stage_path,fase_timeout, fatguy_x, fatguy_y, music): pygame.init() self.clock = pygame.time.Clock() self.initial_clock = pygame.time.get_ticks() self.running = True self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) self.fase_timeout = fase_timeout self.shake = (0,0) self.shake_amplitude = 10 # Loading content... pygame.display.set_caption("Good Intentions") self.font = pygame.font.Font(os.path.join('', 'data', 'actionj.ttf'), 70) self.font_small = pygame.font.Font(os.path.join('', 'data', 'actionj.ttf'), 28) self.km_label_text = self.font_small.render('Km/h', 1, (200,205,215)) self.time_display = pygame.image.load(os.path.join('', 'images', 'display.png')) self.speed_display = pygame.image.load(os.path.join('', 'images', 'display_speed.png')) self.img_fatguy = pygame.image.load(os.path.join('', 'images', 'sprite.png')) #Sounds self.bg_music = Music() self.bg_music.load_music(music) self.beep = SoundEffect() # Loading Rolando self.fatguy = Caracter("Rolando", self.img_fatguy, 10, 115, 115, 25) self.commandHandler = CommandHandler(self.fatguy) self.fatguy_x = fatguy_x%5120 self.fatguy_y = fatguy_y self.fatguy.real_x = fatguy_x self.fatguy.x = ROLANDO_DISTANCE self.fatguy.y = self.fatguy_y # Initial Speed of the camera self.cam_speed = (6,0) # Loading the stage self.world_map = TileMapParser().parse_decode(stage_path) self.world_map.load(ImageLoaderPygame()) # Creating list of collision objects self.ground_objects = get_ground_objects(self.world_map) self.killer_objects = get_killer_objects(self.world_map) self.checkpoint_objects = get_checkpoint_objects(self.world_map) self.goal = get_goal_objects(self.world_map) self.slices = set_slices(self.world_map, SLICE_SIZE, SLICE_SIZE_PIXEL) self.key_timeout = -1 # Create sprites groups for collision detection self.playerGroup = pygame.sprite.RenderUpdates() self.playerGroup.add(self.fatguy) #self.objectGroup = pygame.sprite.Group() #self.enemyGroup = pygame.sprite.Group() #self.sceneGroup = pygame.sprite.Group() # Controlling the movement of the background self.offset = self.initialize_slices() - ROLANDO_DISTANCE self.secs = int((self.fase_timeout) / 1000) self.transition = False self.checkpoint_clock = fase_timeout if(self.offset + SCREEN_WIDTH) > SLICE_SIZE_PIXEL: self.actual_slice = self.slices.pop(0) self.past_slice = self.actual_slice self.actual_slice = self.slices.pop(0) self.transition = True else: self.actual_slice = self.slices.pop(0) self.past_slice = self.actual_slice # Controlling the reset self.reset_delay = RESET_DELAY # Calcule the first slice of the stage def initialize_slices(self): offset = self.fatguy.real_x real_x = SLICE_SIZE_PIXEL while(offset > SLICE_SIZE_PIXEL): g_object = self.ground_objects[0] k_object = self.killer_objects[0] c_object = self.checkpoint_objects[0] while(g_object[0] + g_object[2] < real_x): self.ground_objects.pop(0) g_object = self.ground_objects[0] while(k_object.rect.x + k_object.rect.width < real_x): self.killer_objects.pop(0) k_object = self.killer_objects[0] while(c_object[0] + c_object[2] < real_x): self.checkpoint_objects.pop(0) c_object = self.checkpoint_objects[0] self.slices.pop(0) real_x += SLICE_SIZE_PIXEL offset -= SLICE_SIZE_PIXEL return offset # The main function of the class def main_loop(self): self.bg_music.play_load_music(1) while self.running: self.clock.tick(90) self.recicle() self.draw_background() self.draw_hud() self.update_fatguy() self.killer_collision() self.ground_collision() self.checkpoint_collision() self.goal_collision() self.draw_fatguy() self.event_handler() self.bg_music.pause_music() # Return the position of Rolando if self.fatguy.dead: return self.fatguy_x, self.fatguy_y, self.checkpoint_clock, False #perdeu! else: return 0, 0, 0, True #ganhou! # Throw away utilized objects def recicle(self): g_object = self.ground_objects[0] try: k_object = self.killer_objects[0] except IndexError: running = False # prolog = Story('suceed01', 7) # prolog.play() c_object = self.checkpoint_objects[0] if g_object[0] + g_object[2] <= self.fatguy.real_x: self.ground_objects.pop(0) if k_object.rect.x + k_object.rect.width < self.fatguy.real_x: self.killer_objects.pop(0) if c_object[0] <= self.fatguy.real_x: self.checkpoint_objects.pop(0) # Calcule and Draw the Background def draw_background(self): if self.transition: join_point = SLICE_SIZE_PIXEL - self.offset self.screen.blit(self.past_slice.subsurface(self.offset, 0, join_point, SCREEN_HEIGHT),self.shake) self.screen.blit(self.actual_slice.subsurface(0 ,0, (self.offset + SCREEN_WIDTH - SLICE_SIZE_PIXEL), SCREEN_HEIGHT),(join_point + self.shake[0],self.shake[1])) if join_point < 0: self.offset = 0 self.transition = False else: try: self.screen.blit(self.actual_slice.subsurface((self.offset,0,SCREEN_WIDTH, SCREEN_HEIGHT)), self.shake) except ValueError: running = False # prolog = Story('suceed01', 7) # prolog.play() self.offset += self.cam_speed[0] if(self.offset + SCREEN_WIDTH) > SLICE_SIZE_PIXEL and self.transition == False: self.past_slice = self.actual_slice if len(self.slices) == 0: return None self.actual_slice = self.slices.pop(0) self.transition = True # Draw the head's up display def draw_hud(self): secs_before = self.secs time = int(self.fase_timeout - (pygame.time.get_ticks() - self.initial_clock)) self.secs = time / 1000 decs = ((time % 1000) / 10) if self.secs < 10: # Time's up if self.secs <= 0 and decs <= 0: self.fatguy.dead = True #FIXME: the time cannot be negative self.secs = 0 decs = 0 if secs_before > self.secs: self.beep.play_effect('beep.ogg', 1, 0) timeup_text = self.font.render('0' + str(self.secs) + ':' + str(decs), 1, (200,5,15)) else: timeup_text = self.font.render(str(self.secs) + ':' + str(decs), 1, (160,200,180)) self.screen.blit(self.time_display, (730,-3)) self.screen.blit(timeup_text, Rect(785, 7, 300, 90)) speed_m_per_s = float(self.fatguy.dx / 64.0) * 100 speed_km_per_h = int(speed_m_per_s * 3.6) speed_text = self.font.render(str(speed_km_per_h), 1, (200,205,215)) self.screen.blit(self.speed_display, (788,668)) self.screen.blit(speed_text, Rect(820, 690, 300, 90)) self.screen.blit(self.km_label_text, (930, 682)) # Draw the fatguy def draw_fatguy(self): self.screen.blit(self.fatguy.image, self.fatguy.get_pos()) # Update the fatguy's variables and change his animation def update_fatguy(self): self.fatguy.update(pygame.time.get_ticks(), SCREEN_WIDTH, SCREEN_HEIGHT, self.cam_speed) if self.key_timeout >= 0: if (pygame.time.get_ticks() - self.key_timeout) > KEY_TIMEOUT: self.commandHandler.actual_state = 0 self.key_timeout = -1 adjust = 0 # Check if the Fatguy's too high if self.fatguy.falling and self.ground_objects[0][1] - self.fatguy.apex_height > 335: print self.fatguy.apex_height, self.ground_objects[0][1] self.fatguy.doTooHigh() # Rolando's falling in the hole if self.fatguy.y > SCREEN_HEIGHT: self.fatguy.dead = True if not self.fatguy.dead: if self.fatguy.sprinting: adjust = -4 if self.fatguy.x > 100: adjust += 2 else: adjust = 0 self.cam_speed = (self.fatguy.dx + adjust,0) else: self.reset_delay -= 1 if self.reset_delay == 0: fade_out(self.screen,self.clock) self.running = False # Collides with Killer Objects def killer_collision(self): obj, col_type = self.fatguy.collides_with_objects(self.killer_objects) if col_type == 1: if not self.fatguy.dead: if pygame.sprite.collide_mask(self.fatguy, obj): print self.fatguy.real_x , obj.rect, obj.rect.left, self.fatguy.real_x > obj.rect.left if self.fatguy.real_x > obj.rect.left: self.fatguy.doCrashDown() else: self.fatguy.doCrashSide(obj.rect.x) self.fatguy.sprinting = False self.fatguy.onGround = True self.fatguy.dead = True self.fatguy.crash.play_effect('crash.wav', 1, 1) else: if self.shake_amplitude > 0: self.shake = (randrange(-self.shake_amplitude,self.shake_amplitude),randrange(-self.shake_amplitude,self.shake_amplitude) + random()) self.shake_amplitude -= 1 # Collides with the ground def ground_collision(self): obj, col_type = self.fatguy.collides_with_objects(self.ground_objects) if col_type == 1: self.fatguy.put_on_ground_running(obj[1]) if self.fatguy.broken_legs: if self.shake_amplitude > 0: self.shake = (randrange(-self.shake_amplitude,self.shake_amplitude),randrange(-self.shake_amplitude,self.shake_amplitude) + random()) self.shake_amplitude -= 1 else: self.fatguy.onGround = False # Collides with the goal def goal_collision(self): if self.fatguy.rect.colliderect(Rect(self.goal[0],self.goal[1],self.goal[2],self.goal[3])): print("Colidiu com o goal!") self.running = False #else: print 'NAO COLIDIU' # Collides with the checkpoint def checkpoint_collision(self): check_obj, check_collision = self.fatguy.collides_with_objects(self.checkpoint_objects) if check_collision == 1: self.fatguy_x = check_obj[0] self.fatguy_y = check_obj[1] + check_obj[3] -115 self.checkpoint_clock = self.secs*1000 + 10000 # 5 segundos a mais def event_handler(self): for e in pygame.event.get(): if e.type == QUIT: sys.exit() elif (e.type == KEYDOWN and e.key == K_ESCAPE): self.running = False elif e.type == KEYDOWN: if not self.fatguy.dead: self.key_timeout = pygame.time.get_ticks() self.fatguy.state = self.commandHandler.refresh_state(e.key) elif e.type == KEYUP: if not self.fatguy.dead: if e.key == K_UP: self.fatguy.stopJump() if e.key == K_DOWN: if not self.fatguy.onGround: self.fatguy.pendingGetDown = False if self.fatguy.sliding: self.fatguy.stopGetDown() pygame.display.flip() def __del__(self): self = None del self
def main(): pygame.init() clock = pygame.time.Clock() running = True #Constantes SLICE_SIZE_PIXEL = 5120 SLICE_SIZE = 80 REPEAT_DELAY = 50 #milisseconds between each KEYDOWN event (when repeating) KEY_TIMEOUT = 185 #MAX milisseconds between key pressings SCREEN_WIDTH, SCREEN_HEIGHT = (1024, 640) screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Good Intentions") img_fatguy = pygame.image.load(os.path.join('', 'images', 'sprite.png')) fatguy = Caracter("jonatas", img_fatguy, 10, 115, 115, 25) commandHandler = CommandHandler(fatguy) cam_speed = (4,0) fatguy.set_pos(200,200) world_map = TileMapParser().parse_decode("huge_objects.tmx") world_map.load(ImageLoaderPygame()) ground_objects = get_ground_objects(world_map) killer_objects = get_killer_objects(world_map) print killer_objects slices = set_slices(world_map, SLICE_SIZE, SLICE_SIZE_PIXEL) key_timeout = -1 #create sprites groups for collision detection playerGroup = pygame.sprite.RenderUpdates() playerGroup.add(fatguy) objectGroup = pygame.sprite.Group() enemyGroup = pygame.sprite.Group() sceneGroup = pygame.sprite.Group() pygame.key.set_repeat(REPEAT_DELAY, REPEAT_DELAY) offset = 0 actual_slice = slices.pop(0) past_slice = actual_slice transition = False while running: clock.tick(90) #blit level---------------------------------------------------------------------------------- if transition: join_point = SLICE_SIZE_PIXEL - offset screen.blit(past_slice.subsurface(offset, 0, join_point, SCREEN_HEIGHT),(0,0)) screen.blit(actual_slice.subsurface(0 ,0, (offset + SCREEN_WIDTH - SLICE_SIZE_PIXEL), SCREEN_HEIGHT),(join_point,0)) if join_point < 0: offset = 0 transition = False else: screen.blit(actual_slice.subsurface((offset,0,SCREEN_WIDTH, SCREEN_HEIGHT)), (0, 0)) offset += cam_speed[0] if(offset + SCREEN_WIDTH) > SLICE_SIZE_PIXEL and transition == False: past_slice = actual_slice if len(slices) == 0: break actual_slice = slices.pop(0) transition = True #---------------------------------------------------------------------------------------------- screen.blit(fatguy.image, fatguy.get_pos()) fatguy.update(pygame.time.get_ticks(), SCREEN_WIDTH, SCREEN_HEIGHT, cam_speed) obj, col_type = fatguy.collides_with_objects(ground_objects) if col_type == 1: fatguy.put_on_ground_running(obj[1]) elif col_type == 2: running = False obj, col_type = fatguy.collides_with_objects(killer_objects) if col_type != -1: running = False if key_timeout >= 0: if (pygame.time.get_ticks() - key_timeout) > KEY_TIMEOUT: commandHandler.actual_state = 0 key_timeout = -1 for e in pygame.event.get(): if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE): running = False elif e.type == KEYDOWN: key_timeout = pygame.time.get_ticks() fatguy.state = commandHandler.refresh_state(e.key) if not fatguy.alive(): print 'Game Over' pygame.time.wait(2000) sys.exit() # pygame.display.update() pygame.display.flip()
def demo_pygame(file_name): SLICE_SIZE = 80 SLICE_SIZE_PIXEL = 5120 pygame = __import__("pygame") # parser the map world_map = TileMapParser().parse_decode(file_name) # init pygame and set up a screen pygame.init() img_fatguy = pygame.image.load(os.path.join("", "images", "sprite.png")) fatguy = Caracter("jonatas", img_fatguy, 10, 115, 115, 20) fatguy.set_pos(200, 269) fatguy.dx = 0 clock = pygame.time.Clock() pygame.display.set_caption("tiledtmxloader - " + file_name) screen_width = min(1024, world_map.pixel_width) screen_height = min(768, world_map.pixel_height) screen = pygame.display.set_mode((screen_width, screen_height), HWSURFACE | DOUBLEBUF) world_map.load(ImageLoaderPygame()) slices = set_slices(world_map, SLICE_SIZE, SLICE_SIZE_PIXEL) # load the images using pygame # printer(world_map) # an example on how to access the map data and draw an orthoganl map # draw the map assert world_map.orientation == "orthogonal" print "slices =", slices running = True dirty = True # cam_offset is for scrolling cam_offset_x = 0 cam_offset_y = 0 offset = 0 actual_slice = slices.pop(0) past_slice = actual_slice transition = False # mainloop while running: clock.tick(90) # eventhandling events = pygame.event.get() for event in events: dirty = True if event.type == pygame.QUIT: running = False if transition: join_point = SLICE_SIZE_PIXEL - offset screen.blit(past_slice.subsurface(offset, 0, join_point, screen_height), (0, 0)) screen.blit( actual_slice.subsurface(0, 0, (offset + screen_width - SLICE_SIZE_PIXEL), screen_height), (join_point, 0), ) if join_point < 0: offset = 0 transition = False else: screen.blit(actual_slice.subsurface((offset, 0, screen_width, screen_height)), (0, 0)) offset += 4 if (offset + screen_width) > SLICE_SIZE_PIXEL and transition == False: past_slice = actual_slice if len(slices) == 0: break actual_slice = slices.pop(0) transition = True screen.blit(fatguy.image, fatguy.get_pos()) fatguy.update(pygame.time.get_ticks(), screen_width, screen_height) pygame.display.flip()
def main(): pygame.init() clock = pygame.time.Clock() running = True commandHandler = CommandHandler() #Constantes REPEAT_DELAY = 30 #milisseconds between each KEYDOWN event (when repeating) KEY_TIMEOUT = 185 #MAX milisseconds between key pressings SCREEN_WIDTH, SCREEN_HEIGHT = (640, 480) screen_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Good Intentions") img_fatguy = pygame.image.load(os.path.join('', 'images', 'fat.png')) fatguy = Caracter("jonatas", img_fatguy, 10, 115, 115, 25) # fatguy.set_pos(0,240) fatguy.set_pos(0,325) parser = sax.make_parser() tmxhandler = TMXHandler() parser.setContentHandler(tmxhandler) parser.parse("grande.tmx") key_timeout = -1 #create sprites groups for collision detection playerGroup = pygame.sprite.RenderUpdates() playerGroup.add(fatguy) objectGroup = pygame.sprite.Group() enemyGroup = pygame.sprite.Group() sceneGroup = pygame.sprite.Group() pygame.key.set_repeat(REPEAT_DELAY*3, REPEAT_DELAY) while running: clock.tick(30) screen_surface.fill((255,255,255)) screen_surface.blit(tmxhandler.image, (0,0)) tmxhandler.image.scroll(-10,0) screen_surface.blit(fatguy.image, fatguy.get_pos()) fatguy.update(pygame.time.get_ticks(), SCREEN_WIDTH, SCREEN_HEIGHT) if key_timeout >= 0: if (pygame.time.get_ticks() - key_timeout) > KEY_TIMEOUT: commandHandler.actual_state = 0 key_timeout = -1 for e in pygame.event.get(): if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE): running = False elif e.type == KEYDOWN: key_timeout = pygame.time.get_ticks() fatguy.state = commandHandler.refresh_state(e.key) if not fatguy.alive(): print 'Game Over' pygame.time.wait(2000) sys.exit() # pygame.display.update() pygame.display.flip()