def run(self): timer = clock.Clock() while self.running: dt = timer.tick(60) self.fps_queue.append(1.0/dt) self.handle_events() self.update(dt) self.entity_manager.commit_changes() self.draw() sdl2hl.quit()
import sdl2hl BACKGROUND_COLOR = (0,0,0,255) AVATAR_COLOR = (255,0,0,255) sdl2hl.init() window = sdl2hl.Window() renderer = sdl2hl.Renderer(window) avatar = sdl2hl.Rect(w=64, h=64) while True: for event in sdl2hl.events.poll(): if event.type == sdl2hl.QUIT: sdl2hl.quit() sys.exit() elif event.type == sdl2hl.EventType.keydown and event.keycode == sdl2hl.KeyCode.left: avatar.x -= 1 elif event.type == sdl2hl.EventType.keydown and event.keycode == sdl2hl.KeyCode.right: avatar.x += 1 elif event.type == sdl2hl.EventType.keydown and event.keycode == sdl2hl.KeyCode.up: avatar.y -= 1 elif event.type == sdl2hl.EventType.keydown and event.keycode == sdl2hl.KeyCode.down: avatar.y += 1 renderer.draw_color = BACKGROUND_COLOR renderer.clear() renderer.draw_color = AVATAR_COLOR renderer.fill_rect(avatar)
def run(self, ghost_data, use_mouse): self.ghost_data_replay = ghost_data self.ghost_replay_index = 0 self.ghost_data_store = [] run_start_time = sdl2hl.timer.get_ticks() run_finished = False my_clock = clock.clock() self.player = player.player(self.geometry) while True: dt = my_clock.tick(60) for event in sdl2hl.events.poll(): if event.type == sdl2hl.QUIT: sdl2hl.quit() sys.exit() elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.escape: sdl2hl.quit() sys.exit() elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.up: self.camera.y += 5 elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.down: self.camera.y -= 5 elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.left: self.camera.x -= 5 elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.right: self.camera.x += 5 elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.kp_minus: self.camera.zoom *= 1.05 elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.kp_plus: self.camera.zoom *= .95 elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.r: self.player = player.player(self.geometry) self.ghost_data_store = [] self.ghost_replay_index = 0 run_start_time = sdl2hl.timer.get_ticks() run_finished = False elif event.type == sdl2hl.MOUSEMOTION: self.mouse_screen_x = event.x self.mouse_screen_y = event.y self.mouse_x, self.mouse_y = self.camera.to_world(self.mouse_screen_x, self.mouse_screen_y) # move the camera self.camera.x = self.player.x - 300 self.camera.y = self.geometry.line_height(self.camera.x) - 200 # get the kite angle # TODO poll the mouse for this so it always accurate even if the mouse never moved angle = (self.camera.height / 2.0 - self.mouse_screen_y) / self.camera.height * pi # handle the player self.player.update(dt, angle) # store the ghost t = sdl2hl.timer.get_ticks() - run_start_time self.ghost_data_store.append((t, self.player.get_state())) #check race end if not run_finished and self.player.x > self.geometry.course_length: run_end_time = sdl2hl.timer.get_ticks() run_time = (run_end_time - run_start_time) / 1000.0 # print 'run time', run_time run_finished = True self.player.channel.pause() return run_time, self.ghost_data_store # restore the ghost if self.ghost_data_replay: while self.ghost_replay_index < len(self.ghost_data_replay) - 1 and self.ghost_data_replay[self.ghost_replay_index][0] < t: self.ghost_replay_index += 1 self.ghost.set_state(self.ghost_data_replay[self.ghost_replay_index][1]) self.renderer.draw_color = (0, 0, 0, 255) self.renderer.clear() # draw the slope self.renderer.draw_color = (255, 255, 255, 255) points = [] for x in range(0, self.camera.width): y = self.camera.to_screen_y(self.geometry.height(self.camera.to_world_x(x))) points.append(sdl2hl.Point(x,y)) points.append(sdl2hl.Point(x,self.camera.height)) self.renderer.draw_lines(*points) # draw the finish line finish = self.camera.to_screen_x(self.geometry.course_length) self.renderer.draw_color = (0, 255, 0, 255) self.renderer.draw_line(finish, 0, finish, self.camera.height) # draw the ghost if self.ghost_data_replay: self.ghost.draw(self.renderer, self.camera) # draw the player self.player.draw(self.renderer, self.camera) # text_texture = sdl2hl.Texture.from_surface(self.renderer, self.font.render_solid('Example!', (255,255,255,255))) # self.renderer.copy(text_texture, dest_rect=sdl2hl.Rect(x=100, y=100, w=text_texture.w, h=text_texture.h)) self.renderer.present()
def run(self): # draw a blank frame whiel we load scores self.renderer.draw_color = (0, 0, 0, 255) self.renderer.clear() title = ui.Text(0, 100, "Loading...", 40) title.x = self.window_size[0] / 2 - title.width / 2 title.draw(self.renderer) self.renderer.present() scores = leaderboard.get_hi_scores(5) buttons = [] texts = [] title = ui.Text(0, 100, "Winter Wizard Jam", 40) title.x = self.window_size[0] / 2 - title.width / 2 texts.append(title) how_to_play_lines = [ "Race to the finish line!", "Position your kite with the mouse or joystick.", "Keep the kite in front of you for maximum speed.", "Try to land smoothly to maintain speed.", "Press 'r' for a quick restart", ] x = 200 y = title.y + title.height + 100 for line in how_to_play_lines: s = ui.Text(x, y, line, 30) texts.append(s) y += s.height + 20 y += 30 mouse_text = ui.Text(x + 50, y, "Mouse", 30) texts.append(mouse_text) mouse_button = ui.Button(x, y, mouse_text.height, mouse_text.height) mouse_button.selected = True y += mouse_text.height + 50 stick_text = ui.Text(x + 50, y, "Stick", 30) texts.append(stick_text) stick_button = ui.Button(x, y, stick_text.height, stick_text.height) begin_text = ui.Text(0, self.window_size[1] - 200, "Press any key to begin", 30) begin_text.x = self.window_size[0] / 2 - begin_text.width / 2 texts.append(begin_text) y = title.y + title.height + 100 x = self.window_size[0] / 2 + 300 s = ui.Text(x, y, "Pick a ghost", 30) texts.append(s) y += s.height + 50 for score in scores: s = ui.Text(x + 100, y, score.name + ": " + str(score.time), 30) texts.append(s) buttons.append(ui.Button(x, y, s.height, s.height)) y += s.height + 50 s = ui.Text(x + 100, y, "None", 30) texts.append(s) b = ui.Button(x, y, s.height, s.height) b.selected = True buttons.append(b) button_selected = len(buttons) - 1 y += s.height + 50 my_clock = clock.clock() while True: dt = my_clock.tick(60) for event in sdl2hl.events.poll(): if event.type == sdl2hl.QUIT: sdl2hl.quit() sys.exit() elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.escape: sdl2hl.quit() sys.exit() elif event.type == sdl2hl.KEYDOWN and ( event.keycode == sdl2hl.KeyCode.return_ or event.keycode == sdl2hl.KeyCode.space ): if button_selected == len(buttons) - 1: return (mouse_button.selected, []) else: return (mouse_button.selected, scores[button_selected].ghost) elif event.type == sdl2hl.MOUSEBUTTONDOWN: for i in range(len(buttons)): if buttons[i].intersect_point(event.x, event.y): buttons[button_selected].selected = False button_selected = i buttons[button_selected].selected = True if mouse_button.intersect_point(event.x, event.y): mouse_button.selected = True stick_button.selected = False if stick_button.intersect_point(event.x, event.y): mouse_button.selected = False stick_button.selected = True self.renderer.draw_color = (0, 0, 0, 255) self.renderer.clear() for button in buttons: button.draw(self.renderer) for t in texts: t.draw(self.renderer) mouse_button.draw(self.renderer) stick_button.draw(self.renderer) self.renderer.present()
def run(self, last_time, best_time, best_time_set, last_ghost, best_ghost): scores = leaderboard.get_hi_scores(5) texts = [] buttons = [] if best_time_set: if last_time <= best_time: s = 'New personal best! Your time was ' + str(last_time) + ' seconds.' best_time = last_time best_ghost = last_ghost else: s = 'Your time was ' + str(last_time) + ' seconds. Your personal best is ' + str(best_time) + ' seconds.' else: s = 'Your time was ' + str(last_time) + ' seconds.' best_time = last_time best_ghost = last_ghost title = ui.Text(0, 100, s, 40) title.x = self.window_size[0]/2 - title.width/2 texts.append(title) info_lines = ['Enter your name and submit your score!'] x = 200 y = title.y + title.height + 100 for line in info_lines: s = ui.Text(x, y, line, 30) texts.append(s) y += s.height + 20 y += 30 name_text = ui.Text(x + 10, y + 10, self.name, 30) texts.append(name_text) name_rect = sdl2hl.Rect( x , y, 400, 60) y += 100 submit_text = ui.Text(x + 10, y + 10, 'Submit Score!', 30) texts.append(submit_text) score_submitted = False submit_next_frame = False submit_rect = sdl2hl.Rect(x, y, 250, 60) begin_text = ui.Text(0, self.window_size[1] - 200, "Press 'Enter' to begin", 30) begin_text.x = self.window_size[0]/2 - begin_text.width/2 texts.append(begin_text) y = title.y + title.height + 100 x = self.window_size[0]/2 + 300 s = ui.Text(x, y, 'Pick a ghost', 30) texts.append(s) y += s.height + 50 for score in scores: s = ui.Text(x + 100, y, score.name + ': ' + str(score.time), 30) texts.append(s) buttons.append(ui.Button(x, y, s.height, s.height)) y += s.height + 50 if best_time_set: t = best_time else: t = last_time s = ui.Text(x + 100, y, 'Personal Best' + ': ' + str(t), 30) texts.append(s) b = ui.Button(x, y, s.height, s.height) buttons.append(b) y += s.height + 50 s = ui.Text(x + 100, y, 'Last Run' + ': ' + str(last_time), 30) texts.append(s) b = ui.Button(x, y, s.height, s.height) buttons.append(b) y += s.height + 50 s = ui.Text(x + 100, y, 'None', 30) texts.append(s) b = ui.Button(x, y, s.height, s.height) buttons.append(b) if self.button_selected == -1: self.button_selected = len(buttons) - 1 buttons[self.button_selected].selected = True my_clock = clock.clock() while True: dt = my_clock.tick(60) if submit_next_frame and not score_submitted: leaderboard.post_score(leaderboard.Score(self.name, last_time, last_ghost)) score_submitted = True submit_text.set_text('Submitted!') for event in sdl2hl.events.poll(): if event.type == sdl2hl.QUIT: sdl2hl.quit() sys.exit() elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.escape: sdl2hl.quit() sys.exit() elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.return_: if self.button_selected == len(buttons) - 1: return [] elif self.button_selected == len(buttons) - 2: return last_ghost elif self.button_selected == len(buttons) - 3: return best_ghost else: return scores[self.button_selected].ghost elif event.type == sdl2hl.KEYDOWN and event.keycode == sdl2hl.KeyCode.backspace: if self.name: self.name = self.name[:-1] name_text.set_text(self.name) elif event.type == sdl2hl.TEXTINPUT: if len(self.name) <= self.max_name_length: self.name += event.text name_text.set_text(self.name) elif event.type == sdl2hl.MOUSEBUTTONDOWN: for i in range(len(buttons)): if buttons[i].intersect_point(event.x, event.y): buttons[self.button_selected].selected = False self.button_selected = i buttons[self.button_selected].selected = True if not submit_next_frame and submit_rect.x < event.x < submit_rect.x + submit_rect.w and submit_rect.y < event.y < submit_rect.y + submit_rect.h: submit_text.set_text('Submitting...') submit_next_frame = True self.renderer.draw_color = (0, 0, 0, 255) self.renderer.clear() for t in texts: t.draw(self.renderer) for b in buttons: b.draw(self.renderer) self.renderer.draw_color = (255, 255, 255, 255) self.renderer.draw_rect(name_rect) self.renderer.draw_rect(submit_rect) self.renderer.present()