Ok, mam problem z kodem wiem że jest to totalne spaghetti i ciężko będzie się doczytać.
Gra to shooter można w niej strzelać do zombie mam plany na rozbudowę gry w pełny projekt ale mniejsza
To cały kod:
import pygame as pg, math as m, random as r
pg.init()
w = pg.display.set_mode((0, 0), pg.FULLSCREEN)
class BG:
def __init__(self, x, y):
self.image = pg.image.load("assets/trawa.png")
self.x, self.y = x, y
def draw(self):
w.blit(self.image, (self.x, self.y))
drawings = []
x = 0
y = 0
for y in range(-1080 * 2, 1080 * 2, 64):
for x in range(-1940 * 2, 1940 * 2, 64):
drawings.append(BG(x, y))
class Bullet:
def __init__(self, x, y):
self.mouse = pg.mouse.get_pos()
self.bx = x
self.by = y
self.spd = 30
self.vector = m.atan2(x - self.mouse[0], y - self.mouse[1])
self.bx_vel = m.sin(self.vector) * self.spd
self.by_vel = m.cos(self.vector) * self.spd
self.bwidth = 8
self.bheight = 8
self.bhitbox = pg.Rect(self.bx, self.by, self.bwidth, self.bheight)
def draw(self):
pg.draw.rect(w, (0, 0, 0), self.bhitbox)
def update(self):
self.bhitbox = pg.Rect(self.bx, self.by, self.bwidth, self.bheight)
self.bx -= int(self.bx_vel)
self.by -= int(self.by_vel)
class Pistol(Bullet):
def __init__(self, x, y):
self.Pistol_x = x + 100
self.Pistol_y = y
self.Pistol_image = pg.image.load("assets/pistolet 1.png")
super().__init__(self.Pistol_x, self.Pistol_y)
class Player(Pistol):
def __init__(self):
self.x = 900
self.y = 500
self.width = 32 * 1.5
self.height = 32 * 1.5
self.acc = 1
self.max_speed = 15
self.y_vel = 0
self.x_vel = 0
self.image = pg.image.load("assets/gracz.png")
self.hurtbox = pg.Rect(self.x ,self.y, self.width, self.height)
self.alive = True
super().__init__(self.x, self.y)
def draw(self):
if self.alive:
w.blit(self.image, (self.x, self.y))
w.blit(self.Pistol_image, (self.Pistol_x, self.Pistol_y))
def movment(self, keys):
if self.alive:
if keys[pg.K_a] and self.x_vel > self.max_speed * -1:
self.x_vel -= self.acc
if keys[pg.K_d] and self.x_vel < self.max_speed:
self.x_vel += self.acc
if keys[pg.K_w] and self.y_vel > self.max_speed * -1:
self.y_vel -= self.acc
if keys[pg.K_s] and self.y_vel < self.max_speed:
self.y_vel += self.acc
if not(keys[pg.K_d] or keys[pg.K_a]):
if self.x_vel > 0:
self.x_vel -= self.acc
if self.x_vel < 0:
self.x_vel += self.acc
if not(keys[pg.K_w] or keys[pg.K_s]):
if self.y_vel > 0:
self.y_vel -= self.acc
if self.y_vel < 0:
self.y_vel += self.acc
def update(self):
if self.alive:
self.hurtbox = pg.Rect(self.x ,self.y, self.width, self.height)
else:
self.x_vel = 0
self.y_vel = 0
class Zombie:
def __init__(self):
self.x = 0
self.y = 0
self.width = 48
self.height = 48
self.stop = False
self.image = pg.image.load("assets/1 zombie.png")
self.spd = r.randint(8, 16)
self.vector = m.atan2(0, 0)
self.x_vel = m.sin(self.vector) * self.spd
self.y_vel = m.cos(self.vector) * self.spd
self.hitbox = pg.Rect(self.x, self.y + 48, self.width * 3, self.height * 3)
self.hurtbox = pg.Rect(self.x, self.y, self.width, self.height)
self.health = 3
def draw(self):
if self.health > 0:
w.blit(self.image, (self.x, self.y))
def update(self, px, py):
if self.health > 0:
if not(self.stop):
self.spd = 10
self.vector = m.atan2(self.x - px, self.y - py)
self.x_vel = m.sin(self.vector) * self.spd
self.y_vel = m.cos(self.vector) * self.spd
self.x -= int(self.x_vel)
self.y -= int(self.y_vel)
self.hitbox = pg.Rect(self.x - 24 ,self.y - 24, self.width * 2, self.height * 2)
self.hurtbox = pg.Rect(self.x, self.y, self.width, self.height)
else:
if self.spd > 0:
self.spd -= 1.3
self.x_vel = m.sin(self.vector) * self.spd
self.y_vel = m.cos(self.vector) * self.spd
self.x -= int(self.x_vel)
self.y -= int(self.y_vel)
else:
self.x = 99999999999999
self.y = 99999999999999
self.hitbox = pg.Rect(self.x - 24 ,self.y - 24, self.width * 2, self.height * 2)
self.hurtbox = pg.Rect(self.x, self.y, self.width, self.height)
class ZombieSpawner(Zombie):
def __init__(self):
self.x = r.randint(0, 1940)
self.y = r.randint(0, 1080)
self.delay = 1
self.zombies = []
super().__init__()
def spawn(self):
self.zombies.append(Zombie())
def main():
run = True
player = Player()
zombiespawner = ZombieSpawner()
bullets = []
clock = 1
clock2 = 1
zombies = []
while run:
tick = pg.time.Clock().tick(120) / 1000
clock2 += tick
clock += tick
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
keys = pg.key.get_pressed()
mouse = pg.mouse.get_pressed()
player.movment(keys)
w.fill((0, 0, 0))
if mouse[0] and clock > 0.3 and player.alive:
bullets.append(Bullet(player.Pistol_x, player.Pistol_y))
clock = 0
if clock2 >= zombiespawner.delay:
zombies.append(Zombie())
clock2 = 0
player.update()
for drawing in drawings:
drawing.draw()
drawing.x += player.x_vel * -1
drawing.y += player.y_vel * -1
player.draw()
for bullet in bullets[:]:
bullet.draw()
bullet.update()
bullet.bx += player.x_vel * -1
bullet.by += player.y_vel * -1
for zombie in zombies:
if bullet.bhitbox.colliderect(zombie.hurtbox):
bullets.remove(bullet)
zombie.health -= 1
for zombie in zombies:
zombie.draw()
zombie.x -= player.x_vel
zombie.y -= player.y_vel
zombie.update(player.x, player.y)
if zombie.hitbox.colliderect(player.hurtbox):
zombie.stop = True
player.alive = False
else:
zombie.stop = False
pg.display.flip()
if __name__ == "__main__":
main()
a to fragment który powoduję problemy i błędy:
for bullet in bullets[:]:
bullet.draw()
bullet.update()
bullet.bx += player.x_vel * -1
bullet.by += player.y_vel * -1
for zombie in zombies:
if bullet.bhitbox.colliderect(zombie.hurtbox):
bullets.remove(bullet)
zombie.health -= 1
problem z tym że gdy poruszam się postacią w osi y i strzelam w zombie czasami wyskakuje błąd "ValueError: list.remove(x): x not in list"