Last active
October 25, 2016 02:35
-
-
Save Mekire/16f189237ac0d67dc4d76f856503647b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame as pg | |
from math import degrees, atan2, pi | |
def get_angle(origin, destination): | |
"""Returns angle in radians from origin to destination. | |
This is the angle that you would get if the points were | |
on a cartesian grid. Arguments of (0,0), (1, -1) | |
return pi/4 (45 deg) rather than 7/4. | |
""" | |
x_dist = destination[0] - origin[0] | |
y_dist = destination[1] - origin[1] | |
return atan2(-y_dist, x_dist) % (2 * pi) | |
class Player(pg.sprite.Sprite): | |
def __init__(self, center_pos): | |
super(Player, self).__init__() | |
self.original_image = pg.image.load("data/resources/images/playerImage.png").convert_alpha() | |
self.facing_angle = 90 | |
self.start_angle = 90 | |
self.pos = list(center_pos) | |
self.rect = self.original_image.get_rect(center=self.pos) | |
self.velocity = [0, 0] | |
self.set_image() | |
self.vx = 0 | |
self.vy = 0 | |
self.keys_dict = { | |
pg.K_w: (0, -1), | |
pg.K_s: (0, 1), | |
pg.K_a: (-1, 0), | |
pg.K_d: (1, 0)} | |
self.speed = .2 | |
def set_image(self): | |
angle = self.facing_angle - self.start_angle | |
self.image = pg.transform.rotozoom(self.original_image, angle, 1) | |
self.image_rect = self.image.get_rect() | |
def update(self, mouse_pos, pressed, dt, obstacles): | |
self.facing_angle = degrees(get_angle(self.pos, mouse_pos)) | |
self.set_image() | |
move = [0, 0] | |
for k in self.keys_dict: | |
if pressed[k]: | |
x, y = self.keys_dict[k] | |
self.vx = x * self.speed * dt | |
self.vy = y * self.speed * dt | |
move = [move[0] + self.vx, | |
move[1] + self.vy] | |
self.movement(move, obstacles, 0) | |
self.movement(move, obstacles, 1) | |
self.image_rect.center = self.pos | |
def movement(self, move, obstacles, i): | |
""" | |
Move player and then check for collisions; adjust as necessary. | |
""" | |
self.pos[i] += move[i] | |
self.rect.center = self.pos | |
collision = pg.sprite.spritecollideany(self, obstacles) | |
while collision: | |
self.adjust_on_collision(collision, i) | |
collision = pg.sprite.spritecollideany(self, obstacles) | |
def adjust_on_collision(self, collide, i): | |
""" | |
Adjust player's position if colliding with a solid block. | |
""" | |
bounce = 10 | |
if self.rect[i] < collide.rect[i]: | |
self.rect[i] -= bounce | |
else: | |
self.rect[i] += bounce | |
self.pos[i] = self.rect.center[i] | |
def draw(self, surface): | |
surface.blit(self.image, self.image_rect) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment