So I am trying to implement some collision detection in a little pygame game I am making. However, my code does not seem to be working right. Or I probably just don't know how to implement it. In this game, I have to get the player
to the princess
without running to goblin
. Everything works right but I cannot seem to implement collision detection between player
and goblin
. Ideally, I would like player
to go back to starting position if it hits a goblin
. You can see I do have a isCollision
function but when I try to call it, I get "NameError: name 'playerX' is not defined"
import pygame
import math
# Initialize the pygame
pygame.init()
# Setting up the screen and background
screen = pygame.display.set_mode((800,600))
# Title and Icon of window
pygame.display.set_caption("Get Princess")
icon = pygame.image.load('knight.png')
pygame.display.set_icon(icon)
#Princess Image
princessImg = pygame.image.load('princess.png')
princessImg = pygame.transform.scale(princessImg, (50,50))
princessX = 360
princessY = 20
princessX_change = 0
princessY_change = 0
class player():
def __init__(self, playerX, playerY, playerX_change, playerY_change):
self.playerX = playerX
self.playerY = playerY
self.playerX_change = playerX_change
self.playerY_change = playerY
self.playerImg = pygame.image.load('knight.png')
self.playerImg = pygame.transform.scale(self.playerImg, (50,50))
self.rect = pygame.Rect(32,32,16,16)
def pdraw(self):
screen.blit(self.playerImg, (self.playerX, self.playerY))
def pmovement(self):
self.playerX += self.playerX_change
self.playerY += self.playerY_change
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.playerY_change = -0.4
if event.key == pygame.K_DOWN:
self.playerY_change = 0.4
if event.key == pygame.K_LEFT:
self.playerX_change = -0.4
if event.key == pygame.K_RIGHT:
self.playerX_change = 0.4
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
self.playerX_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
self.playerY_change = 0
if self.playerX <= 0:
self.playerX = 0
elif self.playerX >= 750:
self.playerX = 750
if self.playerY <= 0:
self.playerY = 0
elif self.playerY >= 550:
self.playerY = 550
class goblin():
def __init__(self, goblinX, goblinY, goblinX_change):
self.goblinX = goblinX
self.goblinY = goblinY
self.goblinX_change = goblinX_change
self.goblinImg = pygame.image.load('goblin.png')
self.goblinImg = pygame.transform.scale(self.goblinImg,(50,50))
def draw(self):
screen.blit(self.goblinImg, (self.goblinX, self.goblinY))
def movement(self):
self.goblinX += self.goblinX_change
if self.goblinX <= 0 or self.goblinX >= 750:
self.goblinX_change = self.goblinX_change * -1
def princess(x,y):
screen.blit(princessImg, (x, y))
p = player(360, 520, 0, 0)
g = goblin(360,250, 0.10)
g1 = goblin(360, 280, 0.5)
g2 = goblin(360, 200, 0.7)
g3 = goblin(360, 160, 0.4)
goblinlist = [g, g1, g2, g3]
def isCollision(playerX, playerY, goblinX, goblinY):
dx = playerX - goblinY
dy = playerY - goblinY
distance = math.sqrt(math.pow(dx,2) + math.pow(dy,2))
if distance < 27:
p = player(360, 520, 0,0)
running = True
while running:
screen.fill((50,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
princess(princessX, princessY)
g.movement()
g.draw()
g1.movement()
g1.draw()
g2.movement()
g2.draw()
g3.movement()
g3.draw()
p.pmovement()
p.pdraw()
isCollision(p.playerX, p.playerY, g1.goblinX, g1.goblinY)
pygame.display.update()
isCollision
is a function and playerX
, playerY
, goblinX
, goblinY
are the arguments of the function. You don't have a self
argument. Usually self
is the object in Methods.
Remove all the self.
form the function isCollision
:
def isCollision(playerX, playerY, goblinX, goblinY):
dx = playerX - goblinX
dy = playerY - goblinY
distance = math.sqrt(math.pow(dx, 2) + math.pow(dy, 2))
return distance < 27
Pass the coordinates of the player (p
) and a goblin (g
, g1
, g2
or g3
) to the function:
For instance:
if isCollision(p.playerX, p.playerY, g.goblinX, g.goblinY):
# do something
# [...]
Note, you can create a list of goblins and do the collision test in a loop:
goblin_list = [g, g1, g2, g3]
for gob in goblin_list:
if isCollision(p.playerX, p.playerY, gob.goblinX, gob.goblinY):
# [...]
You can even move and draw the goblins in a loop:
for gob in goblin_list:
gob.movement()
gob.draw()
You need to use the global
statement, if you want to change a variable in global namespace:
def isCollision(playerX, playerY, goblinX, goblinY):
global p
dx = playerX - goblinX
dy = playerY - goblinY
distance = math.sqrt(math.pow(dx, 2) + math.pow(dy,2))
if distance < 27:
p = player(360, 520, 0,0)