-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.py
132 lines (99 loc) · 3.53 KB
/
sprite.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
from setup import *
import pygame as pg
vec = pg.math.Vector2
class Player1(pg.sprite.Sprite):
def __init__(self, game):
self.game = game
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((30,40))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
self.pos = vec(WIDTH / 2,HEIGHT/2)
self.vel = vec(0,0)
self.acc = vec(0,0)
self.extra_jump = False
def jump(self, player):
#jump only if standing on a platform
self.rect.y += 1
hits = pg.sprite.spritecollide(self, self.game.platforms,False) or pg.sprite.collide_rect(self, player)
self.rect.y -= 1
if hits:
self.vel.y = -PLAYER_JUMP
self.extra_jump = True
elif self.extra_jump:
self.vel.y = -PLAYER_JUMP
self.extra_jump = False
def update(self):
self.acc = vec(0,PLAYER_GRAV)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
self.acc.x = -PLAYER_ACC
if keys[pg.K_RIGHT]:
self.acc.x = PLAYER_ACC
if keys[pg.K_DOWN]:
self.vel.y = 20
#apply friction
self.acc.x += self.vel.x * PLAYER_FRICTION
#equation of motion
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
if self.pos.x > WIDTH:
self.pos.x = 0
if self.pos.x < 0:
self.pos.x = WIDTH
self.rect.midbottom = self.pos
def kill(self):
self.kill
class Player2(pg.sprite.Sprite):
def __init__(self, game):
self.game = game
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((30,40))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 4, HEIGHT / 2)
self.pos = vec(WIDTH / 4,HEIGHT/2)
self.vel = vec(0,0)
self.acc = vec(0,0)
self.extra_jump = False
def jump(self, player):
#jump only if standing on a platform
self.rect.y += 1
hits = pg.sprite.spritecollide(self, self.game.platforms,False) or pg.sprite.collide_rect(self, player)
self.rect.y -= 1
if hits:
self.vel.y = -PLAYER_JUMP
self.extra_jump = True
elif self.extra_jump:
self.vel.y = -PLAYER_JUMP
self.extra_jump = False
def update(self):
self.acc = vec(0,PLAYER_GRAV)
keys = pg.key.get_pressed()
if keys[pg.K_a]:
self.acc.x = -PLAYER_ACC
if keys[pg.K_d]:
self.acc.x = PLAYER_ACC
if keys[pg.K_s]:
self.vel.y = 20
#apply friction
self.acc.x += self.vel.x * PLAYER_FRICTION
#equation of motion
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
if self.pos.x > WIDTH:
self.pos.x = 0
if self.pos.x < 0:
self.pos.x = WIDTH
self.rect.midbottom = self.pos
def kill(self):
self.kill()
class Platform(pg.sprite.Sprite):
def __init__(self, x, y, w, h):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((w,h))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y