How Could I Add Blood Particle Effect After I killed An Enemy? - python

I have an Enemys That Move Left And Right With A HealthBar But How Could I add Particles When The Enemy Dies Like Blood Particles When The enemy Is Killed it appears and falls down and dispears?
Example: >>> VIDEO I killed the enemy and its deleted how could I make load blood particles same position as the enemy that just died
heres my enemy class
class enemys:
def __init__(self,x,y,height,width,end):
self.x = x
self.y =y
self.esright = [pygame.image.load("esright1.png"),
pygame.image.load("esright1.png"),
pygame.image.load("esright2.png"),
pygame.image.load("esright3.png"),
pygame.image.load("esright4.png"),
pygame.image.load("esright5.png"),
pygame.image.load("esright6.png"),
pygame.image.load("esright7.png"),
pygame.image.load("esright8.png"),
pygame.image.load("esright9.png"),
pygame.image.load("esright10.png"),
pygame.image.load("esright11.png"),
pygame.image.load("esright12.png"),
pygame.image.load("esright13.png"),
pygame.image.load("esright14.png"),
pygame.image.load("esright15.png"),
pygame.image.load("esright16.png"),
pygame.image.load("esright17.png"),
]
self.esleft = [pygame.image.load("esleft1.png"),
pygame.image.load("esleft1.png"),
pygame.image.load("esleft2.png"),
pygame.image.load("esleft3.png"),
pygame.image.load("esleft4.png"),
pygame.image.load("esleft5.png"),
pygame.image.load("esleft6.png"),
pygame.image.load("esleft7.png"),
pygame.image.load("esleft8.png"),
pygame.image.load("esleft9.png"),
pygame.image.load("esleft10.png"),
pygame.image.load("esleft11.png"),
pygame.image.load("esleft12.png"),
pygame.image.load("esleft13.png"),
pygame.image.load("esleft14.png"),
pygame.image.load("esleft15.png"),
pygame.image.load("esleft16.png"),
pygame.image.load("esleft17.png"),
]
self.esright = [pygame.transform.scale(image,(image.get_width()//3,image.get_height()//3)) for image in self.esright]
self.esleft = [pygame.transform.scale(image,(image.get_width()//3,image.get_height()//3)) for image in self.esleft]
self.height = height
self.width = width
self.anim_index = 0
self.distance = 80
self.speed = 8
self.vel = 3
self.path = [x,end]
self.Walking_index = 0
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.rect = pygame.Rect(x,y,height,width)
COOLDOWN = 30
# enemys health
self.health = 10
self.visible = True
# this makes the enemy move right and left
def draw(self,window):
self.move()
if self.Walking_index + 1 >= 33:
self.Walking_index = 0
if self.vel > 0:
window.blit(self.esright[self.Walking_index//3], (self.x,self.y))
self.Walking_index += 1
else:
window.blit(self.esleft[self.Walking_index//3], (self.x,self.y))
self.Walking_index += 1
# this moves the enemy left and right
def move(self):
if self.visible:
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.Walking_index = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.Walking_index = 0
# the hit box for the enemy the health
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 70, 10)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 20, 70 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 47, self.y + 31, 50, 72)
# THIS PART MAKES the enemy not scroll with the player
def scroll(self,sx, sy):
self.x += sx
self.y += sy
self.path[0] += sx
self.path[1] += sx
heres when the enemys health bar reaches 0 the enemy gets deleted how can I add particles when its deleted? blood particles that drop from the enemys position
# enemys 1
for bullet in bullets:
if bullet.rect.colliderect(enemys2.hitbox):
if enemys2.health > -5:
enemys2.health -= 1
bullets.pop(bullets.index(bullet))
hitesound.play()
# this function calss the -5 text appearing and dispearing on my screen
minusenemyhealthtext()
else:
for oe in range(len(enemyings)-1,-1,-1):
deathsound.play()
del enemyings[oe]

Here are your options:
Get a GIF, split it to multiple images, and just display each of those images one after another. Make all of that in a class.
Make a class (blood class) that would draw make instances of another class (particle class) that just moves outwards and disappears after a while.
I am not going to write the code out for you, you must write it yourself, but that is how you do it.

Related

Starting point for bullet including player rotation in Pygame

I'm making a simple topdown shooter game. The problem is with bullet starting point. I have player image where rifle barrel is between topmid and topright of rectangle. How to make bullet start always from the rifle barrel regardless image rotation?
# key input
def get_input(self, dt):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.direction.rotate_ip(dt * -360)
if keys[pygame.K_RIGHT]:
self.direction.rotate_ip(dt * 360)
self.angle = self.direction.angle_to((0, -1))
self.image = pygame.transform.rotate(self.image, self.angle)
self.rect = self.image.get_rect(center=self.rect.center)
if keys[pygame.K_UP]:
self.movement = 1
self.status = 'move'
else:
self.movement = 0
self.status = 'idle'
if keys[pygame.K_DOWN]:
self.movement = -1
self.status = 'move'
if keys[pygame.K_SPACE] and not self.bullet.sprites():
self.create_bullet(dt)
shoot = pygame.mixer.Sound('audio/rumble.flac')
shoot.play()
# movement
def move(self, speed, dt):
movement_v = self.direction * self.movement
if movement_v.length() > 0:
movement_v.normalize_ip()
self.pos += movement_v * dt * speed
self.rect = self.image.get_rect(center=self.pos)
# boundary
if self.rect.x <= 0:
self.rect.x = 0
if self.rect.x >= 1216:
self.rect.x = 1216
if self.rect.y <= 0:
self.rect.y = 0
if self.rect.y >= 704:
self.rect.y = 704
# create bullet instance
def create_bullet(self, dt):
self.bullet.add(Bullet(self.rect.center, self.direction.normalize(), self.bullet_speed, self.angle))
You have rotate the offset of the bullet starting point with pygame.math.Vector2.rotate. e.g.:
offset = pygame.math.Vecotr2(15, 10) # (15, 10) is just an example
rotated_offset = offset.rotate(-self.angle)
pos = pygame.math.Vector2(self.rect.center) + rotated_offset
bullet = Bullet((pos.x, pos.y), ...)

Pygame: Image not showing up when I use blit

I have started making a game in Pygame, and I am currently writing code for an enemy Object. It should choose a random location close (but not on) the player, and move towards that location. I have four images for the enemy to loop through, and if the enemy reaches the location before a random amount of time passes, I want to stay in place and stop looping through its animation. Instead, I want it to stay at the first image.
In my class Enemy, there is a function main that is called during the game loop to update its position on the screen. Here is the code in the function:
def main(self, display):
if self.animation_count + 1 >= 16:
self.animation_count = 0
self.animation_count += 1
if self.reset_offset == 0:
self.offset_x = randrange(-150, 150)
self.offset_y = randrange(-150, 150)
self.reset_offset = randrange(120, 150)
else:
self.reset_offset -= 1
if player.y + self.offset_y > self.y - display_scroll[1]:
self.y += 1
elif player.y + self.offset_x < self.x-display_scroll[1]:
self.y -= 1
if player.x + self.offset_x > self.x - display_scroll[0]:
self.x += 1
if player.y + self.offset_y == self.y - display_scroll[1] and player.x + self.offset_y == self.x - display_scroll[0]:
display.blit(pygame.transform.scale(self.animation_images[0], (32, 42)),
(self.x - display_scroll[0], self.y - display_scroll[1]))
else:
display.blit(pygame.transform.scale(self.animation_images[self.animation_count // 4], (32, 42)),
(self.x - display_scroll[0], self.y - display_scroll[1]))
elif player.x + self.offset_x < self.x-display_scroll[0]:
self.x -= 1
if player.y + self.offset_y == self.y - display_scroll[1] and player.x + self.offset_y == self.x - display_scroll[0]:
display.blit(pygame.transform.scale(self.animation_images[0], (32, 42)),
(self.x - display_scroll[0], self.y - display_scroll[1]))
else:
display.blit(pygame.transform.scale(pygame.transform.flip(
self.animation_images[self.animation_count // 4], True, False),
(32, 42)), (self.x - display_scroll[0], self.y - display_scroll[1]))
When I run it, and the enemy reaches its location before its time is up, it disappears instead. Does anyone know what could be causing this error?
Also, display_scroll is a list I use to draw all other objects. The player remains in the same spot while everything else moves. display_scroll[0] is the x value and display_scroll[1] is the y value.
In addition, I added the line print(self.x - display_scroll[0], self.y - display_scroll[1]) at the end of the function just to see where python was trying to draw the character. It always printed out normal values, nothing out of the ordinary.
the answer is pretty simple:
you are checking
if player.x + self.offset_x > self.x - display_scroll[0]:
[show the image]
elif player.x + self.offset_x < self.x-display_scroll[0]:
[show the image]
so if the enemy is to the right of the player or if it is to the left of the player it will be drawn. but it will not be drawn, if it is on the same x-coordinate as the player.
in order to achieve this you need to add an else statement for this:
else:
display.blit(pygame.transform.scale(self.animation_images[0], (32, 42)),
(self.x - display_scroll[0], self.y - display_scroll[1]))
# or whatever animation you want to play

My Projectiles Stop Shooting Problem How To Fix?

VIDEO When my player distance is a little farther than the knife they stop shooting at the player I am not sure why? How could I fix it and make sure it shoots where ever the player distance is. Like I don't want it to stop shooting when the player is a little farther away from the projectile I am not sure if I have a range for it to shoot the player or maybe when my player scrolls
when it stops shooting at the player the shooting sound still plays but my projectiles aren't shooting and that causes my sound to keep playing rapidly.
This is how my enemies shoot:
for shootss in shootsright:
shootss.x += shootss.xspeed
shootss.y += shootss.yspeed
if shootss.x > 700 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
shootsright.pop(shootsright.index(shootss))
shootss.lookAt((playerman.x,playerman.y))
if box1.health > 25:
if len(shootsright) < 1:
for enemyshoot in enemyshooting:
BULLET_SPEED = 10
start_x = round(enemyshoot.x+enemyshoot.width+-35)
start_y = round(enemyshoot.y + enemyshoot.height+-25)
target_x = playerman.x+playerman.width//2
target_y = playerman.y+playerman.width//2
delta_x, delta_y = target_x - start_x, target_y - start_y
distance = math.sqrt(delta_x ** 2 + delta_y ** 2)
dir_x = BULLET_SPEED * delta_x / distance
dir_y = BULLET_SPEED * delta_y / distance
distance = math.sqrt(dir_x**2 + dir_y**2)
knifesound.play()
if distance > 0:
shootsright.append(enemyboolss(start_x,start_y,(0,0,0),dir_x, dir_y))
This is the class for the projectile:
class projectile(object):
def __init__(self, x, y, dirx, diry, color):
self.x = x
self.y = y
self.dirx = dirx
self.diry = diry
self.isJump = False
self.slash = pygame.image.load("round.png")
self.slash = pygame.transform.scale(self.slash,(self.slash.get_width()//6,self.slash.get_height()//6))
self.rect = self.slash.get_rect()
self.rect.topleft = ( self.x, self.y )
self.speed = 18
self.color = color
self.hitbox = (self.x + -18, self.y, 46,60)
def move(self):
self.x += self.dirx * self.speed
self.y += self.diry * self.speed
def draw(self, window):
self.rect.topleft = (round(self.x), round(self.y))
window.blit(self.slash, self.rect)
self.hitbox = (self.x + -18, self.y, 30,30)
my full code: script
Your screen is 800x800, but the knife code is checking 700x500. The knife projectile is created but immediately removed from the knife list at the next loop:
if shootss.x > 700 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
shootsright.pop(shootsright.index(shootss))
shootss.lookAt((playerman.x,playerman.y))
This causes the knife flash but no movement for knives past 700.
After setting the check to 800, the knives worked correctly including sound.

How Can I Make My Enemys Projectile Attack The Player Where Ever The Player Moves?

I have an enemy that shoots projectiles but only shoots to the right I want it to shoot at the player with any position I don't know how to do that heres a vid what I have done so fare if you could walk me throw the steps on how to do it that would be great Thank You
this is what I have done so fare it only shoots to the right I want it to shoot at the player within any position the player is at
for shootss in shootsright:
if shootss.x < 500 and shootss.x > 0:
shootss.x += 7
else:
shootsright.pop(shootsright.index(shootss))
if len(shootsright) < 1:
shootsright.append(Bools(round(enemyshoots1.x+enemyshoots1.width-107),round(enemyshoots1.y + enemyshoots1.height-50),(0,0,0)))
and here is my bullet class
# enemys bullets
ksud = pygame.image.load("heart.png")
class Bools(object):
def __init__(self, x, y,color):
self.x = x
self.y = y
self.ksud = pygame.image.load("heart.png")
self.hitbox = self.ksud.get_rect()
self.rect = self.ksud.get_rect()
self.rect.topleft = (self.x,self.y)
self.speed = 10
self.color = color
self.hitbox = (self.x + 57, self.y + 33, 29, 52) # NEW
def draw(self, window):
self.rect.topleft = (self.x,self.y)
player_rect = self.ksud.get_rect(center = self.rect.center)
player_rect.centerx += 0 # 10 is just an example
player_rect.centery += 0 # 15 is just an example
window.blit(self.ksud, player_rect)
self.hitbox = (self.x + 97, self.y + 33, 10, 10) # NEW
window.blit(self.ksud,self.rect)
my enemys class
shotsright = pygame.image.load("shooting2.png")
shotsleft = pygame.image.load("shooting1.png")
class enemyshoot:
def __init__(self,x,y,height,width,color):
self.x = x
self.y =y
self.height = height
self.width = width
self.color = color
self.shootsright = pygame.image.load("shooting2.png")
self.shotsleft = pygame.image.load("shooting1.png")
self.shootsright = pygame.transform.scale(self.shootsright,(self.shootsright.get_width()//3,self.shootsright.get_height()//3))
self.shotsleft = pygame.transform.scale(self.shotsleft,(self.shotsleft.get_width()//3,self.shotsleft.get_height()//3))
self.rect = pygame.Rect(x,y,height,width)
self.health = 10
self.hitbox = (self.x + -20, self.y + 30, 31, 57)
def draw(self):
self.rect.topleft = (self.x,self.y)
window.blit(self.shootsright,self.rect)
self.hits = (self.x + 20, self.y, 28,60)
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 50, 10)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 50 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 60, self.y + 80, 81, 87)
black = (0,0,0)
enemyshoots1 = enemyshoot(1100,240,100,100,black)
enemyshooting = [enemyshoots1]
here is my full code
script
Looking at your script didn't help me a lot as you are maintaining many different classes.
for shootss in shootsright:
if shootss.x < 500 and shootss.x > 0:
shootss.x += 7
Here it appears that when you are updating your bullets x coordinate you are incrementing it with a positive constant value.
And then you update it like this in your bullet class.
window.blit(self.ksud,self.rect)
What you can do instead is have a bullet speed that changes its direction.You can use a code like this
if object_you_want_to_follow.x > object_shooting_the_bullet.x:
bulletSpeed = +7
elif object_you_want_to_follow.x < object_shooting_the_bullet.x:
bulletSpeed = -7
Same applies if you want to follow it vertically along the y direction.
OPTIMIZATION TIP
From what i understand having this in your bullet class makes no difference as you are updating it soon after.
window.blit(self.ksud, player_rect)
EDIT
To make things easier you can simply do this
for shootss in shootsright:
if shootss.x < 500 and shootss.x > 0:
if enemy.x < playerman.x:
shootss.x += 7
else:
shootss.x -= 7
else:
shootsright.pop(shootsright.index(shootss))
if len(shootsright) < 1:
shootsright.append(Bools(round(enemyshoots1.x+enemyshoots1.width-107),round(enemyshoots1.y + enemyshoots1.height-50),(0,0,0)))

brick game bounce off the walls

I have a huge assignment due in class and Im lost like , asking god to spare me lost. Im making a brick game and the most basic function of getting the ball to bounce of the walls I cant figure out , my teacher is too busy and classmates are competitive , I dont want you to write the code for me, I just dont know how to code it. I keep getting errors and its just been hell the last couple of classes.
import math
import pygame
import random
pygame.init()
screen = pygame.display.set_mode([800,600])
done = False
clock = pygame.time.Clock()
#DEFINE COLORS
WHITE = (255,255,255)
BLUE=(0,102,204)
LIGHT_BLUE=(0,0,204)
pink=(238,130,238)
#import images
#lives = pygame.image.load("heart.png")
# initialize font; must be called after 'pygame.init()' to avoid 'Font not Initialized' error
myfont = pygame.font.SysFont("monospace", 15)
#var carying the space of the game
top = -100
left= -50
right= -750
bottom= -600
angle = math.radians(random.randint(-140,-30))
class game_screen():
def draw_screen():
pygame.draw.rect(screen,BLUE,(50,100,700,600))
def save_button():
pygame.draw.rect(screen,pink,(500,20,50,30),0)
save = "Save"
label = myfont.render(save, 40, (0,0,0))
screen.blit(label, (505, 20))
def quit_button():
pygame.draw.rect(screen,pink,(600,20,50,30),0)
quit1 = "Quit"
label = myfont.render(quit1, 40, (0,0,0))
screen.blit(label, (605, 20))
mX, mY = pygame.mouse.get_pos()
mouseButtons = pygame.mouse.get_pressed()
if mouseButtons[0] == True:
if (mX in range (600-20,600+20) and mY in range (20-20,20+20)):
pygame.quit()
#def display_lives():
#lives_counter = screen.blit(lives,(50,30))
#lives_counter2 = screen.blit(lives,(120,30))
#lives_counter3 = screen.blit(lives,(190,30))
class PADDLE:
def __init__(self,xpos,ypos):
self.x = xpos
self.y = ypos
def draw(self): # draws paddle
pygame.draw.rect(screen,pink,(self.x,self.y,70,20))
def move(self):
keys = pygame.key.get_pressed() #checking pressed keys
if keys[pygame.K_LEFT]:
if self.x<=50:
self.x=50
else:
self.x -= 10
elif keys[pygame.K_RIGHT]:
if self.x >=680:
self.x = 680
else:
self.x += 10
class BALL:
def __init__(self,paddle1):
self.x = (paddle1.x+35)
self.y = (paddle1.y-5)
self.speed = 0
self.speedX = 0
self.speedY = 0
self.direction = 200
def draw(self):
pygame.draw.circle(screen,WHITE,(self.x,self.y),10)
def bounce(self):
self.direction = (180 - self.direction) % 360
def move_ball(self):
keys = pygame.key.get_pressed() #checking pressed keys
ball_on_paddle = True
if ball_on_paddle == True :
self.x = (paddle1.x+35)
self.y = (paddle1.y-5)
self.speed = 0
if keys[pygame.K_UP] == True:
ball_on_paddle = False
print("a")
self.speed = 10
self.speedX += int(math.cos(angle)* self.speed)
self.speedY += int(math.sin(angle)* self.speed)
print(bottom)
print(self.speedY)
if self.y <= 0:
self.bounce(0)
self.y = 1
if self.x <= 0:
self.direction = (360 - self.direction) % 360
self.x = 1
if self.x > self.screenwidth - self.width:
self.direction = (360 - self.direction) % 360
self.x = self.screenwidth - self.width - 1
paddle1 = PADDLE(350,550)
ball1 = BALL(paddle1)
# MAIN LOOP
while not done:
screen.fill((LIGHT_BLUE))
game_screen.draw_screen()
game_screen.save_button()
game_screen.quit_button()
paddle1.draw()
paddle1.move()
ball1.draw()
ball1.move_ball()
ball1.bounce()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.display.flip()
clock.tick(60)
pygame.quit()
The collission between ball and wall can be detected by comparing the ball's coordinates with the coordinates of the walls. Reflecting the ball means a multiplication of either speedX or speedY - depending on the wall - with -1.
The easiest way to do this is to make a function bounce(self) in your class BALL.
First, you decide which border you touch.
Then, you change the angle of the ball according to which border you bounce on. If you work in a 360-degree system, the angle is mirrored:
around a vertical axis when the ball bounces of the bottom and the top border. So angle = 180 - angle.
around a horizontal axis when the ball bounces of the side borders. So angle = - angle .
And lastly, you have to move the ball back what it has travelled too much.
Like you can see, the ball travels in 1 iteration from C1 to C3', while we want it to go to C3. We can achieve this by reflecting it around the axis BOUNDARY_X - ball_size.
The new x-coordinate of the ball becomes then x = BOUNDARY_X - ball_size - (x - BOUNDARY_X + ball_size) = 2*(BOUNDARY_X - ball_size) - x .
When we cast that into code, we get:
class BALL:
def bounce(self):
if self.x < ball_size:
self.angle = math.pi - self.angle
self.x = 2*ball_size - self.x
elif self.x > DISPLAY_WIDTH - ball_size:
self.angle = math.pi - self.angle
self.x = 2*(DISPLAY_WIDTH - ball_size) - self.x
if self.y < ball_size:
self.angle = - self.angle
self.y = 2*ball_size - self.y
elif self.y > DISPLAY_HEIGHT - ball_size:
self.angle = - self.angle
self.y = 2*(DISPLAY_HEIGHT - ball_size) - self.y

Categories