def step(self, game, state, frametime): # TODO: Aim at nearest enemy # TODO: Clean up if self.hp <= 0: self.destroy(state) self.target_queue = [] #clear the list for obj in state.entities.values(): if isinstance(obj, character.Character) and math.hypot(self.x-obj.x,self.y - obj.y) <= self.detection_radius: target_tuple = (obj, math.hypot(self.x-obj.x,self.y - obj.y)) self.target_queue.append(target_tuple) if len(self.target_queue) > 0: #TODO: implement point_direction and adjust priorities accordingly self.target_queue.sort(key= lambda distance: distance[1]) #sort by the second item in the tuples; distance self.nearest_target = self.target_queue[0][0] #get the first part of tuple target_character = state.entities[self.nearest_target.id] target_angle = function.point_direction(self.x,self.y,target_character.x,target_character.y) self.direction = target_angle if target_character.x > self.x and self.turret_flip == True: self.rotating = True elif target_character.x < self.x and self.turret_flip == False: self.rotating = True else: self.nearest_target = -1 if self.nearest_target == -1 and self.flip != self.turret_flip: #reset to old position self.rotating = True self.direction = self.default_direction if self.rotating == True: self.rotateindex += 0.15 if (self.rotateindex >= self.rotateend): self.rotating = False self.turret_flip = not self.turret_flip self.rotateindex = self.rotatestart
def step(self, game, state, frametime): self.speed += 30 # Copied from GMK-GG2; should simulate some very basic acceleration+air resistance. # Gravitational force self.vspeed -= 4.5 # calculate direction self.direction = function.point_direction(self.x - self.hspeed, self.y - self.vspeed, self.x, self.y) print(self.direction)
def step(self, game, state, frametime): # gravitational force self.vspeed += 4.5 * frametime # calculate direction self.direction = function.point_direction(self.x - self.hspeed, self.y - self.vspeed, self.x, self.y)
def step(self, game, state, frametime): self.speed += 30 # Copied from GMK-GG2; should simulate some very basic acceleration+air resistance. self.speed *= 0.92 self.hspeed = math.cos(math.radians(self.direction)) * self.speed self.vspeed = math.sin(math.radians(self.direction)) * -self.speed # calculate direction self.direction = function.point_direction(self.x - self.hspeed, self.y - self.vspeed, self.x, self.y)
def gather_input(self, window, game): self.keys = { "up": sfml.Keyboard.is_key_pressed(sfml.Keyboard.W), "down": sfml.Keyboard.is_key_pressed(sfml.Keyboard.S), "left": sfml.Keyboard.is_key_pressed(sfml.Keyboard.A), "right": sfml.Keyboard.is_key_pressed(sfml.Keyboard.D) } self.up = self.keys["up"] self.down = self.keys["down"] self.left = self.keys["left"] self.right = self.keys["right"] self.leftmouse = sfml.Mouse.is_button_pressed(sfml.Mouse.LEFT) self.middlemouse = sfml.Mouse.is_button_pressed(sfml.Mouse.MIDDLE) self.rightmouse = sfml.Mouse.is_button_pressed(sfml.Mouse.RIGHT) mouse_x, mouse_y = sfml.Mouse.get_position(window) self.aimdirection = function.point_direction(window.width / 2, window.height / 2, mouse_x, mouse_y) bytestr = self.serialize_input() event = networking.event_serialize.ClientEventInputstate(bytestr) game.sendbuffer.append(event)
def gather_input(self, window, game): self.keys = { "up": sfml.Keyboard.is_key_pressed(sfml.Keyboard.W), "down": sfml.Keyboard.is_key_pressed(sfml.Keyboard.S), "left": sfml.Keyboard.is_key_pressed(sfml.Keyboard.A), "right": sfml.Keyboard.is_key_pressed(sfml.Keyboard.D), } self.up = self.keys["up"] self.down = self.keys["down"] self.left = self.keys["left"] self.right = self.keys["right"] self.leftmouse = sfml.Mouse.is_button_pressed(sfml.Mouse.LEFT) self.middlemouse = sfml.Mouse.is_button_pressed(sfml.Mouse.MIDDLE) self.rightmouse = sfml.Mouse.is_button_pressed(sfml.Mouse.RIGHT) mouse_x, mouse_y = sfml.Mouse.get_position(window) self.aimdirection = function.point_direction(window.width / 2, window.height / 2, mouse_x, mouse_y) bytestr = self.serialize_input() event = networking.event_serialize.ClientEventInputstate(bytestr) game.sendbuffer.append(event)
def step(self): #game loop while True: self.networker.recieve(self.game, self) if self.networker.has_connected: self.window.poll_events() # check if user exited the game if not self.window.is_open() or self.window.is_key_pressed(key.ESCAPE): event = networking.event_serialize.ClientEventDisconnect() self.networker.sendbuffer.append(event) break # handle input self.oldkeys = self.keys self.keys = get_input(self.window) leftmouse = self.window.is_mouse_button_pressed(mouse.LEFT) middlemouse = self.window.is_mouse_button_pressed(mouse.MIDDLE) rightmouse = self.window.is_mouse_button_pressed(mouse.RIGHT) mouse_x, mouse_y = self.window.get_mouse_position() our_player = self.game.current_state.players[self.our_player_id] our_player.up = self.keys["up"] our_player.down = self.keys["down"] our_player.left = self.keys["left"] our_player.right = self.keys["right"] our_player.leftmouse = leftmouse our_player.middlemouse = middlemouse our_player.rightmouse = rightmouse our_player.aimdirection = function.point_direction(self.window.width / 2, self.window.height / 2, mouse_x, mouse_y) if self.window.is_key_pressed(key._1): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_SCOUT) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key._2): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_PYRO) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key._3): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_SOLDIER) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key._4): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_HEAVY) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key._6): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_MEDIC) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key._7): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_ENGINEER) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key._8): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_SPY) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key.Q): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_QUOTE) self.networker.events.append((self.networker.sequence, event)) #This for loop detects to see if a key has been pressed. Currently useful for precision offsets #DEBUGTOOL for keypress in self.pressed_list: if self.window.is_key_pressed(keypress) == True and self.pressed_dict[keypress] == False: self.pressed_dict[keypress] = True if keypress == key.LEFT: self.game.horizontal -= 1 if keypress == key.RIGHT: self.game.horizontal += 1 self.pressed_right = True if keypress == key.UP: self.game.vertical -= 1 if keypress == key.DOWN: self.game.vertical += 1 if keypress == key.LEFT_SHIFT: print("HORIZONTAL OFFSET = " + str(self.game.horizontal)) print("VERTICAL OFFSET = " + str(self.game.vertical)) elif self.window.is_key_pressed(keypress) == False: self.pressed_dict[keypress] = False # did we just release the F11 button? if yes, go fullscreen if self.window.is_key_pressed(key.F11): self.window.fullscreen = not self.window.fullscreen # update the game and render frame_time = self.clock.tick() frame_time = min(0.25, frame_time) # a limit of 0.25 seconds to prevent complete breakdown self.fpscounter_accumulator += frame_time self.networker.recieve(self.game, self) self.game.update(self.networker, frame_time) self.renderer.render(self, self.game, frame_time) if self.network_update_timer >= constants.INPUT_SEND_FPS: self.networker.update(self) self.network_update_timer = 0 else: self.network_update_timer += frame_time if self.fpscounter_accumulator > 0.5: self.window.title = "PyGG2 - %d FPS" % self.window.get_fps() print "%d FPS" % self.window.get_fps() self.fpscounter_accumulator = 0.0 self.window.flip() self.cleanup()
def step(self, game, state, frametime): # calculate direction self.direction = function.point_direction(self.x - self.hspeed, self.y - self.vspeed, self.x, self.y)
def run(self): # game loop while True: self.networker.recieve(self.game, self) if self.networker.has_connected: self.window.poll_events() # check if user exited the game if not self.window.is_open() or self.window.is_key_pressed(key.ESCAPE): break # handle input self.oldkeys = self.keys self.keys = get_input(self.window) leftmouse = self.window.is_mouse_button_pressed(mouse.LEFT) middlemouse = self.window.is_mouse_button_pressed(mouse.MIDDLE) rightmouse = self.window.is_mouse_button_pressed(mouse.RIGHT) mouse_x, mouse_y = self.window.get_mouse_position() our_player = self.game.current_state.players[self.our_player_id] our_player.up = self.keys["up"] our_player.down = self.keys["down"] our_player.left = self.keys["left"] our_player.right = self.keys["right"] our_player.leftmouse = leftmouse our_player.middlemouse = middlemouse our_player.rightmouse = rightmouse our_player.aimdirection = function.point_direction(self.window.width / 2, self.window.height / 2, mouse_x, mouse_y) if self.window.is_key_pressed(key._1): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_SCOUT) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key._2): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_PYRO) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key._3): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_SOLDIER) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key._4): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_HEAVY) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key._7): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_ENGINEER) self.networker.events.append((self.networker.sequence, event)) elif self.window.is_key_pressed(key._8): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_SPY) self.networker.events.append((self.networker.sequence, event)) # did we just release the F11 button? if yes, go fullscreen if self.window.is_key_pressed(key.F11): self.window.fullscreen = not self.window.fullscreen # update the game and render frame_time = self.clock.tick() frame_time = min(0.25, frame_time) # a limit of 0.25 seconds to prevent complete breakdown self.fpscounter_accumulator += frame_time self.networker.recieve(self.game, self) self.game.update(self.networker, frame_time) self.renderer.render(self, self.game, frame_time) if self.network_update_timer >= constants.INPUT_SEND_FPS: self.networker.update(self) self.network_update_timer = 0 else: self.network_update_timer += frame_time if self.fpscounter_accumulator > 0.5: self.window.title = "PyGG2 - %d FPS" % self.window.get_fps() self.fpscounter_accumulator = 0.0 self.window.flip() self.quit()
def step(self): #game loop running = True while True: self.networker.recieve(self.game, self) if self.networker.has_connected: # check if user exited the game if not self.window.open or running == False: self.window.close() break leftmouse = False #main input handling loop for event in self.window.iter_events(): if event.type == sfml.Event.CLOSED: #Press the 'x' button running = False elif event.type == sfml.Event.LOST_FOCUS: self.window_focused = False elif event.type == sfml.Event.GAINED_FOCUS: self.window_focused = True elif event.type == sfml.Event.KEY_PRESSED: #Key handler if event.code == sfml.Keyboard.ESCAPE: running = False elif event.code == sfml.Keyboard.LEFT: self.game.horizontal -= 1 elif event.code == sfml.Keyboard.RIGHT: self.game.horizontal += 1 elif event.code == sfml.Keyboard.UP: self.game.vertical -= 1 elif event.code == sfml.Keyboard.DOWN: self.game.vertical += 1 elif event.code == sfml.Keyboard.L_SHIFT: print("HORIZONTAL OFFSET = " + str(self.game.horizontal)) print("VERTICAL OFFSET = " + str(self.game.vertical)) # handle input if window is focused self.oldkeys = self.keys self.keys = get_input(self.window) if self.window_focused: leftmouse = sfml.Mouse.is_button_pressed(sfml.Mouse.LEFT) middlemouse = sfml.Mouse.is_button_pressed(sfml.Mouse.MIDDLE) rightmouse = sfml.Mouse.is_button_pressed(sfml.Mouse.RIGHT) mouse_x, mouse_y = sfml.Mouse.get_position(self.window) our_player = self.game.current_state.players[self.our_player_id] our_player.up = self.keys["up"] our_player.down = self.keys["down"] our_player.left = self.keys["left"] our_player.right = self.keys["right"] our_player.leftmouse = leftmouse our_player.middlemouse = middlemouse our_player.rightmouse = rightmouse our_player.aimdirection = function.point_direction(self.window.width / 2, self.window.height / 2, mouse_x, mouse_y) if sfml.Keyboard.is_key_pressed(sfml.Keyboard.NUM1): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_SCOUT) self.networker.events.append((self.networker.sequence, event)) elif sfml.Keyboard.is_key_pressed(sfml.Keyboard.NUM2): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_PYRO) self.networker.events.append((self.networker.sequence, event)) elif sfml.Keyboard.is_key_pressed(sfml.Keyboard.NUM3): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_SOLDIER) self.networker.events.append((self.networker.sequence, event)) elif sfml.Keyboard.is_key_pressed(sfml.Keyboard.NUM4): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_HEAVY) self.networker.events.append((self.networker.sequence, event)) elif sfml.Keyboard.is_key_pressed(sfml.Keyboard.NUM6): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_MEDIC) self.networker.events.append((self.networker.sequence, event)) elif sfml.Keyboard.is_key_pressed(sfml.Keyboard.NUM7): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_ENGINEER) self.networker.events.append((self.networker.sequence, event)) elif sfml.Keyboard.is_key_pressed(sfml.Keyboard.NUM8): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_SPY) self.networker.events.append((self.networker.sequence, event)) elif sfml.Keyboard.is_key_pressed(sfml.Keyboard.Q): event = networking.event_serialize.ClientEventChangeclass(constants.CLASS_QUOTE) self.networker.events.append((self.networker.sequence, event)) # did we just release the F11 button? if yes, go fullscreen #if sfml.Keyboard.is_key_pressed(sfml.Keyboard.F11): # self.window.fullscreen = not self.window.fullscreen # update the game and render frame_time = self.clock.tick() frame_time = min(0.25, frame_time) # a limit of 0.25 seconds to prevent complete breakdown self.fpscounter_accumulator += frame_time self.networker.recieve(self.game, self) self.game.update(self.networker, frame_time) self.renderer.render(self, self.game, frame_time) if self.network_update_timer >= constants.INPUT_SEND_FPS: self.networker.update(self) self.network_update_timer = 0 else: self.network_update_timer += frame_time if self.fpscounter_accumulator > 1.0: self.window.title = "PyGG2 - %d FPS" % (self.fpscounter_frames / self.fpscounter_accumulator) self.fpscounter_accumulator = 0.0 self.fpscounter_frames = 0 self.window.display() self.fpscounter_frames += 1 else: frame_time = self.clock.tick() frame_time = min(0.25, frame_time) self.timeout_accumulator += frame_time if not self.window.open: self.window.close() return (False) # We still need to poll the window to keep it responding for event in self.window.iter_events(): if event.type == sfml.Event.CLOSED: #Press the 'x' button return (False) elif event.type == sfml.Event.KEY_PRESSED: #Key handler if event.code == sfml.Keyboard.ESCAPE: return (False) self.window.title = "PyGG2 - Not Connected %dsecs" % (self.timeout_accumulator) #Finally, if the server is not reachable, end everything. if self.timeout_accumulator > constants.CONNECTION_TIMEOUT: print("Unable to connect to " + str(self.server_ip) + " at port: " + str(self.server_port)) return (False) #exit time.sleep(max(frame_time, 0.25)) # Slow down the execution rate self.cleanup()