def main(): pygame.init() screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH) screen.fill((255,255,255)) clock = pygame.time.Clock() #Init Objects guy = Character([32,32], clock) playersprite = pygame.sprite.RenderPlain(guy) #level data wall_data, exit_location = walls.build_level() level = 0 next_level = True up = left = right = change_sprite = timer_running = False menu = Menu(DISPLAY, DEPTH, FLAGS, screen, clock) menu.menu() while True: #limit framerate to 60fps clock.tick(60) pygame.display.set_caption("Testing FPS = %.2f" % (clock.get_fps())) #Events for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE or event.key == pygame.K_q: sys.exit() if event.key == pygame.K_UP or event.key == pygame.K_SPACE: #Alex made me add space to jump.... up = True if event.key == pygame.K_LEFT: left = True if event.key == pygame.K_RIGHT: right = True if event.type == pygame.KEYUP: if event.key == pygame.K_UP or event.key == pygame.K_SPACE: #I forgot to check for space key up, kept jumping forever up = False if event.key == pygame.K_LEFT: left = False if event.key == pygame.K_RIGHT: right = False if event.type == (pygame.USEREVENT + 1): change_sprite = True if event.type == (pygame.USEREVENT + 3): next_level = True if event.type == (pygame.USEREVENT + 2) and next_level: level += 1 wall_data, exit_location = walls.build_level(level) guy = Character([32,32], clock, level) next_level = False pygame.event.clear() pygame.time.set_timer(pygame.USEREVENT + 3, 1000) if (left or right) and not timer_running: pygame.time.set_timer(pygame.USEREVENT + 1, 100) timer_running = True if (left or right) and change_sprite: guy.change_sprite() change_sprite = False timer_running = False #Update Screen #img_test = load_png("guy.png") screen.fill((255,255,255)) for wall in wall_data: pygame.draw.rect(screen, (0,0,0), wall.rect) #draw the exit pygame.draw.rect(screen, (255,0,0), exit_location[0].rect) guy.update(up, left, right) screen.blit(guy.sprite, guy.rect) #print guy.rect pygame.display.flip()
import os class Character(pygame.sprite.Sprite): def __init__(self, (pos), clock, level = 0): pygame.sprite.Sprite.__init__(self) self.still = load_png("guy.png") self.walk1 = load_png("guy2.png") self.walk2 = load_png("guy3.png") self.sprite = self.still self.clock = pygame.time.Clock() self.actual_clock = clock self.rect = self.sprite.get_rect() self.speed = 30 self.pos = pos self.rect.x = pos[0] self.rect.y = pos[1] self.wall_data, self.exit_location = walls.build_level(level) self.on_ground = False self.dy = self.dx = 0 self.frame_rate = 60 def update(self, up, left, right): if self.actual_clock.get_fps() != 0: self.frame_rate = self.actual_clock.get_fps() if up: #if on ground, jump self.jump() if left: #Move left self.dx = (-300 * (1/self.frame_rate)) if right: #Move right