I've been trying to make a bullet hell (dankamu) game in Python, and when I tried to animate the movements of the character with the bullets, I encountered some issues. Depending on where I put my redrawGameWindow()
function, either the bullets fail to spawn or my game character cannot move at all. I think it might be because of the fact that my movement function is separate from the for loop I used for the bullets? If that's the case, I don't know how to amalgamate them together in order to run both simultaneously.
class mc(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.hitbox = (self.x, self.y, 30, 30)
def draw(self, win):
win.blit(char, (self.x, self.y))
self.hitbox = (self.x, self.y, 30, 30)
pygame.draw.rect(win, (255,0,0), self.hitbox,2)
def hit(self):
print("Hit")
def redrawGameWindow(mainchar, bullet, bullets):
win.blit(bg, (0,0))
mainchar.draw(win)
pygame.display.update()
for bullet in bullets:
bullet.draw(win)
class projectile(object):
def __init__(self,x,y,radius,color,facing):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.facing = facing
self.vel = 8 * facing
def draw(self,win):
pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)
def dankamu(enemy):
mainchar = mc(200, 410, 64, 64)
run = True
bullets = []
while run:
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and mainchar.x > 0:
mainchar.x -= mainchar.vel
if keys[pygame.K_RIGHT] and mainchar.x < 470:
mainchar.x += mainchar.vel
if keys[pygame.K_DOWN] and mainchar.y < 470:
mainchar.y += mainchar.vel
if keys[pygame.K_UP] and mainchar.y > 0:
mainchar.y -= mainchar.vel
if keys[pygame.K_LSHIFT] and mainchar.vel > 0:
mainchar.vel = 3
if keys[pygame.K_RSHIFT]:
mainchar.vel = 5
win.fill((255, 255, 255))
if enemy == "skeleton":
for i in range (30):
bullets.append(projectile(250, 250, 6, (0, 0, 0), 1))
for bullet in bullets:
if bullet.y - bullet.radius < mainchar.hitbox[1] + mainchar.hitbox[3] and bullet.y + bullet.radius > mainchar.hitbox[1]:
if bullet.x + bullet.radius > mainchar.hitbox[0] and bullet.x - bullet.radius < mainchar.hitbox[0] + mainchar.hitbox[2]:
mainchar.hit()
bullets.pop(bullets.index(bullet))
elif bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
redrawGameWindow(mainchar, bullet, bullets)
run = False
pygame.quit()
#Actual Running
dankamu("skeleton")
print("Dankamu success")