-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.py
More file actions
206 lines (149 loc) · 4.11 KB
/
Copy pathobjects.py
File metadata and controls
206 lines (149 loc) · 4.11 KB
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
import time
import random
import threading
class Laser(pygame.sprite.Sprite):
"""
Timer class
"""
def __init__(self, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.rect = self.image.get_rect()
self.width = width
self.height = height
def update(self):
"""
Shoots the bullet
"""
self.rect.y -= 8
def collided_with(self, sprite):
"""
Checks collision of the laser with another sprite
"""
return self.rect.colliderect(sprite.rect)
class Car(pygame.sprite.Sprite):
"""
Car that drives around on the road
"""
points = 0
def __init__(self, width, height, xCord, yCord, carList, health, healthLostEveryHit):
super().__init__()
self.image = pygame.Surface([width, height])
self.image = pygame.image.load("CarSprite.png").convert_alpha()
self.image = pygame.transform.scale(self.image, [width, height])
self.rect = self.image.get_rect()
self.rect.x = 445
self.rect.y = 520
self.health = {'hp':health, 'hitSomething':0}
self.healthLostEveryHit = healthLostEveryHit
def moveRight(self, pixels):
self.rect.x += pixels
def moveLeft(self, pixels):
self.rect.x -= pixels
def moveUp(self, pixels):
self.rect.y -= pixels
def moveDown(self, pixels):
self.rect.y += pixels
def collided_with(self, sprite):
"""
Checks collision of the laser with another sprite
"""
return self.rect.colliderect(sprite.rect)
def loseHealth(self):
"""
Detracts health from the player car
"""
self.health['hp'] -= self.healthLostEveryHit
def getHealth(self):
return self.health['hp']
def getPoints(self):
return self.points
def gainPoint(self):
self.points += 1
class EnemyLaser(pygame.sprite.Group):
"""
Enemy laser for the enemy cars
"""
def __init__(self, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.rect = self.image.get_rect()
self.width = width
self.height = height
def update(self):
"""
Pretty muuch only updates at random times
/When the randomness occurs
"""
self.rect.y -= 8
def collided_with(self, sprite):
"""
Checks collision of the laser with another sprite
"""
return self.rect.colliderect(sprite.rect)
class EnemyCar(pygame.sprite.Sprite):
"""
Enemy car sprite
"""
def __init__(self, width, height, howOftenShoot):
super().__init__()
self.image = pygame.Surface([width, height])
self.image = pygame.image.load("EnemyCar.png").convert_alpha()
self.image = pygame.transform.scale(self.image, [width, height])
self.rect = self.image.get_rect()
self.howOftenShoot = howOftenShoot
self.width = width
self.height = height
def moveRight(self):
"""
Moves right a random amount of pixels
"""
randomAmount = random.randrange(0, 11)
self.rect.x += randomAmount
def moveLeft(self):
"""
Moves left a random amount of pixels
"""
randomAmount = random.randrange(0, 11)
self.rect.x -= randomAmount
def moveUp(self):
"""
Moves up a random amount of pixels
"""
randomAmount = random.randrange(0, 11)
self.rect.y -= randomAmount
def moveDown(self):
"""
Moves down a random amount of pixels
"""
randomAmount = random.randrange(0, 11)
self.rect.y += randomAmount
def newRect(self):
"""
Updates the rect of the car
"""
self.rect = self.image.get_rect()
def collided_with(self, sprite):
"""
Checks collision of the laser with another sprite
"""
return self.rect.colliderect(sprite.rect)
def spawnEnemyCar(allSpritesList, enemyCarList):
"""
Spawns a sprite every (insert number) of seconds
"""
randomAmountOfCars = random.randrange(0, 1)
#print('Random amount of cars:', randomAmountOfCars)
for y in range(2):
enemyCar = EnemyCar(120, 80, 1)
enemyCar.image = pygame.image.load("EnemyCar.png").convert_alpha()
enemyCar.image = pygame.transform.scale(enemyCar.image, [enemyCar.width, enemyCar.height])
randomX = random.randrange(200, 700)
randomY = random.randrange(20, 50)
#print('Random x current car:', randomX)
enemyCar.rect.x = randomX
enemyCar.rect.y = randomY
enemyCarList.update()
enemyCarList.add(enemyCar)
allSpritesList.add(enemyCar)