I have set up the circles so that each one of the circles have independent properties such as x, y and speed by doing this
class Enemy:
def __init__(self):
self.x = 1300
self.y = random.randint(10, 200)
self.radius = random.randint(15, 30)
self.color = (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
self.speed = random.randint(1, 4)
In the below code i am changing the speed and number of circles so that the game gets progresively harder. (note: point1
is time.time()
function set at the start if the code and point2
at the end)
if not enemies:
if (point2 - point1) >= 10 and (point2 - point1) <= 20:
wave += 1
for i in range(random.randint(1, 5)):
enemies.append(Enemy())
e.draw()
e.move()
if (point2 - point1) >= 20 and (point2 - point1) <= 40:
wave += 1
for i in range(random.randint(8,10)):
enemies.append(Enemy())
e.draw()
e.move()
if (point2 - point1) >= 40 and (point2 - point1) <= 60:
wave += 1
for i in range(random.randint(12, 14)):
enemies.append(Enemy())
e.draw()
e.move()
if (point2 - point1) >= 60:
wave += 1
for i in range(random.randint(15, 20)):
wave += 1
enemies.append(Enemy())
e.draw()
e.move()
if point2 - point1 > 20:
Enemy().speed = random.randint(2, 6)
if point2 - point1 > 40:
Enemy().speed = random.randint(3, 6)
if point2 - point1 > 60:
Enemy().speed = random.randint(5, 6)
if point2 - point1 > 40:
Enemy().speed = 7
I think the setup is random enough although when i try to run the code, all the circles with the same speed have the same x-coordinates with different y coordinates. This gets especially obvious as more and more circles starts to spawn. What might be causing this probelm ? Thanks
Heres my full code if anyone would like to check for reference:
import pygame, os, random, math, time
def main():
point1 = time.time()
win = pygame.display
d = win.set_mode((1200, 600))
win.set_caption("SHOOT")
def write(x, y, size, writing, color):
font = pygame.font.SysFont("rage", size)
text = font.render(writing, True, color)
d.blit(text, (x, y))
def collision_circle(c1, c2):
delta_x = abs(c2.x - c1.x)**2
delta_y = abs(c2.y - c1.y)**2
distance = math.sqrt(delta_x + delta_y)
if distance < c1.radius + c2.radius:
return True
else:
return False
class Player:
def __init__(self, x, y, h, w):
self.x = x
self.y = y
self.h = h
self.w = w
self.speed = 10
self.hp = 10
self.score = 0
def draw(self):
pygame.draw.rect(d, (0, 0, 0), (self.x, self.y, self.w, self.h))
def move_right(self):
self.x += self.speed
def move_left(self):
self.x -= self.speed
def boundry(self):
if self.x <= 0:
self.x = 0
if self.x + self.w >= 1200:
self.x = 1200 - self.w
class Enemy:
def __init__(self):
self.x = 1300
self.y = random.randint(10, 200)
self.radius = random.randint(15, 30)
self.color = (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
self.speed = random.randint(1, 4)
def draw(self):
pygame.draw.circle(d, self.color , (self.x, self.y), self.radius)
def move(self):
self.x -= self.speed
enemies = []
enemies.append(Enemy())
p = Player(600, 570, 30, 10)
class Bullet:
def __init__(self, x, y):
self.x = x
self.y = y
self.radius = 10
self.speed = 20
self.color = (255, 0, 0)
def update(self):
self.y -= self.speed
def draw(self):
pygame.draw.circle(d, self.color, (self.x, self.y), self.radius)
bullets = []
wave = 1
#bullet_append = True
while True:
pygame.time.Clock().tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
#while bullet_append:
bullets.append(Bullet(p.x + p.w//2, p.y - 3))
#bullet_append = False
d.fill((98, 98, 98))
p.draw()
p.boundry()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
p.move_left()
if keys[pygame.K_RIGHT]:
p.move_right()
for b in bullets:
b.update()
if b.y <= 0:
bullets.remove(b)
for b in bullets:
b.draw()
for b in bullets:
if b.y <= 300:
bullet_append = False
else:
bullet_append = True
#for b in bullets:
# if b.y <= 300:
# bullet_append = True
# else:
# bullet_append = False
for e in enemies:
e.draw()
e.move()
if e.x <= -10:
p.hp -= 1
enemies.remove(e)
def collided():
collided_list = []
for b in bullets:
for e in enemies:
try:
if collision_circle(b, e):
bullets.remove(b)
enemies.remove(e)
p.score += 1
except:
pass
if p.hp <= 0:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
main()
p.hp = 0
d.fill((98, 98, 98))
#write(x, y, size, text, color)
write(10, 200, 200, "GAME OVER" , (225, 0, 0))
write(300, 400, 50, "press space to start a new game" , (225, 255, 255))
write(200, 10, 30, "SCORE: %s"% p.score , (225, 255, 255))
write(300, 100, 30, "WAVE: %s"% wave , (225, 255, 255))
write(300, 50, 30, "HP: %s"% p.hp , (225, 255, 255))
win.flip()
point2 = time.time()
if not enemies:
if (point2 - point1) >= 10 and (point2 - point1) <= 20:
wave += 1
for i in range(random.randint(1, 5)):
enemies.append(Enemy())
e.draw()
e.move()
if (point2 - point1) >= 20 and (point2 - point1) <= 40:
wave += 1
for i in range(random.randint(8,10)):
enemies.append(Enemy())
e.draw()
e.move()
if (point2 - point1) >= 40 and (point2 - point1) <= 60:
wave += 1
for i in range(random.randint(12, 14)):
enemies.append(Enemy())
e.draw()
e.move()
if (point2 - point1) >= 60:
wave += 1
for i in range(random.randint(15, 20)):
wave += 1
enemies.append(Enemy())
e.draw()
e.move()
if point2 - point1 > 20:
Enemy().speed = random.randint(2, 6)
if point2 - point1 > 40:
Enemy().speed = random.randint(3, 6)
if point2 - point1 > 60:
Enemy().speed = random.randint(5, 6)
if point2 - point1 > 40:
Enemy().speed = 7
#write(x, y, size, text, color)
write(10, 10, 30, "SCORE: %s"% p.score , (225, 255, 255))
write(1050, 10, 30, "WAVE: %s"% wave , (225, 255, 255))
write(10, 50, 30, "HP: %s"% p.hp , (225, 255, 255))
collided()
win.flip()
if __name__ == "__main__":
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
main()
Aucun commentaire:
Enregistrer un commentaire