summaryrefslogtreecommitdiffstats
path: root/Geometry-Dash/player.py
blob: 7e10f768f9979a7f42a9a4ba46f3921b9aabeb25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import pygame.time

from colors import COLOR_BLACK
from setup import *
from particle import Particles


class Player:
    width, height = 25, 25
    color = COLOR_BLACK
    jump_power = 0.75
    gravity = 0.0015
    movement_acceleration = 0.0025
    horizontal_resistance = 0.005
    dead = False

    def __init__(self, x, y):
        self.position = pygame.Vector2(x, y)
        self.velocity = pygame.Vector2(0, 0)
        self.acceleration = pygame.Vector2(0, self.gravity)
        self.square = pygame.image.load('textures/skins/skull2.png')
        self.square = pygame.transform.scale(self.square, (self.width, self.height))
        self.angle = 0

    def jump(self):
        if self.velocity.y == 0:
            if not self.dead:
                self.velocity.y = -self.jump_power

    def resolve_platform_collisions(self, delta_time, platforms):
        epsilon = abs(self.velocity.y) * delta_time

        player_left = self.position.x
        player_right = self.position.x + self.width

        for platform in platforms:
            platform_left = platform.position.x
            platform_right = platform.position.x + platform.width
            if player_left < platform_right and player_right > platform_left:
                if abs((self.position.y + self.height) - platform.position.y) < epsilon:
                    self.position.y = platform.position.y - self.height
                    self.velocity.y = 0
                if abs(self.position.y - (platform.position.y + platform.height)) < epsilon:
                    self.dead = True

    def resolve_wall_collisions(self, delta_time, walls):
        epsilon = abs(self.velocity.y) * delta_time

        player_top = self.position.y
        player_bottom = self.position.y + self.height

        for wall in walls:
            wall_top = wall.position.y
            wall_bottom = wall.position.y + wall.height
            if player_top < wall_bottom and player_bottom > wall_top:
                if abs((self.position.x + self.width) - wall.position.x) < epsilon:
                    self.dead = True

    def update(self, delta_time):
        if not self.dead:
            self.position += self.velocity * delta_time
            self.velocity += self.acceleration * delta_time

    def draw(self, screen, camera_y):
        if not self.dead:
            self.top_left = (SCREEN_WIDTH / 4 - self.width / 2, self.position.y + camera_y)
            if self.velocity.y != 0 and not self.velocity.y > 0.4:
                self.angle -= 1
                self.square_copy = pygame.transform.rotate(self.square, self.angle)
                screen.blit(self.square_copy, self.top_left)
            else:
                self.angle = 0
                screen.blit(self.square, self.top_left)

        # Old way using rectangles
        # self.r = pygame.Rect(SCREEN_WIDTH/4-self.width/2, self.position.y , self.width, self.height)
        # pygame.draw.rect(screen, self.color, self.r)