def __init__(self, world, pos, image): """Initializes the entity. Arguments: world - the main game engine / handler. pos - the initial starting position of the entity. image - the image of the entity TODO: Create a system for efficiently creating entities """ self.world = world self.pos = vec2(pos[0], pos[1]) self.vel = vec2(0, 0) self.base_image = image #I use base image in case animation is added. self.image = self.base_image self.image_rect = self.base_image.get_rect() self.animations = {} self.selected_animation = None self.ani_timer = 0.0 self.ani_frame = 0 self.update() #Call update once to set the position of the rect.
def get_adjusted_coords(self, coord): """By passing in a tuple of coordinates or a vector2 instance into coord, we can draw it in relation to the main object.""" center_x = self.screen_size[0] / 2 center_y = self.screen_size[1] / 2 return vec2(*coord) + vec2(center_x, center_y) - self.ob.pos
def __init__(self, pos=(0, 0)): self.pos = vec2(*pos) self.vel = vec2() self.acceleration = 0.1 self.max_speed = 10 self.orientation = 0 self.health = 100 self.base_img = pygame.image.load( "res/player_ships/PlayerShip1.png").convert_alpha() self.max_reload = 0.25 self.reload_timer = 0
def __init__(self, id, position=(0, 0), orientation=0, health=100, delta=0): self.id = id self.pos = vec2(*position) self.vel = vec2() self.acceleration = 0.15 self.max_speed = 10 self.orientation = orientation self.health = health self.CLIENT_AGE = delta self.rotation_speed = 0.1 self.drag = 0.05 self.speed = 5 self.last_input = None
def update(self, form_input): # [w, a, s, d, SPACE] if form_input[1]: self.orientation += self.rotation_speed if form_input[3]: self.orientation -= self.rotation_speed if self.orientation > math.pi * 2: self.orientation -= math.pi * 2 if self.orientation < math.pi * -2: self.orientation += math.pi * 2 if form_input[0]: self.vel += vec2(math.cos(self.orientation), -math.sin(self.orientation)) * self.acceleration if form_input[2]: self.vel -= vec2(math.cos(self.orientation), -math.sin(self.orientation)) * (self.acceleration / 2) self.vel = self.vel * (1-self.drag) if self.vel.get_magnitude() > self.max_speed: self.vel = self.vel.get_normalised() * self.max_speed self.pos += self.vel
def create_other(cls, data): to_return = cls() to_return.pos = vec2(data[0], data[1]) to_return.orientation = data[2] to_return.health = data[3] return to_return
def main(): pygame.init() screen_size = width, height = (1280, 720) screen = pygame.display.set_mode(screen_size) pygame.display.set_caption("A Race Against Time") handler = GameHandler.GameHandler(screen_size) handler.current_state = "GAME" main_menu = Menu.Menu(width, height) boost_speed = 50.0 done = False while not done: pos = pygame.mouse.get_pos() mouse_pos = vec2(pos[0], pos[1]) for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: done = True if handler.current_state == "MENU": handler.update() handler.render(screen) option = main_menu.handle_mouse_input(mouse_pos, pygame.mouse.get_pressed()) if option is not None: if option == 0: """Start""" print "Start" elif option == 1: """Quit""" done = True main_menu.render(screen) pygame.display.update() if handler.current_state == "GAME": if pygame.key.get_pressed()[pygame.K_w]: boost_speed += 0.5 elif pygame.key.get_pressed()[pygame.K_s]: boost_speed -= 0.5 if pygame.key.get_pressed()[pygame.K_SPACE]: handler.update(boost_speed) else: handler.update() handler.render(screen) pygame.display.update() pygame.quit()
def __init__(self, position, velocity, orientation): self.position = vec2(*position) self.velocity = vec2(*velocity) self.orientation = orientation