-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.py
204 lines (189 loc) · 6.71 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import pygame as pg
from random import uniform, choice, randint
from setting import *
vec = pg.math.Vector2
c = 0
a = 0
b = 0
class Player_1(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.playerS_img #플레이어 이미지 설정
self.rect = self.image.get_rect()
self.vx, self.vy = 0, 0
self.vel = vec(0, 0)
self.pos = vec(x, y)
def get_keys(self):
global a, b, c
self.vel = vec(0, 0)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
a = 1
self.vel.x = -PLAYER_SPEED
if c == 0 :
self.image = self.game.playerW8_img
elif c == 1 :
self.image = self.game.playerW9_img
elif c == 2 :
self.image = self.game.playerW10_img
c = -1
c+=0.25
if keys[pg.K_RIGHT]:
a = 2
self.vel.x = PLAYER_SPEED
if c == 0 :
self.image = self.game.playerW1_img
elif c == 1 :
self.image = self.game.playerW2_img
elif c == 2 :
self.image = self.game.playerW3_img
c = -1
c+=0.25
if keys[pg.K_UP]:
a=3
self.vel.y = -PLAYER_SPEED
if c == 0 :
self.image = self.game.playerB_img
elif c == 1 :
self.image = self.game.playerW4_img
elif c == 2 :
self.image = self.game.playerW5_img
c = -1
c+=0.25
if keys[pg.K_DOWN]:
a=4
self.vel.y = PLAYER_SPEED
if c == 0 :
self.image = self.game.playerS_img
elif c == 1 :
self.image = self.game.playerW6_img
elif c == 2 :
self.image = self.game.playerW7_img
c = -1
c+=0.25
if keys[pg.K_SPACE] :
if a == 1 : #왼
self.vel.x = 0
if b == 0:
self.image = self.game.playerside1catch1_img
elif b == 1 :
self.image = self.game.playerside1catch2_img
elif b == 2 :
self.image = self.game.playerside1catch3_img
b = -1
b += 1
if a == 2 : #오
self.vel.x = 0
if b == 0 :
self.image = self.game.playerside2catch1_img
elif b == 1 :
self.image = self.game.playerside2catch2_img
elif b == 2 :
self.image = self.game.playerside2catch3_img
b = -1
b += 1
if a == 3 :
self.vel.y = 0
if b == 0 :
self.image = self.game.playerBcatch1_img
elif b == 1 :
self.image = self.game.playerBcatch2_img
elif b == 2 :
self.image = self.game.playerBcatch3_img
b = -1
b += 1
if a == 4 : #밑
self.vel.y = 0
if b == 0 :
self.image = self.game.playerstaycatch1_img
elif b == 1 :
self.image = self.game.playerstaycatch2_img
elif b == 2 :
self.image = self.game.playerstaycatch3_img
b = -1
b += 1
if self.vel.x != 0 and self.vel.y != 0:
self.vel *= 0.7071
def collide_with_walls(self, dir): #우리가 원할 때만 벽통과
if dir == 'x':
hits = pg.sprite.spritecollide(self, self.game.walls, False)
if hits:
if self.vel.x > 0:
self.pos.x = hits[0].rect.left - self.rect.width
if self.vel.x < 0:
self.pos.x = hits[0].rect.right
self.vel.x = 0
self.rect.x = self.pos.x
if dir == 'y':
hits = pg.sprite.spritecollide(self, self.game.walls, False)
if hits:
if self.vel.y > 0:
self.pos.y = hits[0].rect.top - self.rect.height
if self.vel.y < 0:
self.pos.y = hits[0].rect.bottom
self.vel.y = 0
self.rect.y = self.pos.y
def update(self):
self.get_keys()
self.pos += self.vel * self.game.dt
self.rect.x = self.pos.x
self.collide_with_walls('x')
self.rect.y = self.pos.y
self.collide_with_walls('y')
class Wall(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.walls
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.sea_img
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x * TILESIZE
self.rect.y = y * TILESIZE
# class Raccoon(pg.sprite.Sprite):
# def __init__(self, game, x, y) :
# self.groups = game.all_sprites, game.raccoon_dog
# pg.sprite.Sprite.__init__(self, self.groups)
# self.game = game
# self.image = game.raccoon_img
# self.rect = self.image.get_rect()
# self.pos = vec(x, y)
# self.rect.center = self.pos
# def update(self) :
# # self.image = pg.transform.scale(self.game.raccoon_img, )
# self.rect.center = self.pos
class Tree(pg.sprite.Sprite) :
def __init__(self, game, x, y) :
self.groups = game.all_sprites, game.trees
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.tree_img
self.rect = self.image.get_rect()
self.pos = vec(x,y)
self.rect.center = self.pos
def update(self) :
self.rect.center = self.pos
class Appletree(pg.sprite.Sprite) :
def __init__(self, game, x, y) :
self.groups = game.all_sprites, game.appletrees
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.appletree_img
self.rect = self.image.get_rect()
self.pos = vec(x,y)
self.rect.center = self.pos
def update(self) :
self.rect.center = self.pos
class Obstacle(pg.sprite.Sprite):
def __init__(self, game, x, y, w, h):
self.groups = game.walls#, game.raccoon_dog, game.appletrees, game.trees
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.rect = pg.Rect(x, y, w, h)
self.x = x
self.y = y
self.rect.x = x
self.rect.y = y