forked from stamaniorec/icy_tower_clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.py
134 lines (112 loc) · 4.16 KB
/
Player.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import pygame
pygame.init()
from Constants import GRAVITY, SCREEN_WIDTH, SCREEN_HEIGHT
from Utils import load_image
from copy import deepcopy
class Player:
width = 30
height = 50
vel_x = 0
vel_y = 0
max_falling_speed = 20
acceleration = 0.5
max_vel_x = 7
color = (255, 0, 0)
speed = 5
def __init__(self):
self.x = 30
self.y = 500
self.score = -10 # negate floor platform
self.spritesheet_image = load_image('spritesheet.png')
self.spritesheet = []
# Idle
self.cropped = pygame.Surface((33, 57), pygame.SRCALPHA, 32)
self.cropped.blit(self.spritesheet_image, (0, 0), (0, 0, 33, 57))
self.cropped2 = pygame.Surface((33, 57), pygame.SRCALPHA, 32)
self.cropped2.blit(self.spritesheet_image, (0, 0), (37, 0, 33, 57))
self.cropped3 = pygame.Surface((33, 57), pygame.SRCALPHA, 32)
self.cropped3.blit(self.spritesheet_image, (0, 0), (75, 0, 33, 57))
self.spritesheet.append(self.cropped)
self.spritesheet.append(self.cropped2)
self.spritesheet.append(self.cropped3)
# Going right
self.cropped4 = pygame.Surface((33, 57), pygame.SRCALPHA, 32)
self.cropped4.blit(self.spritesheet_image, (0, 0), (0, 56, 33, 57))
self.cropped5 = pygame.Surface((33, 57), pygame.SRCALPHA, 32)
self.cropped5.blit(self.spritesheet_image, (0, 0), (37, 56, 33, 57))
self.cropped6 = pygame.Surface((33, 57), pygame.SRCALPHA, 32)
self.cropped6.blit(self.spritesheet_image, (0, 0), (75, 56, 33, 57))
self.spritesheet.append(self.cropped4)
self.spritesheet.append(self.cropped5)
self.spritesheet.append(self.cropped6)
# Going left
self.spritesheet.append(pygame.transform.flip(self.cropped4, True, False))
self.spritesheet.append(pygame.transform.flip(self.cropped5, True, False))
self.spritesheet.append(pygame.transform.flip(self.cropped6, True, False))
# Jumping
self.cropped7 = pygame.Surface((33, 57), pygame.SRCALPHA, 32)
self.cropped7.blit(self.spritesheet_image, (0, 0), (75, 112, 33, 57))
self.spritesheet.append(self.cropped7)
self.spritesheet.append(self.cropped7)
self.spritesheet.append(self.cropped7)
self.sprite_index_x = 0
self.sprite_index_y = 0
self.frame_counter = 0
self.frame_delay = 9
def draw(self, game_display, camera):
game_display.blit(self.spritesheet[self.sprite_index_y*3 + self.sprite_index_x], (self.x, self.y-camera.y))
self.frame_counter += 1
if self.frame_counter >= self.frame_delay:
self.sprite_index_x += 1
if self.sprite_index_x > 2:
self.sprite_index_x = 0
self.frame_counter = 0
def update(self):
self.x += self.vel_x
self.y += self.vel_y
self.vel_y += GRAVITY
if self.vel_y > self.max_falling_speed:
self.vel_y = self.max_falling_speed
if self.x <= 0:
self.x = 0
if self.x + self.width >= SCREEN_WIDTH:
self.x = SCREEN_WIDTH - self.width
def combo(self):
if self.x == 0:
if self.vel_y < 0:
if self.vel_x < 0:
self.vel_y -= 10
self.vel_x *= -2.5
if self.x + self.width >= SCREEN_WIDTH:
if self.vel_y < 0:
if self.vel_x > 0:
self.vel_y -= 10
self.vel_x *= -2.5
def on_platform(self, platform):
# return platform.rect.top <= self.y + self.height
return platform.rect.collidepoint((self.x, self.y + self.height)) or \
platform.rect.collidepoint((self.x+self.width, self.y + self.height))
def on_any_platform(self, platform_controller, floor):
for p in platform_controller.platform_set:
if self.on_platform(p):
return True
if self.on_platform(floor):
return True
return False
def collide_platform(self, platform, index):
for i in range(0,self.vel_y):
if pygame.Rect(self.x, self.y-i, self.width, self.height).colliderect(platform.rect):
if platform.rect.collidepoint((self.x, self.y + self.height-i)) or \
platform.rect.collidepoint((self.x+self.width, self.y + self.height-i)): #do not change! no on_platform here
self.y = platform.y - self.height
if not platform.collected_score:
self.score += 10
if self.score < index * 10:
self.score = index * 10
platform.collected_score = True
def get_rect(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
def fallen_off_screen(self, camera):
if self.y - camera.y + self.height >= SCREEN_HEIGHT:
return True
return False