-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.py
192 lines (158 loc) · 6.52 KB
/
card.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
###################################################################################
###################################################################################
# Sprite Movement Towards Target Example
# programed/documented by Mad Cloud Games
# contact Mad Cloud Games @ [email protected] with any comments, questions, or changes
#
# This project is released under the GNU General Public License V3
# This code is open source
# We would love to know what it gets used for
###################################################################################
###################################################################################
import pygame, math
import config
from pygame.locals import *
pygame.init()
class Vector():
'''
Class:
creates operations to handle vectors such
as direction, position, and speed
'''
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self): # used for printing vectors
return "(%s, %s)"%(self.x, self.y)
def __getitem__(self, key):
if key == 0:
return self.x
elif key == 1:
return self.y
else:
raise IndexError("This "+str(key)+" key is not a vector key!")
def __sub__(self, o): # subtraction
return Vector(self.x - o.x, self.y - o.y)
def length(self): # get length (used for normalize)
return math.sqrt((self.x**2 + self.y**2))
def normalize(self): # divides a vector by its length
l = self.length()
if l != 0:
return (self.x / l, self.y / l)
return None
class Card(pygame.sprite.Sprite):
def __init__(self,image,rank,suit):
'''
Class:
creates a Card sprite
Parameters:
- self, image, rank, suite
'''
super(Card,self).__init__()
self.surf = pygame.transform.smoothscale(
pygame.image.load(image).convert_alpha(),
(config.CARD_WIDTH, config.CARD_HEIGHT)
) # load image
self.rect = self.surf.get_rect()
self.trueX = -100 # created because self.rect.center does not hold
self.trueY = -100 # decimal values but these do
self.rect.center = (self.trueX, self.trueY) # set starting position
self.speed = config.CARD_ANIMATION_SPEED # movement speed of the sprite
self.speedX = 0 # speed in x direction
self.speedY = 0 # speed in y direction
self.hasReachedTarget = False
self.target = None # starts off with no target
self.rank = rank
self.suit = suit
def get_direction(self, target):
'''
Function:
takes total distance from sprite.center
to the sprites target
(gets direction to move)
Returns:
a normalized vector
Parameters:
- self
- target
x,y coordinates of the sprites target
can be any x,y coorinate pair in
brackets [x,y]
or parentheses (x,y)
'''
if self.target: # if the square has a target
position = Vector(self.rect.centerx, self.rect.centery) # create a vector from center x,y value
target = Vector(target[0], target[1]) # and one from the target x,y
self.dist = target - position # get total distance between target and position
direction = self.dist.normalize() # normalize so its constant in all directions
return direction
def distance_check(self, dist):
'''
Function:
tests if the total distance from the
sprite to the target is smaller than the
ammount of distance that would be normal
for the sprite to travel
(this lets the sprite know if it needs
to slow down. we want it to slow
down before it gets to it's target)
Returns:
bool
Parameters:
- self
- dist
this is the total distance from the
sprite to the target
can be any x,y value pair in
brackets [x,y]
or parentheses (x,y)
'''
dist_x = dist[0] ** 2 # gets absolute value of the x distance
dist_y = dist[1] ** 2 # gets absolute value of the y distance
t_dist = dist_x + dist_y # gets total absolute value distance
speed = self.speed ** 2 # gets aboslute value of the speed
if t_dist < (speed): # read function description above
return True
def update(self):
'''
Function:
gets direction to move then applies
the distance to the sprite.center
()
Parameters:
- self
'''
self.dir = self.get_direction(self.target) # get direction
if self.dir: # if there is a direction to move
if self.distance_check(self.dist): # if we need to stop
self.rect.center = self.target # center the sprite on the target
else: # if we need to move normal
self.trueX += (self.dir[0] * self.speed) # calculate speed from direction to move and speed constant
self.trueY += (self.dir[1] * self.speed)
self.rect.center = (round(self.trueX),round(self.trueY)) # apply values to sprite.center
else:
self.hasReachedTarget = True
def isStationary(self):
return self.get_direction(self.target) == None
def main():
screen = pygame.display.set_mode((1100,600))
pygame.display.set_caption("Sprite Movement Towards Target Example - Mad Cloud Games")
background_color = pygame.Surface(screen.get_size()).convert()
background_color.fill((240,50,0))
sprite = Card(image='assets/cards/2_of_hearts.png',rank=2, suit='HEARTS') # create the sprite
clock = pygame.time.Clock()
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == MOUSEBUTTONDOWN:
sprite.target = event.pos # set the sprite.target to the mouse click position
screen.blit(background_color, (0,0))
sprite.update() # update the sprite
screen.blit(sprite.surf, sprite.rect.topleft) # blit the sprite to the screen
pygame.display.flip()
pygame.quit() # for a smooth quit
if __name__ == "__main__":
main()