pygame images not loading? - python

How come my Goku sprites aren't loading my left and right image appear correct but my others aren't playing in motion? the mian thing am trying to accomplish is for my character images to work correctly in motion
below is the code with a link to the full code description:
https://pastebin.com/umMJHNQj
import pygame
pygame.init()#We always need to initialize 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("First Game")#We are giving our Window/Screen a name
walkRight = [pygame.image.load('image/gokuR0.png'), pygame.image.load('image/gokutest2.png'), pygame.image.load('image/gokuR2.png')]
walkLeft = [pygame.image.load('image/gokuL0.png'), pygame.image.load('image/gokutest.png'), pygame.image.load('image/gokuL2.png')]
bg = pygame.image.load('image/bg.jpg')
char = pygame.image.load('image/goku sprite - standing.png')
clock = pygame.time.Clock()
class player():
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 = False
Here is where i have tried to alter the code but yet no results.
def draw(self, win):
if self.walkCount + 1 >= 8:
self.walkCount = 0
if not self.standing:
if self.left:
win.blit(walkLeft[self.walkCount // 100], (self.x, self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount // 100], (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))
pygame.display.flip()
print(walkLeft)
print(walkRight)
class projectile():
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)
All these images are working correctly yet can't seem to get my main characters images to work in properly in motion
class enemy():
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
def draw(self,win):
self.move()
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
print(walkLeft)
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 redrawGameWindow():
win.blit(bg, (0, 0))
goblin.draw(win)
man.draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
# mainloop
man = player(200, 400, 85, 85)
goblin = enemy(100, 400, 64, 64, 450)
bullets = []
run = True
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
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]:
if man.left:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(
projectile(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing))
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()

