-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhot_dog.py
87 lines (75 loc) · 2.7 KB
/
hot_dog.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
import pygame
from pygame.sprite import Sprite
import random
class Hotdog(pygame.sprite.Sprite):
"""A class to represent a single hot dog in the fleet."""
def __init__(self, bs_settings, screen):
"""Initialize the hot dog and set its starting position."""
pygame.sprite.Sprite.__init__(self)
self.screen = screen
self.bs_settings = bs_settings
# Load the hot dog image and set its rect attribute.
self.image = pygame.image.load('images/hot_dog.bmp')
self.rect = self.image.get_rect()
# Start each new hot dog near the top left of the screen.
self.rect.x = self.rect.width
self.rect.y = self.rect.height
# Store the hot dog's exact position.
self.x = float(self.rect.x)
self.y = float(self.rect.y)
# Store and randomize the hot dog's direction.
self.hot_dog_direction_factor = 1
numbers = [x for x in range(-4, 4) if x != 0]
self.direction = random.choice(numbers)
print(self.direction)
def blitme(self):
"""Draw the hot dog at its current location."""
self.screen.blit(self.image, self.rect)
def check_edges(self):
"""Return True if hot dog is at edge of screen."""
screen_rect = self.screen.get_rect()
if self.rect.right >= screen_rect.right:
return True
elif self.rect.left <= 0:
return True
elif self.rect.top <= 0:
return True
elif self.rect.bottom >= screen_rect.bottom:
return True
def update(self):
"""Move the hot dog."""
self.speed = self.bs_settings.hot_dog_speed_factor
self.direction_factor = self.hot_dog_direction_factor
if self.direction == -4:
self.y += (1 * self.speed * self.direction_factor)
elif self.direction == -3:
self.y -= (1 * self.speed * self.direction_factor)
elif self.direction == -2:
self.x += (1 * self.speed * self.direction_factor)
elif self.direction == -1:
self.x -= (1 * self.speed * self.direction_factor)
elif self.direction == 1:
self.y += (1 * self.speed * self.direction_factor)
self.x += (1 * self.speed * self.direction_factor)
elif self.direction == 2:
self.y -= (1 * self.speed * self.direction_factor)
self.x -= (1 * self.speed * self.direction_factor)
elif self.direction == 3:
self.x += (1 * self.speed * self.direction_factor)
self.y -= (1 * self.speed * self.direction_factor)
elif self.direction == 4:
self.x -= (1 * self.speed * self.direction_factor)
self.y += (1 * self.speed * self.direction_factor)
self.rect.x = self.x
self.rect.y = self.y
def kill_offscreen(self):
"""Kill hot dogs that wander off screen."""
screen_rect = self.screen.get_rect()
if self.rect.left >= screen_rect.right:
return True
elif self.rect.right <= 0:
return True
elif self.rect.bottom <= 0:
return True
elif self.rect.top >= screen_rect.bottom:
return True