-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprites.py
57 lines (43 loc) · 1.54 KB
/
Sprites.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
import pygame
from settings import *
vec = pygame.math.Vector2
class Player(pygame.sprite.Sprite):
def __init__(self, game):
pygame.sprite.Sprite.__init__(self)
self.game = game
self.image = standing
self.rect = self.image.get_rect()
self.rect.center = (SCREEN_WIDTH/2 , SCREEN_HEIGHT/2)
self.pos = vec(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
def jump(self):
self.rect.y += 1
hits = pygame.sprite.spritecollide(self, self.game.platforms, False)
self.rect.y -= 1
if hits:
self.vel.y = -12
def update(self):
self.acc = vec(0, 0.5)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.image = l_standing
self.acc.x = -PLAYER_ACC
if keys[pygame.K_RIGHT]:
self.image = standing
self.acc.x = PLAYER_ACC
self.acc.x += self.vel.x * PLAYER_FRI
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
if self.pos.x > SCREEN_WIDTH:
self.pos.x = 0
if self.pos.x < 0:
self.pos.x = SCREEN_WIDTH
self.rect.midbottom = self.pos
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y ,w, h):
pygame.sprite.Sprite.__init__(self)
self.image = new_platform
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y