The issue is that self.walkCount is (integral) divided by 100:
win.blit(walkLeft[self.walkCount // 100], (self.x, self.y))
Note self.walkCount will never become grater than 7, because of:
if self.walkCount + 1 >= 8:
self.walkCount = 0
So the result of self.walkCount // 100 is always 0.
Anyway this code makes no sense, since walkLeft and walkRight are lists containing 3 images. I recommend to do the following:
def draw(self, win):
walkfps = 10
if self.standing:
self.walkCount = 0
if self.right:
win.blit(walkRight[0], (self.x, self.y))
else:
win.blit(walkLeft[0], (self.x, self.y))
elif self.left:
if self.walkCount // walkfps >= len(walkLeft):
self.walkCount = 0
win.blit(walkLeft[self.walkCount // walkfps], (self.x, self.y))
self.walkCount += 1
elif self.right:
if self.walkCount // walkfps >= len(walkRight):
self.walkCount = 0
win.blit(walkRight[self.walkCount // walkfps], (self.x, self.y))
self.walkCount += 1
If the images change to rapidly, then increase walkfps.

Related

The text isn't coming up in pygame when it is supposed to and causing problems [duplicate]

This question already has answers here:
How to detect collisions between two rectangular objects or images in pygame
(1 answer)
How do I detect collision in pygame?
(5 answers)
Closed 2 years ago.
So, I am making a simple game in which when the player collides with the enemy it's health is supposed to decrease with a graphic of a -5 popping up and then the game restarts. But when I start the programme the -5 pops up while it is not even supposed to and the rest of the screen stays black. And their is no error except a warning that has nothing to do with the messed up graphics. So, I am just going to paste the whole code as I think there is a problem with the player class, the scoring method and also with the blit method.
import pygame
pygame.init()
win = pygame.display.set_mode((500,480))
pygame.display.set_caption("First Game")
walkRight = [pygame.image.load('Game//R1.png'), pygame.image.load('Game//R2.png'), pygame.image.load('Game//R3.png'), pygame.image.load('Game//R4.png'), pygame.image.load('Game//R5.png'), pygame.image.load('Game//R6.png'), pygame.image.load('Game//R7.png'), pygame.image.load('Game//R8.png'), pygame.image.load('Game//R9.png')]
walkLeft = [pygame.image.load('Game//L1.png'), pygame.image.load('Game//L2.png'), pygame.image.load('Game//L3.png'), pygame.image.load('Game//L4.png'), pygame.image.load('Game//L5.png'), pygame.image.load('Game//L6.png'), pygame.image.load('Game//L7.png'), pygame.image.load('Game//L8.png'), pygame.image.load('Game//L9.png')]
bg = pygame.image.load('Game//bg.jpg')
char = pygame.image.load('Game//standing.png')
clock = pygame.time.Clock()
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):
if self.walkCount + 1 >= 27:
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)
#pygame.draw.rect(win, (255,0,0), self.hitbox,2)
def hit(self):
self.x = 60
self.y = 410
self.walkCount = 0
font1 = pygame.font.SysFont('comicsans', 100)
text = font1.render('-5', 1, (255,0,0))
win.blit(text, (250 - round(text.get_width()/2), 200))
pygame.display.update()
i = 0
while i < 300:
pygame.time.delay(10)
i += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
i = 301
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):
pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)
class enemy(object):
walkRight = [pygame.image.load('Game//R1E.png'), pygame.image.load('Game//R2E.png'), pygame.image.load('Game//R3E.png'), pygame.image.load('Game//R4E.png'), pygame.image.load('Game//R5E.png'), pygame.image.load('Game//R6E.png'), pygame.image.load('Game//R7E.png'), pygame.image.load('Game//R8E.png'), pygame.image.load('Game//R9E.png'), pygame.image.load('Game//R10E.png'), pygame.image.load('Game//R11E.png')]
walkLeft = [pygame.image.load('Game//L1E.png'), pygame.image.load('Game//L2E.png'), pygame.image.load('Game//L3E.png'), pygame.image.load('Game//L4E.png'), pygame.image.load('Game//L5E.png'), pygame.image.load('Game//L6E.png'), pygame.image.load('Game//L7E.png'), pygame.image.load('Game//L8E.png'), pygame.image.load('Game//L9E.png'), pygame.image.load('Game//L10E.png'), pygame.image.load('Game//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
def hit(self):
if self.health > 0:
self.health -= 1
else:
self.visible = False
print('hit')
def redrawGameWindow():
win.blit(bg, (0,0))
text = font.render('Score: ' + str(score), 1, (0,0,0))
win.blit(text, (390, 10))
man.draw(win)
goblin.draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
#mainloop
font = pygame.font.SysFont('comicsans', 30, True)
man = player(200, 410, 64,64)
goblin = enemy(100, 410, 64, 64, 450)
shootLoop = 0
bullets = []
run = True
while run:
clock.tick(27)
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[0]:
man.hit()
score += 5
if shootLoop > 0:
shootLoop += 1
if shootLoop > 3:
shootLoop = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
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]:
goblin.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:
if man.left:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(projectile(round(man.x + man.width //2), round(man.y + man.height//2), 6, (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()
And so sorry for the inconvenience with the previous question, I hope this one clears out everything. And if it doesn't please let me know.
Without images it's hard to run that locally and observe what exactly happens, but at this point it seems that hit condition, the whole
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[0]:
is true at the very beginning. This means that as only main loop starts, it goes to man.hit(). Please check your initial settings for both items.
By the way, consider to make it somewhat more readable with changes like
if (
man.hitbox[1] < goblin.hitbox[1] + goblin.hitbox[3] and
man.hitbox[1] + man.hitbox[3] > goblin.hitbox[1] and
man.hitbox[0] + man.hitbox[2] > goblin.hitbox[0] and
man.hitbox[0] < goblin.hitbox[0] + goblin.hitbox[0]
):
...
Also you could think about moving that condition into some method called collide (form example), which could calculate that internally.

Why does Pygame crashing from list index error?

I am working on a game in Pygame and the window crashes a few seconds into execution. I traced it back to the list I used for the "enemy" sprite walking animation. I still cannot find what it is that it is having trouble within my list. I did the same exact thing with my player walking animations.
here is the error it threw:
Traceback (most recent call last):
File "/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic >platformer/basic_platformer.py", line 188, in <module>
redrawGameWindow()
File "/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic >platformer/basic_platformer.py", line 111, in redrawGameWindow
goblin.draw(ben.window)
File "/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic >platformer/basic_platformer.py", line 85, in draw
window.blit(self.walkRight[self.walkCount //3], (self.x, self.y))
IndexError: list index out of range
I've commented out the player and enemy classes and lists for walking; "walkRight, "walkLeft".
import sys
import pygame
pygame.init()
pygame.display.set_caption("Ben Meitzen")
#player walking lists
walkRight = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/sprites/tile%s.png' % frame) for frame in range(17, 24)]
walkLeft = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/sprites/tile%s.png' % frame) for frame in range(9, 16)]
bg = pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/skybackground.png')
char = pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/sprites/tile1.png')
clock = pygame.time.Clock()
#player class
class player(object):
def __init__(self,x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.velocity = 11
self.isJump = False
self.jumpCount = 7
self.right = False
self.left = False
self.walkCount = 0
self.window = pygame.display.set_mode((860,538))
self.standing = True
def draw(self, window):
if self.walkCount + 1 >= 8:
self.walkCount = 0
if not (self.standing):
if self.left:
self.window.blit(walkLeft[self.walkCount//1], (self.x, self.y))
self.walkCount += 1
elif self.right:
self.window.blit(walkRight[self.walkCount//1], (self.x, self.y))
self.walkCount += 1
else:
if self.right:
window.blit(walkRight[0], (self.x, self.y))
else:
window.blit(walkLeft[0], (self.x, self.y))
class projectile(object):
def __init__(self, x, y, radius, color, facing):
self.x = x
self.y = y
self.color = color
self.facing = facing
self.velocity = 40 * facing
self.radius = radius
def draw(self, window):
pygame.draw.circle(window, self.color, (self.x, self.y), self.radius)
#enemy class
class enemy(object):
#enemy walking lists
walkRight = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/enemy sprites/enemy%s.png' % frame) for frame in range(17, 24)]
walkLeft = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/enemy sprites/enemy%s.png' % frame) for frame in range(9, 16)]
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.velocity = 3
self.path = [self.x, self.end]
def draw(self, window):
self.move()
if self.walkCount + 1 >= 8:
self.walkcount = 0
if self.velocity > 0:
window.blit(self.walkRight[self.walkCount //3], (self.x, self.y))
self.walkCount += 1
else:
window.blit(self.walkLeft[self.walkCount //3], (self.x, self.y))
self.walkCount += 1
def move(self):
if self.velocity > 0:
if self.x + self.velocity < self.path[1]:
self.x += self.velocity
else:
self.velocity = self.velocity * -1
self.walkcount = 0
else:
if self.x - self.velocity > self.path[0]:
self.x += self.velocity
else:
self.velocity = self.velocity * -1
self.walkcount = 0
#game window-------------------------------------------------------------------------------------------------------------------------------------------------
def redrawGameWindow():
ben.window.blit(bg,(0,0))
ben.draw(ben.window)
goblin.draw(ben.window)
for bullet in bullets:
bullet.draw(ben.window)
pygame.display.update()
##main loop##------------------------------------------------------------------------------------------------------------------------------------------------
goblin = enemy(100, 460, 70, 70, 700)
ben = player(30, 460, 64, 64)
bullets = []
run = True
while run:
clock.tick(8)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
if bullet.x < 860 and bullet.x > 0:
bullet.x += bullet.velocity
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
if ben.left:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(projectile(round(ben.x + ben.width //2), round(ben.y + ben.height -30 //2), 6, (0,0,0), facing))
if keys[pygame.K_LEFT] and ben.x > ben.velocity:
ben.x -= ben.velocity
ben.left = True
ben.right = False
ben.standing = False
elif keys[pygame.K_RIGHT] and ben.x < 860 - ben.width - ben.velocity:
ben.x += ben.velocity
ben.right = True
ben.left = False
ben.standing = False
else:
ben.standing = True
ben.walkCount = 0
if not(ben.isJump):
if keys[pygame.K_UP]:
ben.isJump = True
ben.right = False
ben.left = False
else:
if ben.jumpCount >= -7:
neg = 0.5
if ben.jumpCount < 0:
neg = -0.5
ben.y -= (ben.jumpCount **2) * 1.5 * neg
ben.jumpCount -= 2
else:
ben.isJump = False
ben.jumpCount = 7
ben.velocity = 11
redrawGameWindow()
#-----------------------------------------------------------------------------------------------
pygame.quit()
In this part of the code:
if self.walkCount + 1 >= 8:
self.walkcount = 0
self.walkCount is written as self.walkcount (lowercase c). So, the actual walkCount is not being reset to zero, thus the list index out of range error.

IndexError: pop from empty list in pygame [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hi guy i have a problem with my game, i receive this error when u run it, "line 283, in
bullets.pop(0)", "IndexError: pop from empty list", i think it is because the list of bullets is empty and it cannot pop them, but i dont know how to fix it.
If you can help me i would appreciate it very much.
Here is the code, thanks.
import pygame
import math
import random
pygame.init()
win = pygame.display.set_mode((800, 400))
pygame.display.set_caption("Pandemic")
clock = pygame.time.Clock()
score = 0
walkup = [pygame.image.load("Soldier-1up.png"), pygame.image.load("Soldier-2up.png"), pygame.image.load("Soldier-3up.png")]
walkdown = [pygame.image.load("Soldier-1down.png"), pygame.image.load("Soldier-2down.png"), pygame.image.load("Soldier-3down.png")]
walkright = [pygame.image.load("Soldier-1right.png"), pygame.image.load("Soldier-2right.png"), pygame.image.load("Soldier-3right.png")]
walkleft = [pygame.image.load("Soldier-1left.png"), pygame.image.load("Soldier-2left.png"), pygame.image.load("Soldier-3left.png")]
bg = pygame.image.load("map.png")
ch = pygame.image.load("Soldier-1up.png")
bulletimg = pygame.image.load("bullet.png")
walkvirus1 = [pygame.image.load("virus1.png"), pygame.image.load("virus2.png"), pygame.image.load("virus3.png")]
walkvirus2 = [pygame.image.load("1virus1.png"), pygame.image.load("1virus2.png"), pygame.image.load("1virus3.png")]
background1 = pygame.image.load("menu.png")
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.walkcount = 0
self.right = False
self.left = False
self.up = False
self.down = False
self.standing = True
self.hitbox = (self.x, self.y, self.width, self.height)
self.life = 3
def draw(self, win):
if self.walkcount + 1 >= 60:
self.walkcount = 0
if not (self.standing):
if self.left:
win.blit(walkleft[self.walkcount // 20], (self.x, self.y))
self.walkcount += 1
elif self.right:
win.blit(walkright[self.walkcount // 20], (self.x, self.y))
self.walkcount += 1
elif self.up:
win.blit(walkup[self.walkcount // 20], (self.x, self.y))
self.walkcount += 1
elif self.down:
win.blit(walkdown[self.walkcount // 20], (self.x, self.y))
self.walkcount += 1
else:
if self.up:
win.blit(walkup[0], (self.x, self.y))
elif self.down:
win.blit(walkdown[0], (self.x, self.y))
elif self.right:
win.blit(walkright[0], (self.x, self.y))
elif self.left:
win.blit(walkleft[0], (self.x, self.y))
self.hitbox = (self.x, self.y, self.width, self.height)
#pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
def hit(self):
self.x = 0
self.y = 0
self.life -= 1
self.walkcount = 0
font1 = pygame.font.SysFont("comicsans", 100)
text = font1.render("-1 out of 3 lifes.", 1, (255,0,0))
win.blit(text, (400 - (text.get_width()/2),150))
pygame.display.update()
pygame.time.delay(1000)
if self.life == 0:
pygame.quit()
class Bullet(object):
def __init__(self, x, y, radius, color, vertfacing, hortfacing):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.speed = 8
self.vertfacing = vertfacing
self.hortfacing = hortfacing
def draw(self, screen):
pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)
def move(self):
if self.hortfacing == -1:
self.x -= self.speed
elif self.hortfacing == 1:
self.x += self.speed
elif self.vertfacing == 1:
self.y += self.speed
elif self.vertfacing == -1:
self.y -= self.speed
class enemy(object):
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 = 2
self.life = 3
self.hitbox = (self.x, self.y, self.width, self.height)
self.health = 15
self.visible = True
def draw(self, win):
self.move()
if self.visible:
if self.walkcount + 1 >= 60:
self.walkcount = 0
if self.vel > 0:
win.blit(walkvirus1[self.walkcount // 20], (self.x, self.y))
self.walkcount += 1
else:
win.blit(walkvirus1[self.walkcount // 20], (self.x, self.y))
self.walkcount += 1
pygame.draw.rect(win, (250, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 65, 10))
pygame.draw.rect(win, (0, 250, 0), (self.hitbox[0], self.hitbox[1] - 20, 65 - ((65/15) * (15 - self.health)), 10))
self.hitbox = (self.x, self.y, self.width, self.height)
#pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
def move(self):
dx, dy = man.x - self.x, man.y - self.y
dist = math.hypot(dx, dy)
dx, dy = dx / dist, dy / dist
self.x += dx * self.vel
self.y += dy * self.vel
def hit(self):
self.walkcount = 0
if self.health > 0:
self.health -= 1
elif self.life > 0:
self.x = random.randint(400, 600)
self.y = random.randint(800, 1000)
self.health = 15
self.life -= 1
else:
self.visible = False
print("hit")
class enemy1(object):
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 = 2
self.hitbox = (self.x, self.y, self.width, self.height)
self.health = 30
self. life = 3
self.visible = True
def draw(self, win):
self.move()
if self.visible:
if self.walkcount + 1 >= 60:
self.walkcount = 0
if self.vel > 0:
win.blit(walkvirus2[self.walkcount // 20], (self.x, self.y))
self.walkcount += 1
else:
win.blit(walkvirus2[self.walkcount // 20], (self.x, self.y))
self.walkcount += 1
pygame.draw.rect(win, (250, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 65, 10))
pygame.draw.rect(win, (0, 250, 0), (self.hitbox[0], self.hitbox[1] - 20, 65 - ((65/30) * (30 - self.health)), 10))
self.hitbox = (self.x, self.y, self.width, self.height)
#pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
def move(self):
dx, dy = man.x - self.x, man.y - self.y
dist = math.hypot(dx, dy)
dx, dy = dx / dist, dy / dist
self.x += dx * self.vel
self.y += dy * self.vel
def hit(self):
self.walkcount = 0
if self.health > 0:
self.health -= 1
elif self.life > 0:
self.x = random.randint(400, 600)
self.y = random.randint(800, 1000)
self.health = 30
self.life -= 1
else:
self.visible = False
print("hit1")
def RedrawGame():
win.blit(bg, (0, 0))
text = font.render("score: " + str(score), 1, (0,0,0))
win.blit(text, (380, 10))
man.draw(win)
virus1.draw(win)
virus2.draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
#Loop
font = pygame.font.SysFont("comicsans",30,True)
man = player(400, 100, 64, 64)
virus1 = enemy(40, 110, 64, 64, 450)
virus2 = enemy1(40, 10, 64, 64, 450)
shoot = 0
bullets = []
run = True
menu = True
while run:
while menu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
menu = False
win.fill((0, 0, 0))
clock.tick(60)
win.blit(background1, (0, 0))
pygame.display.update()
clock.tick(60)
if virus1.visible == True:
if man.hitbox[1] < virus1.hitbox[1] + virus1.hitbox[3] and man.hitbox[1] + man.hitbox[3] > virus1.hitbox[1]:
if man.hitbox[0] + man.hitbox[2] > virus1.hitbox[0] and man.hitbox[0] < virus1.hitbox[0] + virus1.hitbox[2]:
man.hit()
if virus2.visible == True:
if man.hitbox[1] < virus2.hitbox[1] + virus2.hitbox[3] and man.hitbox[1] + man.hitbox[3] > virus2.hitbox[1]:
if man.hitbox[0] + man.hitbox[2] > virus2.hitbox[0] and man.hitbox[0] < virus2.hitbox[0] + virus2.hitbox[2]:
man.hit()
if shoot > 0:
shoot += 1
if shoot > 3:
shoot = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
if virus1.visible == True:
if bullet.y - bullet.radius < virus1.hitbox[1] + virus1.hitbox[3] and virus1.y + bullet.radius > virus1.hitbox[1]:
if bullet.x + bullet.radius > virus1.hitbox[0] and bullet.x - bullet.radius < virus1.hitbox[0] + \
virus1.hitbox[2]:
virus1.hit()
score += 1
bullets.pop(0)
if virus2.visible == True:
if bullet.y - bullet.radius < virus2.hitbox[1] + virus2.hitbox[3] and virus2.y + bullet.radius > virus2.hitbox[1]:
if bullet.x + bullet.radius > virus2.hitbox[0] and bullet.x - bullet.radius < virus2.hitbox[0] + \
virus2.hitbox[2]:
virus2.hit()
score += 1
bullets.pop(0)
if bullet.x < 800 and bullet.x > 0 and bullet.y < 400 and bullet.y > 0:
bullet.move()
else:
bullets.remove(bullet)
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE] and shoot == 0:
hortfacing, vertfacing = 0, 0
if man.left:
hortfacing = -1
elif man.right:
hortfacing = 1
elif man.up:
vertfacing = -1
elif man.down:
vertfacing = 1
if len(bullets) < 5:
bullet = Bullet(round(man.x + man.width // 2),
round(man.y + man.height // 2), 6, (255, 165, 0),
vertfacing, hortfacing)
bullets.append(bullet)
shoot = 1
if keys[pygame.K_a] and man.x >= man.vel:
man.x -= man.vel
man.right = False
man.left = True
man.up = False
man.down = False
man.standing = False
elif keys[pygame.K_d] and man.x < 800 - man.width:
man.x += man.vel
man.right = True
man.left = False
man.up = False
man.down = False
man.standing = False
elif keys[pygame.K_s] and man.y < 400 - man.height:
man.y += man.vel
man.right = False
man.left = False
man.up = False
man.down = True
man.standing = False
elif keys[pygame.K_w] and man.y >= man.vel:
man.y -= man.vel
man.right = False
man.left = False
man.up = True
man.down = False
man.standing = False
else:
man.standing = True
man.walkcount = 0
RedrawGame()
pygame.quit()
Add an if statement so the program will only execute the for loop when there are bullets in the list:
if bullets:
for bullet in bullets:
if virus1.visible == True:
if bullet.y - bullet.radius < virus1.hitbox[1] + virus1.hitbox[3] and virus1.y + bullet.radius > virus1.hitbox[1]:
if bullet.x + bullet.radius > virus1.hitbox[0] and bullet.x - bullet.radius < virus1.hitbox[0] + \
virus1.hitbox[2]:
virus1.hit()
score += 1
bullets.pop(0)

Trying to pop enemy in pygame when health reaches 0

Here is the section I'm trying to get to work, my intent is to have the enemy 'pop' out of the code, currently when the enemy reaches 0 it disappears and no longer hurts the play, but bullets can still hit the enemy at the location it was destroyed:
for lizard in enemy:
if lizard.visible == True:
enemy.pop(enemy.index(lizard))
Here is the error I keep receiving:
TypeError: 'type' object is not iterable
Below you can see the full code:
import pygame
pygame.init()
win = pygame.display.set_mode((600, 580))
pygame.display.set_caption('new nas')
screenWidth = 500
walkRight = [pygame.image.load('R1.png'),
pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
bg = pygame.image.load('sunset.jpg')
char = pygame.image.load('standing.png')
clock = pygame.time.Clock()
bowSound = pygame.mixer.Sound('Bow_release.wav')
arrowSound = pygame.mixer.Sound('Arrow_hit.wav')
music = pygame.mixer.music.load('upbeat.mp3')
pygame.mixer.music.play(-1)
score = 0
#hero
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.jumpCount = 10
self.left = False
self.right = False
self.walkCount = 0
self.standing = True
self.hitbox = (self.x + 17, self.y + 11, 29, 52)
self.health = 10
self.visible = True
def draw(self, win):
if self.visible:
self.health > 1
else:
self.visible == False
if self.walkCount + 1 >= 27:
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)
pygame.draw.rect(win, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 50,10))
pygame.draw.rect(win, (0,100,0), (self.hitbox[0], self.hitbox[1] - 20, 50 - ((50/10) * (10 - self.health)),10))
#pygame.draw.rect(win, (255,0,0), self.hitbox,2)
def hit(self):
self.jumpCount = False
self.jumpCount = 10
self.x = 25
self.y = 480
self.walkCount = 0
font1 = pygame.font.SysFont('forte', 100)
text = font1.render('-5', 1, (255,0,0))
pygame.display.update()
i = 0
while i < 100:
pygame.time.delay(10)
i += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
i = 301
pygame.quit()
# weap
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)
# enemy fodder
class enemy(object):
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.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 > 1:
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,100,0), (self.hitbox[0], self.hitbox[1] - 20, 50 - ((50/10) * (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.path[1] + self.vel:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
else:
if self.x > self.path[0] - self.vel:
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
print('hit')
def redrawGameWindow():
win.blit(bg, (0,0))
text = font.render('Score: ' + str(score), 1, (0,0,0))
win.blit(text, (400, 10))
nas.draw(win)
lizard.draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
#main loop
font = pygame.font.SysFont('forte', 20, True)
nas = player(50, 480, 64, 64)
lizard = enemy(100, 485, 64, 64, 450)
run = True
shootLoop = 0
bullets = []
while run:
clock.tick(27)
if lizard.visible == True:
if nas.hitbox[1] < lizard.hitbox[1] + lizard.hitbox[3] and nas.hitbox[1] + nas.hitbox[3] > lizard.hitbox[1]:
if nas.hitbox[0] + nas.hitbox[2] > lizard.hitbox[0] and nas.hitbox[0] < lizard.hitbox[0] + lizard.hitbox[2]:
nas.hit()
score -= 5
for lizard in enemy:
if lizard.visible == True:
enemy.pop(enemy.index(lizard))
# projectile cool down
if shootLoop > 0:
shootLoop += 1
if shootLoop > 3:
shootLoop = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
if bullet.y - bullet.radius < lizard.hitbox[1] + lizard.hitbox[3] and bullet.y + bullet.radius > lizard.hitbox[1]:
if bullet.x + bullet.radius > lizard.hitbox[0] and bullet.x - bullet.radius < lizard.hitbox[0] + lizard.hitbox[2]:
arrowSound.play()
lizard.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:
bowSound.play()
if nas.left:
facing = -1
else:
facing = 1
if len(bullets) < 3:
bullets.append(projectile(round(nas.x + nas.width//2), round(nas.y + nas.height//2), 6,(250,0,0), facing))
shootLoop = 1
if keys [pygame.K_a] and nas.x > nas.vel:
nas.x -= nas.vel
nas.left = True
nas.right = False
nas.standing = False
elif keys [pygame.K_d] and nas.x < 500 - nas.width - nas.vel:
nas.x += nas.vel
nas.right = True
nas.left = False
nas.standing = False
else:
nas.standing = True
nas.walkCount = 0
if not (nas.isJump):
if keys[pygame.K_w]:
nas.isJump = True
nas.right = False
nas.left = False
nas.walkCount = 0
else:
if nas.jumpCount >= -10:
neg = 1
if nas.jumpCount < 0:
neg = -1
nas.y -= (nas.jumpCount ** 2) * 0.2 * neg
nas.jumpCount -= 1
else:
nas.isJump = False
nas.jumpCount = 10
redrawGameWindow()
pygame.quit()
enemy is a class not a list, so it can't be iterated. By the way, class names should normally use the CapWords convention. See Style Guide for Python Code.
Anyway, add a list of enemies to the application. The list can be traversed and the killed enemies can be removed:
enemies = []
enemies.append(Enemy(100, 485, 64, 64, 450))
# [...]
while run:
# [...]
for lizard in enemies[:]:
if lizard.visible == True:
if nas.hitbox[1] < lizard.hitbox[1] + lizard.hitbox[3] and nas.hitbox[1] + nas.hitbox[3] > lizard.hitbox[1]:
if nas.hitbox[0] + nas.hitbox[2] > lizard.hitbox[0] and nas.hitbox[0] < lizard.hitbox[0] + lizard.hitbox[2]:
nas.hit()
score -= 5
if lizard.visible == False:
enemies.pop(enemies.index(lizard))
I recommend to read about pygame.Rect. Use collidepoint() to simplify the collision test code. e.g:
while run:
# [...]
for lizard in enemies[:]:
if lizard.visible:
lizard_rect = pygame.Rect(*lizard.hitbox)
nas_rect = pygame.Rect(*nas.hitbox)
if nas_rect.colliderect(lizard_rect):
nas.hit()
score -= 5
if not lizard.visible:
enemies.pop(enemies.index(lizard))
Further mote, the way to implement applications like this in pygame, is to use pygame.sprite.Sprite and pygame.sprite.Group
Try moving the enemy off the screen. Something like enemy.move(-100, 100) in pseudocode.

These hit boxes are crossing over even though they are literally not with if statements

I am trying to get my pieces to collide and and when I jump up into the air the characters should not collide.
Here is a video explaining the problem:
https://drive.google.com/file/d/1a26Vmd6TF4C3dJ0DpnFLTR1d3a_lEfn4/view?usp=sharing
The collision is with the goblin.
Furthermore, we can see that the goblin is colliding even though the boxes aren't touching and when on top of the hitbox.
I have tried to change the condition of the loop for example changed the ">" around to "<" and tried making the jump higher. Both of these did not resolve the problem and I am not an experienced coder.
Here is the main problem:
if (adventurer.hitbox_running[0] > attacker.hitbox[0] and adventurer.hitbox_running[0] < attacker.hitbox[0]+70) or (adventurer.hitbox_running[0]+70 > attacker.hitbox[0] and adventurer.hitbox_running[0]+90 < attacker.hitbox[0]+70):
if (adventurer.hitbox_running[1] > attacker.hitbox[1] and adventurer.hitbox_running[1] < attacker.hitbox[1]+70) or (adventurer.hitbox_running[1]+90 > attacker.hitbox[1] and adventurer.hitbox_running[1]+90 < attacker.hitbox[1]+70):
window.fill((255,255,255))
death()
pygame.display.update()
s(0.1)
print("hit")
Here is the entire code with all of the classes and everything:
from time import sleep as s
import random
import pygame
import time
#Loads and plays the music
pygame.mixer.pre_init(44100,16,2,4096)
pygame.init()
pygame.mixer.music.load("Music.mp3")
pygame.mixer.music.set_volume(0.0)
pygame.mixer.music.play(-1)
score = 0
#sets the dimension of the window and loads in the background
window = pygame.display.set_mode((1000, 500))
pygame.display.set_caption("Practice Game")
background = pygame.image.load('pixel6.png')
#Transforms the background to desired dimensions
background = pygame.transform.scale(background, (1000, 500))
#Loads all the jump animation
jumpv1 = [pygame.image.load('adventurer-jump-00.png'),pygame.image.load('adventurer-jump-01.png'),pygame.image.load('adventurer-jump-02.png'),pygame.image.load('adventurer-jump-03.png'), pygame.image.load('adventurer-smrslt-00.png'),pygame.image.load('adventurer-smrslt-01.png'),pygame.image.load('adventurer-smrslt-02.png'),pygame.image.load('adventurer-smrslt-03.png')]
jumpv2 = [pygame.image.load('adventurer-smrslt-00.png'),pygame.image.load('adventurer-smrslt-01.png'),pygame.image.load('adventurer-smrslt-02.png'),pygame.image.load('adventurer-smrslt-03.png')]
jumpv3 = [pygame.image.load('adventurer-smrslt-00.png'),pygame.image.load('adventurer-smrslt-01.png'),pygame.image.load('adventurer-smrslt-02.png'),pygame.image.load('adventurer-smrslt-03.png')]
jumpv4 = [pygame.image.load('adventurer-smrslt-00.png'),pygame.image.load('adventurer-smrslt-01.png'),pygame.image.load('adventurer-smrslt-02.png'),pygame.image.load('adventurer-smrslt-03.png')]
jump = jumpv1+jumpv2+jumpv3+jumpv4
#running animation
run = [pygame.image.load('adventurer-run-00.png'), pygame.image.load('adventurer-run-01.png'),pygame.image.load('adventurer-run-02.png'),pygame.image.load('adventurer-run-03.png')]
#sliding animation
slide = [pygame.image.load('adventurer-slide-00.png'),pygame.image.load('adventurer-slide-01.png'),pygame.image.load('adventurer-stand-00.png'),pygame.image.load('adventurer-stand-01.png'),pygame.image.load('adventurer-stand-02.png')]
#attacking animation
firstattack = [pygame.image.load('adventurer-attack3-00.png'),pygame.image.load('adventurer-attack3-01.png'),pygame.image.load('adventurer-attack3-02.png'),pygame.image.load('adventurer-attack3-03.png'),pygame.image.load('adventurer-attack3-04.png'),pygame.image.load('adventurer-attack3-05.png')]
secondattack = [pygame.image.load('adventurer-attack2-00.png'),pygame.image.load('adventurer-attack2-01.png'),pygame.image.load('adventurer-attack2-02.png'),pygame.image.load('adventurer-attack2-03.png'),pygame.image.load('adventurer-attack2-04.png'),pygame.image.load('adventurer-attack2-05.png')]
thirdattack = [pygame.image.load('adventurer-attack1-00.png'),pygame.image .load('adventurer-attack1-01.png'),pygame.image.load('adventurer-attack1-02.png'),pygame.image.load('adventurer-attack1-03.png'), pygame.image.load('adventurer-attack1-04.png')]
attack = firstattack+secondattack+ thirdattack
#falling animation
falling = [pygame.image.load('adventurer-fall-00.png'), pygame.image.load('adventurer-fall-01.png')]
score = 0
run_program = True
#resizes all the adventuruer images to specified dimensions
for i in range (20):
jump[i] = pygame.transform.scale(jump[i],(90,90))
for i in range(3):
run[i] = pygame.transform.scale(run[i],(90,90))
for i in range (4):
slide[i] = pygame.transform.scale(slide[i],(90,90))
for i in range (1):
falling[i] = pygame.transform.scale(falling[i],(90,90))
for i in range(16):
attack[i] = pygame.transform.scale(attack[i],(90,90))
background_x = 0
background_2 = background.get_width()
clock = pygame.time.Clock()
ghost_image = pygame.image.load('imgg.png')
def text_objects(text, font):
textSurface = font.render(text, True, (0,0,0))
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((1000/2),(500/2))
window.blit(TextSurf, TextRect)
pygame.display.update()
def death():
message_display('You DIED')
class ghost(object):
ghost_animation = [pygame.image.load('imgg.png'),pygame.image.load('imgg.png')]
def __init__(self, x,y,height):
self.x = x
self.y =y
self.height = height
self.hitbox = (x,y,100,100)
self.count = 0
def draw(self, window):
self.hitbox = (self.x, self.y, 45, 60)
if self.count >=1:
self.count = 0
self.count +=1
window.blit(pygame.transform.scale(self.ghost_animation[self.count], (45,60)), (self.x, self.y))
pygame.draw.rect(window, (255,0,0), self.hitbox, 2)
def hit(self):
print ("hit")
class eye(object):
eye_load = pygame.image.load('obstc2.png')
def __init__(self, x,y,width2, height2):
self.x = x
self.y =y
self.hitbox_enemy = (x,y,200,100)
self.count1 = 0
def draw(self, window):
self.hitbox_enemy = (self.x, self.y, 65, 65)
window.blit(pygame.transform.scale(self.eye_load, (65,65)), (self.x, self.y))
pygame.draw.rect(window, (255,0,0), self.hitbox_enemy, 2)
class player(object):
def __init__(self, x,y):
self.vel = 0.5
self.running = True
self.jumping = True
self.sliding = True
self.attacking = True
self.isJump = False
self. jumps = True
self.fall = True
self.player_x = 40
self.player_y = 378
self.x = 40
self.y = 378
self.width = 378
self.speed = 0.6
self.jumpcount = 10
self.jumpingcount = 0
self.runcount = 0
self.attackcount = 0
self.slidecount = 0
self.fallspeed = 0.3
self.fallingcount = 0
self.hitbox_running = (self.player_x,self.player_y,90,90)
self.hitbox_sliding = (45,self.player_y+47,65,45)
self.hitbox_falling = (self.player_x+30,self.player_y,35,80)
self.hitbox_jumping = (self.player_x+20,self.player_y+20,52,55)
self.hitbox_attacking = (58,405,47,70)
self.hitbox_sword = (108, 405, 20, 50)
def movement(self, window):
pygame.time.delay(20)
if self.runcount >= 3:
self.runcount = 0
if self.running == True:
window.blit(run[self.runcount],(int(self.player_x),int(self.player_y)))
self.runcount +=1
self.hitbox_running = (self.player_x+30,self.player_y+20,48,70)
pygame.draw.rect(window,(255,0,0),self.hitbox_running, 2)
if (keys[pygame.K_DOWN]) or ((keys[pygame.K_DOWN]) and keys[pygame.K_p]):
if self.player_y == 378:
self.running = False
if self.slidecount >= 4:
self.slidecount = 0
if self.sliding:
window.blit(slide[self.slidecount],(int(self.player_x),int(self.player_y)))
self.slidecount +=1
pygame.draw.rect(window,(255,0,0),self.hitbox_sliding, 2)
if event.type == pygame.KEYDOWN:
if (event.key == pygame.K_DOWN )and self.player_y < self.width:
self.running = False
self.jumping = False
self.fallspeed += 0.2
if self.fallingcount >= 1:
self.fallingcount = 0
if self.fall:
window.blit(falling[self.fallingcount], (int(self.player_x),int(self.player_y)))
self.hitbox_falling = (self.player_x+30,self.player_y,35,80)
pygame.draw.rect(window,(255,0,0),self.hitbox_falling, 2)
self.fallingcount +=1
if keys[pygame.K_UP] and keys[pygame.K_p] :
self.fallspeed = 0.3
self.running = False
self.jumping = False
self.sliding = False
if self.attackcount >= 16:
self.attackcount = 0
if self.attacking:
window.blit(attack[self.attackcount],(int(self.player_x),int(self.player_y)))
self.attackcount += 1
self.hitbox_attacking = (self.player_x+30,self.player_y+20,38,70)
self.hitbox_sword = (self.player_x+72, self.player_y+20, 20, 50)
pygame.draw.rect(window,(255,0,0),self.hitbox_attacking, 2)
pygame.draw.rect(window,(255,0,0),self.hitbox_sword, 2)
if self.jumpingcount >= 20:
self.jumpingcount = 0
if self.jumping and self.player_y < self.width:
window.blit(jump[self.jumpingcount],(int(self.player_x),int(self.player_y)))
self.hitbox_jumping = (int(self.player_x+20),int(self.player_y+20),52,55)
pygame.draw.rect(window,(255,0,0),self.hitbox_jumping, 2)
self.jumpingcount +=1
self.fallspeed = 0.3
if keys[pygame.K_UP]:
self.fallspeed = 0.3
self.running = False
if self.jumpingcount >= 20:
self.jumpingcount = 0
if self.jumping and self.player_y < self.width:
window.blit(jump[self.jumpingcount],(int(self.player_x),int(self.player_y)))
self.hitbox_jumping = (int(self.player_x+20),int(self.player_y+20),52,55)
pygame.draw.rect(window,(255,0,0),self.hitbox_jumping, 2)
self.jumpingcount +=1
self.fallspeed = 0.3
if keys[pygame.K_p] and not keys[pygame.K_UP]:
self.running = False
self.jumping = False
self.sliding = False
if self.attackcount >= 16:
self.attackcount = 0
if self.attacking:
self.hitbox_attacking = (self.player_x+30,self.player_y+20,38,70)
self.hitbox_sword = (self.player_x+72, self.player_y+20, 20, 50)
window.blit(attack[self.attackcount],(int(self.player_x),int(self.player_y)))
self.attackcount += 1
pygame.draw.rect(window,(255,0,0),self.hitbox_attacking, 2)
pygame.draw.rect(window,(255,0,0),self.hitbox_sword, 2)
if keys[pygame.K_DOWN] and keys[pygame.K_UP]:
self.running = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
self.running = True
self.jumping = True
self.fallspeed = 0.3
if event.key == pygame.K_UP:
self.running=True
if event.key == pygame.K_p:
self.running = True
self.jumping = True
self.sliding = True
class enemy(object):
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')]
for i in range (10):
walkright[i] = pygame.transform.scale(walkright[i],(70,70))
for i in range(10):
walkleft[i] = pygame.transform.scale(walkleft[i],(70,70))
def __init__(self,enemy_x,enemy_y, end):
self.x = enemy_x
self.y = enemy_y
self.end = end
self.path = [self.x,self.end]
self.walkcount = 0
self.vel = 7
self.hitbox = (self.x,self.y-15,90,90)
def draw_enemy(self,window):
self.move()
if self.walkcount+1 >= 33:
self.walkcount = 0
self.vel+=0.2
#walking right
if self.vel > 0:
window.blit(self.walkleft[self.walkcount//3], (self.x,self.y-15))
self.walkcount += 1
#walking left
if self.vel < 0:
## window.blit(self.walkleft[self.walkcount//3], (self.x,self.y))
self.walkcount -= 1
if self.x < -100:
self.x = 1050
self.hitbox = (self.x+25,self.y-15,41,70)
pygame.draw.rect(window, (255,0,0),self.hitbox,2)
def move(self):
if self.vel > 0:
#see if position + movement space is < the end, then it is able to move
if self.x + self.vel < self.path[1]:
self.x -= self.vel
#past end point and turns 180
else:
self.vel = self.vel*-1
self.walkcount = 0
#see if position is smaller than starting position
else:
if self.x - self.vel > self.path[0]:
#vel is already negative
self.x += self.vel
else:
#Truns 180 agian
self.vel = self.vel* -1
self.walkcount = 0
class wizard(object):
walkleft = [pygame.image.load('wizard-fly-forward_04.gif'), pygame.image.load('wizard-fly-forward_04.gif'), pygame.image.load('wizard-fly-forward_04.gif'), pygame.image.load('wizard-fly-forward_04.gif'), pygame.image.load('wizard-fly-forward_04.gif'), pygame.image.load('wizard-fly-forward_05.gif'), pygame.image.load('wizard-fly-forward_04.gif'), pygame.image.load('wizard-fly-forward_04.gif'), pygame.image.load('wizard-fly-forward_05.gif'), pygame.image.load('wizard-fly-forward_04.gif'), pygame.image.load('wizard-fly-forward_04.gif'), pygame.image.load('wizard-fly-forward_04.gif'), pygame.image.load('wizard-fly-forward_05.gif'),pygame.image.load('wizard-fly-forward_04.gif'), pygame.image.load('wizard-fly-forward_04.gif'),pygame.image.load('wizard-fly-forward_05.gif')]
for i in range(16):
walkleft[i] = pygame.transform.scale(walkleft[i],(70,70))
def __init__(self,enemy_x,enemy_y, end):
self.x = enemy_x
self.y = enemy_y
self.end = end
self.path = [self.x,self.end]
self.walkcount = 0
self.vel = 7
self.hitbox = (self.x,self.y-15,90,90)
def draw_enemy(self,window):
self.move()
if self.walkcount >= 16:
self.walkcount = 0
#walking left
if self.vel > 0:
window.blit(self.walkleft[self.walkcount], (self.x,self.y-15))
self.walkcount += 1
self.vel+=0.002
if self.x < -120:
self.x = 1100
self.hitbox = (self.x+25,self.y-15,41,70)
pygame.draw.rect(window, (255,0,0),self.hitbox,2)
def move(self):
if self.vel > 0:
#see if position + movement space is < the end, then it is able to move
if self.x + self.vel < self.path[1]:
self.x -= self.vel
#past end point and turns 180
else:
self.vel = self.vel*-1
self.walkcount = 0
#see if position is smaller than starting position
else:
if self.x - self.vel > self.path[0]:
#vel is already negative
self.x += self.vel
else:
#Truns 180 agian
self.vel = self.vel* -1
self.walkcount = 0
class dragon(object):
walkleft = [pygame.image.load('dragon_1.gif'), pygame.image.load('dragon_2.gif'), pygame.image.load('dragon_3.gif'), pygame.image.load('dragon_4.gif'), pygame.image.load('dragon_5.gif'), pygame.image.load('dragon_6.gif')]
for i in range (6):
walkleft[i] = pygame.transform.scale(walkleft[i],(200,176))
def __init__(self,enemy_x,enemy_y, end):
self.x = enemy_x
self.y = enemy_y
self.end = end
self.path = [self.x,self.end]
self.walkcount = 0
self.vel = 7
self.hitbox = (self.x,self.y-15,90,90)
def draw_enemy(self,window):
self.move()
if self.walkcount >= 6:
self.walkcount = 0
#walking left
if self.vel > 0:
window.blit(self.walkleft[self.walkcount], (self.x,300))
self.walkcount += 1
self.vel+=0.002
if self.x < -120:
self.x = 1100
self.hitbox = (self.x+25,self.y-15,41,70)
pygame.draw.rect(window, (255,0,0),self.hitbox,2)
def move(self):
if self.vel > 0:
#see if position + movement space is < the end, then it is able to move
if self.x + self.vel < self.path[1]:
self.x -= self.vel
#past end point and turns 180
else:
self.vel = self.vel*-1
self.walkcount = 0
#see if position is smaller than starting position
else:
if self.x - self.vel > self.path[0]:
#vel is already negative
self.x += self.vel
else:
#Truns 180 agian
self.vel = self.vel* -1
self.walkcount = 0
ghost_list = []
eye_list = []
def keepdrawing():
window.blit(background, (background_x,0))
window.blit(background, (background_2,0))
for draw_ghost in ghost_list:
draw_ghost.draw(window)
for draw_eye in eye_list:
draw_eye.draw(window)
adventurer.movement(window)
attacker.draw_enemy(window)
wizard.draw_enemy(window)
dragon.draw_enemy(window)
score_text = score_font.render("score: " + str(score), 1, (0,0,0))
window.blit(score_text,(470,10))
pygame.display.update()
score_font = pygame.font.SysFont("Arial",30,True)
attacker = enemy(950,407,951)
adventurer = player(40,387)
wizard = wizard(905,407,951)
dragon = dragon(905,407, 951)
vel_background = 2
pygame.time.set_timer(pygame.USEREVENT+1,random.randint(1000, 2000))
pygame.time.set_timer(pygame.USEREVENT+2,random.randint(2000, 3000))
while run:
while run_program:
clock.tick(60)
background_x -= vel_background
background_2 -= vel_background
if background_x < background.get_width() * -1:
background_x = background.get_width()
if background_2 < background.get_width() * -1:
background_2 = background.get_width()
keys = pygame.key.get_pressed()
if (adventurer.hitbox_running[0] > attacker.hitbox[0] and adventurer.hitbox_running[0] < attacker.hitbox[0]+70) or (adventurer.hitbox_running[0]+70 > attacker.hitbox[0] and adventurer.hitbox_running[0]+90 < attacker.hitbox[0]+70):
if (adventurer.hitbox_running[1] > attacker.hitbox[1] and adventurer.hitbox_running[1] < attacker.hitbox[1]+70) or (adventurer.hitbox_running[1]+90 > attacker.hitbox[1] and adventurer.hitbox_running[1]+90 < attacker.hitbox[1]+70):
window.fill((255,255,255))
death()
pygame.display.update()
s(0.1)
print("hit")
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
if event.type == pygame.USEREVENT+1:
vel_background+=0.2
if event.type == pygame.USEREVENT+2:
r = random.randint(0,1)
if r==0:
new_eye = eye(1050, 390, 64,64)
eye_list.append(new_eye)
if r==1:
new_ghost = ghost(1050,390,64)
ghost_list.append(new_ghost)
if not(adventurer.isJump):
if keys[pygame.K_UP]:
adventurer.isJump =True
else:
if adventurer.jumpcount >= -10:
neg=1
if adventurer.player_y > adventurer.width:
adventurer.player_y = adventurer.width
if adventurer.jumpcount <0:
neg = -1
## s(0.01)
adventurer.player_y -= (adventurer.jumpcount**2)*adventurer.fallspeed*neg*2
## s(0.01)
adventurer.jumpcount -=1
if adventurer.player_y > adventurer.width:
adventurer.player_y=adventurer.width
else:
adventurer.isJump = False
adventurer.jumpcount = 10
if adventurer.player_y > adventurer.width:
player_y = adventurer.width
adventurer.fallspeed = 0.05
keepdrawing()
The expected results should be that the output should not be shit if the hit boxes are not in contact.
The major issue is that you've different "hitboxis, which have different meanings at different states of the game, but you use adventurer.hitbox_running for the collison test event if adventurer is .running or .falling.
The best solution would be the set one .hitbox attribute, which always contains the proper location of adventurer in the method player.movement.
But it is also possible the choose the proper hit box:
if adventurer.running:
hitbox = adventurer.hitbox_running
elif adventurer.jumping:
hitbox = adventurer.hitbox_jumping
elif adventurer.falling:
hitbox = adventurer.hitbox_falling
Further I recommend to use pygame.Rect objects and .colliderect() for the collision test:
rect_adventurer = pygame.Rect(*hitbox)
rect_attacker = pygame.Rect(*attacker.hitbox)
if rect_adventurer.colliderect(rect_attacker):
window.fill((255,255,255))
death()
pygame.display.update()
s(0.1)
print("hit")

Categories