Enemies won't go visible after health reaches 0 - how to fix? - python

In my main loop I add health for the enemy. If I keep hitting the enemy and the health drops to 0, the enemy should disappear (enemys1.visible = False).
I ran the visible variable on my enemy class but for some reason the enemy wont go invisible and still shows up when its health reaches 0.
Here is a video of the problem in my game.
for bullet in bullets:
if bullet.rect.colliderect(enemys1.hitbox):
bullets.pop(bullets.index(bullet))
if enemys1.health > 0:
enemys1.health -= 1
else:
enemys1.visible = False # if my health reaches 0 the enemy should disappear but it doesnt
print('hit')
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)
# 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

for bullet in bullets: # never mind I fixed it
if bullet.rect.colliderect(enemys1.hitbox):
bullets.pop(bullets.index(bullet))
if enemys1.health > 0:
enemys1.health -= 1
else:
for one in range(len(enemying)-1,-1,-1):
del enemying[one]

Related

How do I get the enemy to chase the player? I would also like to know how to jump onto the moving object when I jump and not just go through it

moving object. It currently moves left and right across the screen and I would like the user to be able to jump on it.
def moving_object():
global x_speed, y_speed
pygame.draw.rect(win, (200, 140, 150), rectangle)
rectangle.x += x_speed
if rectangle.right >= 500 or rectangle.left <= 0:
x_speed *= -1
class for my enemy. They currently move left and right across the screen but I would like them to follow the user. They can also not jump
class enemy():
walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'), pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'), pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'), pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'), pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
def __init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.WalkCount = 0
self.vel = 3
self.path = [self.x, self.end]
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.health = 10
self.visible = True
def draw(self, win):
self.move()
if self.visible:
if self.WalkCount + 1 >= 33:
self.WalkCount = 0
if self.vel > 0:
win.blit(self.walkRight[self.WalkCount //3], (self.x, self.y))
self.WalkCount += 1
else:
win.blit(self.walkLeft[self.WalkCount //3], (self.x, self.y))
self.WalkCount += 1
pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
pygame.draw.rect(win, (0, 255, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.WalkCount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.WalkCount = 0
def hit(self):
if self.health > 0:
self.health -= 1
else:
self.visible = False
pass
So here is one way to make a pathfinding algorithm:
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((700, 500))
clock = pygame.time.Clock()
to_coords = [550, 100]
from_coords = (150, 400)
def go_to_koeff(from_, to):
dx = to[0] - from_[0]
dy = to[1] - from_[1]
if dx != 0:
dx /= abs(dx)
if dy != 0:
dy /= abs(dy)
return dx, dy
follow_coords_x = from_coords[0]
follow_coords_y = from_coords[1]
velocity = 1
run = True
while run:
clock.tick(60)
screen.fill((0, 0, 0))
pygame.draw.circle(screen, (0, 255, 0), to_coords, 5)
pygame.draw.circle(screen, (255, 0, 0), from_coords, 5)
pygame.draw.circle(screen, (255, 255, 255), (follow_coords_x, follow_coords_y), 5)
koeff = go_to_koeff((follow_coords_x, follow_coords_y), to_coords)
follow_coords_x += velocity * koeff[0]
follow_coords_y += velocity * koeff[1]
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
to_coords[1] -= 3
if keys[pygame.K_DOWN]:
to_coords[1] += 3
if keys[pygame.K_LEFT]:
to_coords[0] -= 3
if keys[pygame.K_RIGHT]:
to_coords[0] += 3
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
so basically the whole algorithm is that go_to_koeff() function: that function returns the koefficent of direction that can be used to change where the white dot moves that can be seen where the coefficient is multiplied by velocity. This algorithm isn't perfect but it finds the position so there is that. (You can play around a bit by moving the green dot with arrow keys to see how it moves)
Another idea that came to mind was tracking player position in a list and making the enemy follow all the coords in the list which could be even worse but it will feel like it is actually following You.
Also I just noticed that there is some useless change of variable names, but it doesn't affect anything much.

Why can't my enemy healthbar shrink after it collides with a bullet?

So I am currently making a game where the hotdog shoots bullets that should damage the Enemy. But after the bullet-enemy collision, The red healthbar that signifies its health does not shrink at all. Any tips on how to fix this? Thanks in advance.
import pygame
import random
import math
# Screen parameters
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("SPPACCE")
bg = pygame.image.load("bg.png")
font = pygame.font.SysFont('comicsans', 30, True)
clock = pygame.time.Clock()
score = 0
# Player parameters
class Player(object):
def __init__(self, x, y, height, width):
self.x = x
self.y = y
self.height = height
self.width = width
self.player_vel = 5
def draw(self, screen):
screen.blit(player_char, (self.x, self.y))
# Enemy parameters
class Enemy(object):
def __init__(self, x, y, height, width, end):
self.x = x
self.y = y
self.height = height
self.width = width
self.enemy_vel = 1.5
self.end = end
self.path = [self.x, self.end]
self.hitbox = (self.x + 17, self.y + 2, 65, 65)
self.health = 10
self.visible = True
def draw(self, screen):
self.move()
if self.visible:
self.hitbox = (self.x + 0, self.y, 65, 65)
pygame.draw.rect(screen, (255, 0, 0), self.hitbox, 2)
screen.blit(enemy_char, (self.x, self.y))
# Health bars
pygame.draw.rect(screen, (0, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 65, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 65 - (6.5 * (10 - self.health)), 10))
def move(self):
if self.enemy_vel > 0:
if self.x < self.path[1] + self.enemy_vel:
self.x += self.enemy_vel
else:
self.enemy_vel = self.enemy_vel * -1
self.x += self.enemy_vel
else:
if self.x > self.path[0] - self.enemy_vel:
self.x += self.enemy_vel
else:
self.enemy_vel = self.enemy_vel * -1
self.x += self.enemy_vel
def hit(self):
if self.health > 0:
self.health -= 1
else:
self.visible = False
# Player Projectile parameters
class Projectile(object):
def __init__(self, x, y, color, radius):
self.x = x
self.y = y
self.color = color
self.radius = radius
self.vel = 12.5
def draw(self, screen):
pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)
# Images
player_char = pygame.image.load('sprites/hotdog.png')
enemy_char = pygame.image.load('sprites/hamburger.png')
def blit(): # This draws the sprites
player.draw(screen)
enemy.draw(screen)
for projectile in projectiles:
projectile.draw(screen)
score_text = font.render("Score: " + str(score), 1, (0, 109, 255))
version = font.render("Version 01 ", 1, (51, 153, 255))
screen.blit(score_text, (0, 0))
screen.blit(version, (520, 0))
shootloop = 0
if shootloop > 0:
shootloop += 1
if shootloop > 2:
shootloop = 0
player = Player(300, 400, 64, 64)
enemy = Enemy(random.randint(10, 100), random.randint(20, 100), 64, 64, 480)
projectiles = []
run = True
while run:
clock.tick(60)
screen.fill((0, 0, 0))
screen.blit(bg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# Movement keys with playeborders
keys = pygame.key.get_pressed()
if keys[pygame.K_s] and player.y < 480 - player.height - player.player_vel:
player.y += player.player_vel
if keys[pygame.K_w] and player.y > 280:
player.y -= player.player_vel
if keys[pygame.K_d] and player.x < 640 - player.width - player.player_vel:
player.x += player.player_vel
if keys[pygame.K_a] and player.x > player.player_vel:
player.x -= player.player_vel
for projectile in projectiles:
if projectile.y - projectile.radius < enemy.hitbox[1] + enemy.hitbox[3] and projectile.y + projectile.radius > enemy.hitbox[1]:
if projectile.x + projectile.radius > enemy.hitbox[0] and projectile.x - projectile.radius < enemy.hitbox[0] + enemy.hitbox[2]:
score += 1
projectiles.pop(projectiles.index(projectile))
if projectile.y < 640 and projectile.y > 0:
enemy.hit
projectile.y -= projectile.vel
else:
projectiles.pop(projectiles.index(projectile))
# Player shooting
if keys[pygame.K_SPACE] and shootloop == 0:
if len(projectiles) < 1:
projectiles.append(Projectile(round(player.x + player.width //2),
round(player.y + player.height //2), [255, 150, 0], 7))
blit()
pygame.display.update()
I followed TechwithTim's tutorial by the way. Also, I think this is enough to run the code.
There are two issues with your code. The first, as #Apple points out, is that you are not correctly calling enemy.hit().
The other issue is that the enemy.hit() is not being called in the correct spot. It needs to be called after the collision is detected, and the existing code has it where the projectile moves up:
for projectile in projectiles:
if projectile.y - projectile.radius < enemy.hitbox[1] + enemy.hitbox[3] and projectile.y + projectile.radius > enemy.hitbox[1]:
if projectile.x + projectile.radius > enemy.hitbox[0] and projectile.x - projectile.radius < enemy.hitbox[0] + enemy.hitbox[2]:
score += 1
projectiles.pop(projectiles.index(projectile))
if projectile.y < 640 and projectile.y > 0:
enemy.hit # <<-- HERE
projectile.y -= projectile.vel
else:
projectiles.pop(projectiles.index(projectile))
If you change it to apply the hit() when the projectile collides, it seems to work as desired:
for projectile in projectiles:
if projectile.y - projectile.radius < enemy.hitbox[1] + enemy.hitbox[3] and projectile.y + projectile.radius > enemy.hitbox[1]:
if projectile.x + projectile.radius > enemy.hitbox[0] and projectile.x - projectile.radius < enemy.hitbox[0] + enemy.hitbox[2]:
# Enemy was hit by the projectile
score += 1
enemy.hit() # <<-- HERE
projectiles.pop(projectiles.index(projectile))
if projectile.y < 640 and projectile.y > 0:
# move the projectile up
projectile.y -= projectile.vel
else:
# projectile went off-screen
projectiles.pop(projectiles.index(projectile))
Please don't forget to add comments to your code, they really really help.
I don't know if it's the only issue, but you are missing a set of parentheses when you are attempting to call the hit method:
enemy.hit
Should be
enemy.hit()

list index out of range Error Pygame How To Fix?

window.blit(self.esright[self.Walking//3], (self.x,self.y))
IndexError: list index out of range
I am not sure how to fix this I tried to fix it but I keep getting the same error
# this makes the enemy move right and left
def draw(self,window):
self.move()
if self.Walking + 1 >= 33:
self.Walking = 0
if self.vel > 0:
window.blit(self.esright[self.Walking//3], (self.x,self.y))
self.Walking += 1
else:
window.blit(self.esleft[self.Walking//3], (self.x,self.y))
self.Walking += 1
the class of the enemy
# ---------------------------------------------- # this for bird my g LOl
class bird:
def __init__(self,x,y,height,width,end):
self.x = x
self.y =y
self.esright = [pygame.image.load("bird1.png"),
pygame.image.load("bird2.png"),
pygame.image.load("bird3.png"),
pygame.image.load("bird4.png")
]
self.esleft = [pygame.image.load("b1.png"),
pygame.image.load("b2.png"),
pygame.image.load("b3.png"),
pygame.image.load("b4.png")
]
self.esright = [pygame.transform.scale(image,(image.get_width()//5,image.get_height()//5)) for image in self.esright]
self.esleft = [pygame.transform.scale(image,(image.get_width()//5,image.get_height()//5)) for image in self.esleft]
self.height = height
self.width = width
self.distance = 80
self.speed = 8
self.vel = 3
self.path = [x,end]
self.Walking = 0
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.rect = pygame.Rect(x,y,height,width)
# enemys health
self.health = 10
self.soko = 0
self.visible = True
# this makes the enemy move right and left
def draw(self,window):
self.move()
if self.Walking + 1 >= 33:
self.Walking = 0
elif self.vel > 0:
window.blit(self.esright[self.Walking//3], (self.x,self.y))
self.Walking += 1
else:
window.blit(self.esleft[self.Walking//3], (self.x,self.y))
self.Walking += 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 = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.Walking = 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
# define the enemy class
black = (0,0,0)
bird1 = bird(300,199,104,64,500)
birds = [bird1]
fefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefesfefes
Change self.Walking//3 to self.Walking % 4
if you want it to be computed on each iteration like :
0 1 2 3 0 1 2 3 0 1...etc
Change self.Walking//3 to int(self.Walking / 4) % 4
if you want it to be computed on each iteration like :
0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 0 0 0 0 1 1 1 1...etc
Try making your second if statement to be part of the elif like this I think
def draw(self,window):
self.move()
if self.Walking + 1 >= 33:
self.Walking = 0
elif self.vel > 0:
window.blit(self.esright[self.Walking//3], (self.x,self.y))
self.Walking += 1
else:
window.blit(self.esleft[self.Walking//3], (self.x,self.y))
self.Walking += 1
or try indenting your self.move() to a if, elif, else statements

Enemy Projectiles Aren't Appending On Screen

I have here my script that targets the player what ever position he is at but the projectiles aren't showing on my screen VIDEO. He isn't attacking at all, I don't know why. I am in my main loop I draw the bullets to.
my enemy bullet class
# enemys bullets
ksud = pygame.image.load("heart.png")
class Boolss(object):
def __init__(self, x, y,color, xspeed, yspeed):
self.x = x
self.y = y
self.xspeed = xspeed
self.yspeed = yspeed
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)
this goes on my main loop, it appends bullets and targets the player
for shootss in shootsright:
shootss.x += shootss.xspeed
shootss.y += shootss.yspeed
if shootss.x > 500 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
shootsright.pop(shootsright.index(shootss))
if len(shootsright) < 2:
start_x = round(enemyshoots1.x+enemyshoots1.width-107)
start_y = round(enemyshoots1.y + enemyshoots1.height-50)
target_x = playerman.x+playerman.width//2
target_y = playerman.y+playerman.width//2
dir_x, dir_y = target_x - start_x, target_y - start_y
distance = math.sqrt(dir_x**2 + dir_y**2)
if distance > 0:
shootsright.append(Boolss(start_x,start_y,(0,0,0),dir_x, dir_y))
I draw the bullets that are appending on my screen but they don't show
for shootss in shootsright:
shootss.draw(window)
my full code script
I didn't run code but I think you have wrong indentations and this make problem.
You append to shootsright` inside
for shootss in shootsright:
shootsright.append(...)
but at start shootsright is empty so it never runs for shootss in shootsright: and it never runs shootsright.append() - so shootsright is always empty.
Probably you have to move it outside this loop (you have to change indentations)
for shootss in shootsright:
shootss.x += shootss.xspeed
shootss.y += shootss.yspeed
if shootss.x > 500 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
shootsright.pop(shootsright.index(shootss))
# outside `for`-loop
if len(shootsright) < 2:
start_x = round(enemyshoots1.x+enemyshoots1.width-107)
start_y = round(enemyshoots1.y + enemyshoots1.height-50)
target_x = playerman.x+playerman.width//2
target_y = playerman.y+playerman.width//2
dir_x, dir_y = target_x - start_x, target_y - start_y
distance = math.sqrt(dir_x**2 + dir_y**2)
if distance > 0:
shootsright.append(Boolss(start_x,start_y,(0,0,0),dir_x, dir_y))
BTW: next time you can use print() in many places to see values in this list in different moments and to see which part of code is executed. It is called "print debuging".

How would I go about deleting a sprite? [duplicate]

This question already has answers here:
how to make image/images disappear in pygame?
(1 answer)
Do not display removed sprites, Pygame
(1 answer)
Closed 2 years ago.
So the problem is an trying to get my sprite to disappear once the health bar goes 0 but I can’t seem to that the most I can do is make him invisible but I am trying to delete my sprite not make it invisible
import pygame import sys
pygame.init()#We always need to initalize our pygame IN EVERY PROJECT/FILE
win = pygame.display.set_mode((500, 480))# Here win is representing "window" for our screen which we have set at 500 by 480
pygame.display.set_caption("Dragon Ball Z Mini-game")#We are giving our Window/Screen a name
walkRight = [pygame.image.load('image/young_goku_right_image0 - t.png'), pygame.image.load('image/young_goku_right_image1 - t.png'), pygame.image.load('image/young_goku_right_image2 - t.png'), pygame.image.load('image/young_goku_right_image3 - t.png'), pygame.image.load('image/young_goku_right_image4 - t.png')] walkLeft = [pygame.image.load('image/young_goku_left_image0 - t.png'), pygame.image.load('image/young_goku_left_image1 - t.png'), pygame.image.load('image/young_goku_left_image2 - t.png'), pygame.image.load('image/young_goku_left_image3 - t.png'), pygame.image.load('image/young_goku_left_image4 - t.png')] bg = pygame.image.load('image/bg2.jpg') char = pygame.image.load('image/young_goku - standing - t.png')
clock = pygame.time.Clock()
bulletSound = pygame.mixer.Sound("image/kiblast.wav") hitSound = pygame.mixer.Sound("image/Bomb+1.wav")
#bulletSound.play() music = pygame.mixer.music.load("image/Dragon Ball Z - Rock The Dragon.mp3") pygame.mixer.music.play(-1)
score = 0
class player(object): def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.isJump = False
self.left = False
self.right = False
self.walkCount = 0
self.jumpCount = 10
self.standing = True
self.hitbox = (self.x + 17, self.y + 11, 29, 52)
def draw(self, win):
font2 = pygame.font.SysFont("Papyrus", 45)
text2 = font2.render("DBZ Adventure!", 1, (255, 50, 25))
win.blit(text2, (120 - (text2.get_width() / 2), 15))
#pygame.display.flip()
if self.walkCount + 1 >= 10:
self.walkCount = 0
if not (self.standing):
if self.left:
win.blit(walkLeft[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
else:
if self.right:
win.blit(walkRight[0], (self.x, self.y))
else:
win.blit(walkLeft[0], (self.x, self.y))
self.hitbox = (self.x + 17, self.y + 11, 29, 52)
if keys[pygame.K_a]:
if self.left:
ki_stance_left = pygame.image.load("image/goku-ki_left.png")
win.blit(ki_stance_left, (self.x, self.y))
elif self.right:
ki_stance = pygame.image.load("image/goku-ki.png")
win.blit(ki_stance, (self.x, self.y))
if keys[pygame.K_SPACE]:
if self.left:#needed to put self.left and self the right
ki_stance_left = pygame.image.load("image/goku-ki_left.png")
win.blit(ki_stance_left, (self.x, self.y))
elif self.right:
ki_stance = pygame.image.load("image/goku-ki.png")
win.blit(ki_stance, (self.x, self.y))
pygame.display.flip()
def hit(self):
self.isJump = False
self.jumpCount = 10
self.x = 100
self.y = 410
self.walkCount = 0
font1 = pygame.font.SysFont('comicsans', 100)
text = font1.render('-5', 1, (255, 0, 0))
win.blit(text, (250 - (text.get_width() / 2), 200))
pygame.display.update()
i = 0
while i < 200:
pygame.time.delay(10)
i += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
i = 201
pygame.quit()
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):
ki = pygame.image.load("image/ki.png")
win.blit(ki, (self.x, self.y))
ki_stance = pygame.image.load("image/goku-ki.png")
win.blit(ki, (self.x, self.y))
pygame.draw.circle(win, (255,255,0), (self.x, self.y), self.radius) """ def draw2(self, win):
ki2 = pygame.image.load("image/b4.png")
win.blit(ki2, (self.x, self.y))
ki_stance = pygame.image.load("image/goku-ki.png")
win.blit(ki2, (self.x, self.y))
pygame.draw.rect(win, (255, 255, 0), (self.x, self.y), self.radius) """
class projectile2(): 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):
ki = pygame.image.load("image/b4.png")
win.blit(ki, (self.x, self.y))
ki_stance = pygame.image.load("image/goku-ki.png")
win.blit(ki, (self.x, self.y))
#pygame.draw.circle(win, (0,0,139), (self.x, self.y), self.radius)
class enemy(object): walkRight = [pygame.image.load("image/R1E.png"), pygame.image.load("image/R2E.png"),
pygame.image.load("image/R3E.png"), pygame.image.load("image/R4E.png"),
pygame.image.load("image/R5E.png"), pygame.image.load("image/R6E.png"),
pygame.image.load("image/R7E.png"), pygame.image.load("image/R8E.png"),
pygame.image.load("image/R9E.png"), pygame.image.load("image/R10E.png"),
pygame.image.load("image/R11E.png")] walkLeft = [pygame.image.load("image/L1E.png"), pygame.image.load("image/L2E.png"),
pygame.image.load("image/L3E.png"), pygame.image.load("image/L4E.png"),
pygame.image.load("image/L5E.png"), pygame.image.load("image/L6E.png"),
pygame.image.load("image/L7E.png"), pygame.image.load("image/L8E.png"),
pygame.image.load("image/L9E.png"), pygame.image.load("image/L10E.png"),
pygame.image.load("image/L11E.png")]
def __init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x, self.end]
self.walkCount = 0
self.vel = 3
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.health = 10
self.visible = True
def draw(self, win):
self.move()
if self.visible:
if self.walkCount + 1 >= 33:
self.walkCount = 0
if self.vel > 0:
win.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
else:
win.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
pygame.draw.rect(win, (0, 128, 0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
# pygame.draw.rect(win, (255,0,0), self.hitbox,2)
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
Here’s where I can keep on trying to delete my sprite but it does not seem to work? The most I can do is just make it invisible but it does not go away permanently.
def hit(self):
hit = enemy
if self.health > 0:
self.health -= 1
else:
self.visible = False
font2 = pygame.font.SysFont("Papyrus", 45)
text2 = font2.render("You won!", 5, (45, 50, 45))
win.blit(text2, (95 - (text2.get_width() / 3), 45))
pygame.display.flip()
print('hit')
class enemy2(): walkRight = [pygame.image.load("image/enemy-sprite-standing-T.gif"), pygame.image.load("image/enemy-sprite-moving_T.gif"), pygame.image.load("image/enemy-sprite-moving_T.gif"), pygame.image.load("image/enemy-sprite-move_right-T.gif")] walkLeft = [pygame.image.load("image/enemy-sprite-standing-T.gif"), pygame.image.load("image/enemy-sprite - moving_left-T.gif"), pygame.image.load("image/enemy-sprite - moving_left-T.gif"), pygame.image.load("image/enemy-sprite - moving_left2-T.gif")] def
__init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x, self.end]
self.walkcount = 0
self.vel = 3
self.hitbox = (self.x + 20, self.y, 28, 60)
self.health = 10
self.visible = True
def draw(self, win):
self.move()
if self.visible:
if self.walkcount + 0 >= 10:
self.walkcount = 0
if self.vel > 0:
win.blit(self.walkRight[self.walkcount // 3], (self.x, self.y))
self.walkcount += 1
else:
win.blit(self.walkLeft[self.walkcount // 3], (self.x, self.y))
self.walkcount += 1
pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
pygame.draw.rect(win, (0, 128, 0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 20, self.y, 28, 60)
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkcount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkcount = 0
def hit(self):
if self.health > 0:
self.health -= 1
else:
self.visible = False
self.visible = False
font2 = pygame.font.SysFont("Papyrus", 45)
text2 = font2.render("You won!", 5, (45, 50, 45))
win.blit(text2, (95 - (text2.get_width() / 3), 45))
pygame.display.flip()
print("Hit!!")
pass
def redrawGameWindow(): win.blit(bg, (0, 0)) text = font.render('Score: ' + str(score), 1, (0, 0, 0)) win.blit(text, (390, 5)) man.draw(win) goblin.draw(win) goblin2.draw(win) for bullet in bullets:
bullet.draw(win)
pygame.display.update()
# mainloop font = pygame.font.SysFont("comicsans", 30, True, True) man = player(200, 420, 64, 64) goblin = enemy(50, 410, 64, 64, 450) goblin2 = enemy2(250, 410, 64, 64, 450) shootloop = 0 bullets = [] run
= True while run: clock.tick(27) if goblin.visible == True:
if man.hitbox[1] < goblin.hitbox[1] + goblin.hitbox[3] and man.hitbox[1] + man.hitbox[3] > goblin.hitbox[1]:
if man.hitbox[0] + man.hitbox[2] > goblin.hitbox[0] and man.hitbox[0] < goblin.hitbox[0] + goblin.hitbox[2]:
man.hit()
score -= 5
print("Hurt")
if goblin2.visible == True:
if man.hitbox[1] < goblin2.hitbox[1] + goblin2.hitbox[3] and man.hitbox[1] + man.hitbox[3] > goblin2.hitbox[1]:
if man.hitbox[0] + man.hitbox[2] > goblin2.hitbox[0] and man.hitbox[0] < goblin2.hitbox[0] + goblin2.hitbox[2]:
man.hit()
score -= 5
print("Hurt")
if shootloop > 0:
shootloop += 1 if shootloop > 3:
shootloop = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
#pygame.quit()
sys.exit()
run = False
for bullet in bullets:
if bullet.y - bullet.radius < goblin.hitbox[1] + goblin.hitbox[3] and bullet.y + bullet.radius > goblin.hitbox[1]:
if bullet.x + bullet.radius > goblin.hitbox[0] and bullet.x - bullet.radius < goblin.hitbox[0] + goblin.hitbox[2]:
#hitSound.play()
goblin.hit()
score += 1
#bullets.pop(bullets.index(bullet))
for bullet in bullets:
if bullet.y - bullet.radius < goblin2.hitbox[1] + goblin2.hitbox[3] and bullet.y + bullet.radius > goblin2.hitbox[1]:
if bullet.x + bullet.radius > goblin2.hitbox[0] and bullet.x - bullet.radius < goblin2.hitbox[0] + goblin2.hitbox[2]:
#hitSound.play()
goblin2.hit()
score += 1
#bullets.pop(bullets.index(bullet))
if bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE] and shootloop == 0:
bulletSound.play()
if man.left:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(projectile(round(man.x + man.width // 3), round(man.y + man.height // 3), 6, (0, 0, 0), facing))
shootloop = 1
if keys[pygame.K_a] and shootloop == 0:
bulletSound.play()
if man.left:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(projectile2(round(man.x + man.width // 3), round(man.y + man.height // 3), 12, (0, 0, 0), facing))
shootloop = 1
if keys[pygame.K_LEFT] and man.x > man.vel:
man.x -= man.vel
man.left = True
man.right = False
man.standing = False elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
man.x += man.vel
man.right = True
man.left = False
man.standing = False else:
man.standing = True
man.walkCount = 0
if not (man.isJump):
if keys[pygame.K_UP]:
man.isJump = True
man.right = False
man.left = False
man.walkCount = 0 else:
if man.jumpCount >= -10:
neg = 1
if man.jumpCount < 0:
neg = -1
man.y -= (man.jumpCount ** 2) * 0.5 * neg
man.jumpCount -= 1
else:
man.isJump = False
man.jumpCount = 10
redrawGameWindow()
pygame.quit()
In order to move a created image off of the screen you can do either one of the following solutions.
1. Move the image off of the screen.
An example of moving a sprite off of a screen is as follows:
someImg = pygame.image.load("coolWater.png")
screen.blit(someImg, (int(someXValueOffScreen), int(someYValueOffScreen))
2. Make the image transparent
Here's an example of this:
someImg = pygame.image.load("coolWater.png")
someImg.image.fill((0,0,0,0)) #Makes the image transparent. The last parameter is the alpha value. An alpha value of 0 makes it transparent. The first 3 zeros is the RGB value
Quick note: You should format your code in this post correctly with encapsulating your code with these three characters " ``` ".

Categories

Resources