python error 'global name 'player' is not defined' - python

ive been learning python on my own for the past cople months and ive started making games about a weak ago (with pygame) and i have absolutly no idea what i did wrong
this is my code (btw im making pong):
import pygame
pygame.init()
display_width = 800
display_height = 600
black = (0, 0, 0)
white = (255, 255, 255)
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('pong')
clock = pygame.time.Clock()
FPS = 60
class Player():
def __init__(self):
self.padWid = 8
self.padHei = 64
self.x = display_width - 16
self.y = (display_height/2) - (self.padHei/2)
self.speed = 3
self.score = 0
self.scoreFont = pygame.font.Font('freesansbold.ttf', 40)
def scoring(self):
self.text = self.scoreFont.render(str(self.score),True,white)
gameDisplay.blit(self.text, [(display_width * 0.75),30])
def movement(self):
key = pygame.key.get_pressed()
if key[pygame.K_UP]:
self.y -= self.speed
elif key[pygame.K_DOWN]:
self.y += self.speed
if self.y <= 0:
self.y = 0
elif self.y >= display_height - self.padHei:
self.y = display_height - self.padHei
def draw(self):
pygame.draw.rect(gameDisplay, white, [self.x, self.y, self.padWid, self.padHei])
class Enemy():
def __init__(self):
self.padWid = 8
self.padHei = 64
self.x = 16
self.y = (display_height/2) - (self.padHei/2)
self.speed = 3
self.score = 0
self.scoreFont = pygame.font.Font('freesansbold.ttf', 40)
def scoring(self):
self.text = self.scoreFont.render(str(self.score),True,white)
gameDisplay.blit(self.text, [(display_width * 0.23),30])
def movement(self):
key = pygame.key.get_pressed()
if key[pygame.K_w]:
self.y -= self.speed
elif key[pygame.K_s]:
self.y += self.speed
if self.y <= 0:
self.y = 0
elif self.y >= display_height - self.padHei:
self.y = display_height - self.padHei
def draw(self):
pygame.draw.rect(gameDisplay, white, [self.x, self.y, self.padWid, self.padHei])
class Ball():
def __init__(self):
self.x = display_width/2
self.y = display_height/2
self.radius = 4
self.x_speed = -3
self.y_speed = 3
def movement(self):
self.x += self.x_speed
self.y += self.y_speed
if self.y <= self.radius or self.y >= display_height - self.radius:
self.y *= -1
if self.x + self.radius >= display_width:
self.__init__()
enemy.score += 1
elif self.x - self.radius <= 0:
self.__init__()
player.score += 1
for n in range(-self.radius, [player.padHei + self.radius]):
if self.y == player.y + n:
if self.x + self.radius >= player.x:
self.x_speed *= -1
break
n += 1
for n in range(-self.radius, [enemy.padHei + self.radius]):
if self.y == player.y + n:
if self.x - self.radius <= enemy.x + enemy.w:
self.x_speed *= -1
break
n += 1
def draw(self):
pygame.draw.circle(gameDisplay, white, [self.x, self.y], self.radius)
def game_loop():
running = True
player = Player()
enemy = Enemy()
ball = Ball()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
player.movement()
enemy.movement()
ball.movement()
player.draw()
enemy.draw()
ball.draw()
player.scoring()
enemy.scoring()
pygame.display.update()
game_loop()
I always get the following error:
Traceback (most recent call last):
File "C:\programs\python pong\pong.py", line 159, in <module>
game_loop()
File "C:\programs\python pong\pong.py", line 148, in game_loop
ball.movement()
File "C:\programs\python pong\pong.py", line 112, in movement
for n in range(-self.radius, [player.padHei + self.radius]):
NameError: global name 'player' is not defined

You create playerin the function game_loop(), but this variable is local, so you are not able to see it outside that function.
Pass the player as an argument in the ball.movement() function.
In this way:
def movement(self, player):
self.x += self.x_speed
self.y += self.y_speed
...
...
ball.movement(player)

Related

pygame detecting if drawn circle sprite is clicked failing

I am working on a small game where if you click a circle it makes a pop sound and goes back to the bottom my problem is when I switched to using a class and sprite to allow multiple circles it no longer will detect if one is clicked what am I doing wrong?
class Bubble(pygame.sprite.Sprite):
def __init__(self):
self.radius = random.randint(50,100)
self.image = pygame.Surface([self.radius/2,self.radius/2])
self.image.fill(blue)
self.image = pygame.image.load(bPath+"Images\\Bubble.png")
self.rect = self.image.get_rect()
self.bx = random.randint(70, 1466)
self.x = self.bx
self.y = 930
self.way = 0
self.rect.x = self.x
self.rect.y = self.y
def move(self):
pygame.draw.circle(tv,blue,(self.x,self.y),self.radius)
self.y -= 2
if self.x == self.bx+20:
self.way = 1
elif self.x == self.bx-20:
self.way = 0
else:
pass
if self.way == 0:
self.x += 1
else:
self.x -= 1
if self.y <= 0:
self.bx = random.randint(70, 1466)
self.x = self.bx
self.y = 930
self.radius=random.randint(50,100)
else:
pass
bubbleList = []
nBubble = 0
while True:
tv.fill(white)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
for b in bubbleList:
if b.rect.collidepoint(pygame.mouse.get_pos()):
print("hi")
pygame.mixer.music.play()
if pygame.time.get_ticks() > nBubble:
nBubble += 5000
if len(bubbleList) < 10:
bubbleList.append(Bubble())
else:
pass
for b in bubbleList:
b.move()
pygame.display.update()
FramePerSec.tick(fps)
I have looked at a couple other similar posts on here and have implemented them as you can probably see. It throws no errors on my computer it just does nothing when clicked.
What are self.x and self.y for? You don't need these attributes. You have self.rect.x and self.rect.y. The collision detection depends on the position stored in the rect attribute.
if b.rect.collidepoint(pygame.mouse.get_pos()):
However, self.rect.x and self.rect.y do not magically change when you change self.x and self.y. The position stored in the rect attribute is not tied to self.x and self.y. Get rid of self.x and self.y:
class Bubble(pygame.sprite.Sprite):
def __init__(self):
self.radius = random.randint(50,100)
self.image = pygame.Surface([self.radius/2,self.radius/2])
self.image.fill(blue)
self.image = pygame.image.load(bPath+"Images\\Bubble.png")
self.rect = self.image.get_rect()
self.bx = random.randint(70, 1466)
self.way = 0
self.rect.x = self.bx
self.rect.y = 930
def move(self):
pygame.draw.circle(tv, blue, self.rect.center, self.radius)
self.rect.y -= 2
if self.rect.x == self.bx+20:
self.way = 1
elif self.rect.x == self.bx-20:
self.way = 0
else:
pass
if self.way == 0:
self.rect.x += 1
else:
self.rect.x -= 1
if self.rect.y <= 0:
self.bx = random.randint(70, 1466)
self.rect.x = self.bx
self.rect.y = 930
self.radius=random.randint(50,100)
else:
pass
See also How do I detect collision in pygame?.

Pygame is a bit laggy. Is it because of the many calculations?

import pygame
import math
import random
import time
pygame.init()
clock = pygame.time.Clock()
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Shadow Warrior")
#its like that because this reduses lag a lot and help for better gameplay
def loadify(imgname):
return pygame.image.load(imgname).convert_alpha(screen)
#Models:
logo = loadify("textures/logo.png")
pygame.display.set_icon(logo)
wallModel = loadify("textures/wall.png")
groundModel = loadify("textures/ground.png")
weaponModel = loadify("textures/weapon.png")
enemyBullet = loadify("textures/bullet.png")
playerBullet = loadify("textures/enemyBullet.png")
crosshair = loadify("textures/crosshair.png")
playerWalkAnimR = [loadify("textures\walk0.png"),
loadify("textures\walk1.png"),
loadify("textures\walk2.png")]
playerWalkAnimL = [loadify("textures\walk3.png"),
loadify("textures\walk4.png"),
loadify("textures\walk5.png")]
pygame.mouse.set_visible(False)
tileSize = 64
level = 0
hitParticles = []
weapons = []
removed_bullets = []
enemies = []
backgrounds = []
walls = []
all_bullets = []
# Classes
class Particle:
def __init__(self, x, y, velocityX, velocityY, radius, color):
self.x = x
self.y = y
self.velocityX = velocityX
self.velocityY = velocityY
self.radius = radius
self.color = color
self.lifetime = random.randrange(50, 100)
def draw(self, screen, camx, camy):
self.lifetime -= 1
self.x += self.velocityX
self.y += self.velocityY
pygame.draw.circle(screen, self.color, (self.x - camx, self.y - camy), self.radius)
class Background:
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self, camx, camy):
screen.blit(groundModel, (self.x - camx - tileSize / 2, self.y - camy - tileSize / 2))
class Wall:
def __init__(self, x, y):
self.x = x
self.y = y
#Check if wall collides with smth
def collidesWith(self, other):
return pygame.Rect(self.x, self.y, tileSize, tileSize).colliderect(other.x, other.y, tileSize-16, tileSize)
def draw(self, camx, camy):
screen.blit(wallModel, (self.x - camx - tileSize / 2, self.y - camy - tileSize / 2))
class Bullet:
def __init__(self, x, y, velx, vely, isPlayer):
self.x = x
self.y = y
self.velx = velx
self.vely = vely
self.isPlayer = isPlayer
def collidesWith(self, other):
return pygame.Rect(self.x, self.y, tileSize - 16, tileSize - 16).colliderect(other.x, other.y, tileSize-36, tileSize-36)
def update(self):
self.x += self.velx
self.y += self.vely
for bullet in all_bullets:
for wall in walls:
if bullet.collidesWith(wall):
removed_bullets.append(bullet)
for i in removed_bullets:
if i in all_bullets:
all_bullets.remove(i)
def draw(self, camx, camy):
if (self.isPlayer):
screen.blit(pygame.transform.smoothscale(playerBullet, (16, 16)), (self.x - camx + 5, self.y - camy + 7))
else: screen.blit(pygame.transform.smoothscale(enemyBullet, (16, 16)), (self.x - camx + 5, self.y - camy + 7))
class slimeEnemy:
def __init__(self, x, y):
self.x = x
self.y = y
self.bulletSpeed = 1
self.aliveAnimations = [loadify("textures\slime_animation_0.png"),
loadify("textures\slime_animation_1.png"),
loadify("textures\slime_animation_2.png"),
loadify("textures\slime_animation_3.png"),]
self.animationsCount = 0
self.attackRate = 60
self.resetOffset = 0
self.SlimeHp = 7 + level * 2
self.slimeCollisionDmg = 1 + level/2
self.dmg = 1 + level / 2
self.isAlive = True
# self.isBoss = isBoss
#adding offset so the enemy doesnt move directly towards the player
self.offsetX = random.randrange(-150, 150)
self.offsetY = random.randrange(-150, 150)
def collidesWithAnything(self):
for wall in walls:
if wall.collidesWith(self):
return True
return False
def healthBar(self):
pygame.draw.rect(screen, (0, 255, 0), (self.x - camx - 20, self.y + 40 - camy, self.SlimeHp*10 + level*2, 7))
def collidesWith(self, other):
return pygame.Rect(self.x, self.y, tileSize-36, tileSize-36).colliderect(other.x, other.y, tileSize-36, tileSize-36)
def update(self):
# move enemy accordingly
if self.resetOffset == 0:
self.offsetX = random.randrange(-400, 400)
self.offsetY = random.randrange(-400, 400)
self.resetOffset = random.randrange(120, 150)
else: self.resetOffset -= 1
if player.x + self.offsetX > self.x:
self.x += 1
if self.collidesWithAnything():
self.x -= 1
elif player.x + self.offsetX < self.x:
self.x -= 1
if self.collidesWithAnything():
self.x += 1
if player.y + self.offsetY > self.y:
self.y += 1
if self.collidesWithAnything():
self.y -= 1
elif player.y + self.offsetY < self.y:
self.y -= 1
if self.collidesWithAnything():
self.y += 1
self.healthBar()
def attack(self):
for i in range(3):
angle = random.randrange(0, 360)
bulletSpeed_x = self.bulletSpeed * math.cos(angle) + random.uniform(-5, 5)
bulletSpeed_y = self.bulletSpeed * math.sin(angle) + random.uniform(-5, 5)
all_bullets.append(Bullet(self.x, self.y, bulletSpeed_x, bulletSpeed_y, False))
def drawAlive(self, camx, camy):
if self.animationsCount + 1 == 32:
self.animationsCount = 0
self.animationsCount += 1
if self.attackRate == 0:
self.attackRate = 60
self.attack()
self.attackRate -= 1
screen.blit(pygame.transform.scale(self.aliveAnimations[self.animationsCount// 8], (32, 30)), (self.x - camx, self.y - camy))
class Weapon:
def __init__(self, bulletSpeed, fireRate, bulletDmg):
self.shooting = False
self.bulletSpeed = bulletSpeed
self.fireRate = fireRate
self.bulletDmg = bulletDmg
self.energy = 100
self.i = 0
def handle_weapons(self, screen):
mouse_x, mouse_y = pygame.mouse.get_pos()
rel_x, rel_y = mouse_x - width/2, mouse_y - height/2
angle = (180/math.pi) * - math.atan2(rel_y, rel_x)
#draw crosshair
screen.blit(crosshair, (mouse_x, mouse_y+5))
#rotate and draw weapon accordingly
if angle > 90 or angle < -90:
player_weapon_copy = pygame.transform.rotate(pygame.transform.flip(weaponModel, True, False), angle - 180)
else:
player_weapon_copy = pygame.transform.rotate(weaponModel, angle)
screen.blit(player_weapon_copy, (width/2 +5 - int(player_weapon_copy.get_width()/2), height/2 + 15 - int(player_weapon_copy.get_height()/2)))
def Shooting(self):
if self.shooting:
#calculates the angles the bullet should travel :/
if self.i % self.fireRate == 0:
if (self.energy == 0):
pass
else:
mouse_x, mouse_y = pygame.mouse.get_pos()
distance_x = mouse_x - width/2
distance_y = mouse_y - height/2
angle = math.atan2(distance_y, distance_x)
bulletSpeed_x = self.bulletSpeed * math.cos(angle)
bulletSpeed_y = self.bulletSpeed * math.sin(angle)
all_bullets.append(Bullet(player.x, player.y, bulletSpeed_x, bulletSpeed_y, True))
self.energy -= 1
self.i += 1
class Player:
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
self.leftPr = False
self.rightPr = False
self.downPr = False
self.upPr = False
self.speed = 4
self.health = 5
self.maxHp = 5
self.animationsCount = 0
def healthBar(self):
pygame.draw.rect(screen, (255, 0, 0), (20, 25, 200, 10))
pygame.draw.rect(screen, (0, 255, 0), (20, 25, self.health*40, 10))
def energyBar(self):
pygame.draw.rect(screen, (255, 0, 0), (20, 55, 200, 10))
pygame.draw.rect(screen, (48, 117, 255), (20, 55, weapon.energy*2, 10))
def collidesWithAnything(self):
for wall in walls:
if wall.collidesWith(self):
return True
return False
def update(self):
if self.leftPr and not self.rightPr:
self.x -= self.speed
if self.collidesWithAnything():
self.x += self.speed
if self.rightPr and not self.leftPr:
self.x += self.speed
if self.collidesWithAnything():
self.x -= self.speed
if self.upPr and not self.downPr:
self.y -= self.speed
if self.collidesWithAnything():
self.y += self.speed
# revert if collision
if self.downPr and not self.upPr:
self.y += self.speed
if self.collidesWithAnything():
self.y -= self.speed
# check for enemy colisions
def animations(self):
if self.animationsCount + 1 >= 36:
self.animationsCount = 0
if self.rightPr:
screen.blit(pygame.transform.scale(playerWalkAnimR[self.animationsCount//12],(64, 64)), (width/2 - 40, height/2 - 30))
elif self.leftPr:
screen.blit(pygame.transform.scale(playerWalkAnimL[self.animationsCount//12],(64, 64)), (width/2 - 40, height/2 - 30))
else:
screen.blit(pygame.transform.scale(playerWalkAnimR[0],(64, 64)), (width/2 - 40, height/2 - 30))
self.animationsCount += 1
weapon = Weapon(10, 20, 2)
player = Player(1, 0)
#Loads map from file
def loadMapFromFile(path):
walls.clear()
backgrounds.clear()
enemies.clear()
with open(path, "r") as f:
y = 0
enemyLocations = []
# bossLocations = []
for line in f.readlines():
x = 0
for char in line:
if char != ' ' and char != "\n":
backgrounds.append(Background(x * tileSize, y * tileSize))
if char == '#':
walls.append(Wall(x * tileSize, y * tileSize))
elif char == 'p' or char == 'P':
player.x = x * tileSize
player.y = y * tileSize
elif char == 'e' or char == 'E':
if random.randint(1, 100) <= 40:
enemyLocations.append((x * tileSize, y * tileSize))
# elif char == 'b' or char == 'B':
# if random.randint(1, 100) <= 10:
# bossLocations.append((x * tileSize, y * tileSize, "True"))
x += 1
y += 1
for enemyL in enemyLocations:
enemies.append(slimeEnemy(*enemyL))
# for enemyL in bossLocations:
# enemies.append(slimeEnemy(*enemyL, "True"))
def gameOverScreen():
pygame.font.init()
font = pygame.font.Font('arial.ttf', 32)
text = font.render('GAME OVER', True, (255, 0, 0), (0, 0, 0))
text.set_colorkey((0, 0, 0))
screen.blit(text, (width/2-80,height/2-40))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
loadMapFromFile('maps/Map0.txt')
hitCooldown = 75
cooldown = 600
#running loop
gameOver = False
running = True
while running:
if gameOver:
gameOverScreen()
pass
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.upPr = True
if event.key == pygame.K_s:
player.downPr = True
if event.key == pygame.K_d:
player.rightPr = True
if event.key == pygame.K_a:
player.leftPr = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
player.upPr = False
if event.key == pygame.K_s:
player.downPr = False
if event.key == pygame.K_d:
player.rightPr = False
if event.key == pygame.K_a:
player.leftPr = False
if event.type == pygame.MOUSEBUTTONDOWN:
weapon.shooting = True
if event.type == pygame.MOUSEBUTTONUP:
weapon.shooting = False
# updating
camx = player.x - width / 2
camy = player.y - height / 2
screen.fill((0, 0, 0))
player.update()
#check if slime got hit
for bullet in all_bullets:
bullet.update()
for enemy in enemies:
if enemy.collidesWith(player):
if hitCooldown < 0:
hitCooldown = 75
player.health -= enemy.slimeCollisionDmg
if player.health <= 0:
gameOver = True
elif enemy.collidesWith(bullet) and bullet.isPlayer:
removed_bullets.append(bullet)
for i in range(10):
hitParticles.append(Particle(enemy.x, enemy.y, random.randrange(-5, 5)/10, random.randrange(-5, 5)/10, 4, (108, 216, 32)))
enemy.SlimeHp -= weapon.bulletDmg
if enemy.SlimeHp <= 0:
enemy.isAlive = False
if player.health >= player.maxHp:
player.health = player.maxHp
else:
player.health += 1
if weapon.energy >= 100:
weapon.energy = 100
else:
weapon.energy += 2
if bullet.collidesWith(player) and not bullet.isPlayer:
if hitCooldown < 0:
hitCooldown = 75
player.health -= enemy.dmg
if player.health <= 0:
gameOver = True
removed_bullets.append(bullet)
for bg in backgrounds:
bg.draw(camx, camy)
for wall in walls:
wall.draw(camx, camy)
# update all other entities
#draw bullet
for bullet in all_bullets:
bullet.draw(camx, camy)
# slime animations and remove dead slimes
for enemy in enemies:
enemy.update()
if enemy.isAlive:
enemy.drawAlive(camx, camy)
if not enemy.isAlive:
enemies.remove(enemy)
# draw particles
for particle in hitParticles:
if particle.lifetime > 0:
particle.draw(screen, camx, camy)
else:
hitParticles.pop(hitParticles.index(particle))
player.animations()
weapon.handle_weapons(screen)
weapon.Shooting()
player.healthBar()
player.energyBar()
if len(enemies) == 0:
if cooldown <= 0:
cooldown = 600
loadMapFromFile("maps\Map{0}.txt".format(level+1))
level+=1
hitCooldown -= 1
cooldown -= 1
# finally update screen
pygame.display.update()
clock.tick(60)
I know it is written really badly but I'm learning. The game is really laggy and I was wondering how I can make it run faster and smoother. Also is the lag created from the many calculations or am I just dumb? The lag comes when there are a lot of enemies on the map and they shoot at the same time. Can I fix the lag by making all the enemies shoot in different times? The game is for a school project.
I think your biggest issue is this:
class Bullet:
[ ... ]
def update(self):
self.x += self.velx
self.y += self.vely
for bullet in all_bullets: # <-- HERE
for wall in walls:
if bullet.collidesWith(wall):
removed_bullets.append(bullet)
for i in removed_bullets:
if i in all_bullets:
all_bullets.remove(i)
Then in the main loop:
#check if slime got hit
for bullet in all_bullets: # <-- AGAIN
bullet.update()
The code is iterating through the bullets many more times than is necessary. By the time all your bullet objects are updated, they have been processed /N/ * /N/ times (i.e.: N-squared). So as you get more any more bullets, this square grows catastrophically.
Try changing your Bullet.update() to only check itself. This will probably fix your lag issues - or at least help a lot. It will also "correct" your object encapsulation. A single Bullet has no business knowing about all the others, let alone checking collisions on their behalf.
class Bullet:
[ ... ]
def update(self):
self.x += self.velx
self.y += self.vely
for wall in walls:
if self.collidesWith(wall):
all_bullets.remove( self )
break
If you have any more N-squared loops like this, it will give the best speedup reworking them first.

collisions in python pygame between a randomly spawned Object an player by hitboxes

I code for the first time with pygame and no one can help me ecause all may friends code wit javascript
I wrote those lines of Code and I want to do something when Food and player collide. This is my code and I want to do it the easiest way:
import pygame
import os
import random
pygame.init()
win_height = 400
win_width = 800
win = pygame.display.set_mode((win_width, win_height))
class Hero:
def __init__(self, x, y):
# Walk
self.x = x
self.y = y
self.velx = 10
self.vely = 6
self.face_right = True
self.face_left = False
self.stepIndex = 0
self.hitboxPlayer = pygame.Rect(self.x, self.y, 50, 112.5)
# Jump
self.jump = False
def draw(self, win):
self.hitboxPlayer = pygame.Rect(self.x, self.y, 50, 112.5)
pygame.draw.rect(win, (0, 0, 0), self.hitboxPlayer, 1)
if self.stepIndex >= 3:
self.stepIndex = 0
if self.face_left:
win.blit(left[self.stepIndex], (self.x, self.y))
self.stepIndex += 1
if self.face_right:
win.blit(right[self.stepIndex], (self.x, self.y))
self.stepIndex += 1
def jump_motion(self, userInput):
if userInput[pygame.K_SPACE] and self.jump is False:
self.jump = True
if self.jump:
self.y -= self.vely * 4
self.vely -= 1
if self.vely < -6:
self.jump = False
self.vely = 6
def direction(self):
if self.face_right:
return 1
if self.face_left:
return -1
class Food:
def __init__(self, lx, hx):
self.y = 0
self.speed = 5
self.highestx = win_width
self.x = random.randint(0,self.highestx)
self.sprite = str(1)
self.FoodCount = []
self.hitboxFood = pygame.Rect(self.x, self.y, 50, 50)
def draw(self):
self.hitboxFood = pygame.Rect(self.x + 15, self.y + 15, 50, 50)
pygame.draw.rect(win, (0, 0, 0), self.hitboxFood, 1)
win.blit(food, (self.x,self.y))
self.sprite = win.blit(food, (self.x,self.y))
if self.y == win_height-200:
print("Ground ")
if snacks.hitboxFood.colliderect(player.hitboxPlayer) :
print("hit")
def move(self):
self.y += self.speed
def draw_game():
global tower_health, speed
win.fill((0, 0, 0))
win.blit(background, (0, 0))
player.draw(win)
for snack in snacks.FoodCount:
snack.draw()
snack.move()
pygame.time.delay(30)
pygame.display.update()
pygame.time.set_timer(pygame.USEREVENT, 1000)
snacks = Food(0, win_width)
player = Hero(250, 290)
run = True
while run:
if event.type == pygame.USEREVENT:
snacks.spawn()
userInput = pygame.key.get_pressed()
player.move_hero(userInput)
player.jump_motion(userInput)
draw_game()
and I really don't know how to do this... I tried so much
tanks for helping!!!!!!!!!!!!!
have you tried searching on Google pygame detect collision. I did and this is what I found, I hope this does help you.
Spending time searching on Google is the first thing you should think of when learning to code or using a new module.

Enemy move is wrong why?

I want to create a spaceship game to using pygame. I want to enemies movement like this:
first enemies moving x coordination and enemy x coordinate increasing on x (moving right)
if enemy coordinates x >= WIDHT , enemy coordinates y += 5 and enemy x coordinate is decreasing on x (moving left)
if enemy coordinates x <= 0 ,enemy coordinates y -= 5 and enemy x coordinate is increasing on x (moving right)
I want to move enemies like this, but I typed code and enemies not moving as I want:
import pygame
import random
import time
WIDTH , HEIGHT = (750, 600)
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
BG_IMAGE = pygame.transform.scale(pygame.image.load("yeni_resim.jpg"),(WIDTH, HEIGHT))
CAPTION_IMAGE = pygame.image.load("spaceship.png")
ENEMY_IMAGE = pygame.image.load("enemy.png")
BULLET_IMAGE = pygame.image.load("bullet.png")
PLAYER_IMAGE = pygame.image.load("warship.png")
Creating Bullet Class
class Bullet:
def __init__(self, x, y, img):
self.x = x
self.y = y
self.img = img
self.mask = pygame.mask.from_surface(self.img)
def draw(self, window):
window.blit(self.img, (self.x, self.y))
def move(self, vel):
self.y += vel
def off_screen(self, height):
return not (self.y <= height and self.y >=0)
def collision(self, obj):
pass
And creating ship class(for warship and enemy)
class Ship:
def __init__(self, x, y):
self.x = x
self.y = y
self.ship_img = None
def draw(self, window):
window.blit(self.ship_img, (self.x, self.y))
def get_width(self):
return self.ship_img.get_width()
def get_height(self):
return self.ship_img.get_height()
class Player(Ship):
def __init__(self, x, y):
super().__init__(x, y)
self.ship_img = PLAYER_IMAGE
self.mask = pygame.mask.from_surface(self.ship_img)
def draw(self, window):
super().draw(window)
This is enemy class and i create def for enemy movement:
class Enemy(Ship):
def __init__(self, x, y):
super().__init__(x,y)
self.ship_img = ENEMY_IMAGE
self.mask = pygame.mask.from_surface(self.ship_img)
def move(self, vel):
self.x += vel
if self.x >= 684:
self.x -= vel
self.y += 5
elif self.x <= 0:
self.x += vel
self.y += 5
def main():
run = True
FPS = 60
clock = pygame.time.Clock()
player = Player(350, 500)
player_vel = 8
enemy = Enemy(350, 100)
enemy_vel = 5
def redraw_window():
WIN.blit(BG_IMAGE,(0,0))
player.draw(WIN)
enemy.draw(WIN)
pygame.display.update()
while run:
clock.tick(FPS)
redraw_window()
enemy.move(enemy_vel)
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT] and player.x + player_vel + player.get_width() < WIDTH:
player.x += player_vel
if keys[pygame.K_LEFT] and player.x - player_vel > 0:
player.x -= player_vel
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
main()
Try removing your move function and replacing it with this in the while loop:
enemy.x += enemy_vel
if enemy.x >= WIDTH - ENEMY_IMAGE.get_width() or enemy.x <= 0:
enemy_vel *= -1
enemy_y += 5

Collision Detection Not Working - PyGame

I'm working in a game, and in this game an object falls from the top of the game screen and the player at the bottom of the screen has to hit the object falling. When the player hits the falling object, the player's width and height needs to increase. When I tested the code, the collision worked when the player hit the falling object from the side, but the collision didn't work when the player hit the falling object in the middle. Can someone help me?
PYTHON
# IMPORTS
import pygame, random
# GLOBALS
global screen, displayW, displayH
global clock, FPS
global end, food, player
# SETGLOBALVALUES
def setGlobalValues():
global screen, displayW, displayH
global clock, FPS
global end, food, player
displayW = 800
displayH = 600
screen = pygame.display.set_mode((displayW, displayH))
clock = pygame.time.Clock()
FPS = 60
end = False
food = Food()
player = Player()
# MAIN
def main():
pygame.init()
setGlobalValues()
setup()
gameLoop()
quitGame()
# GAMELOOP
def gameLoop():
global end, player
while not end:
for event in pygame.event.get():
# ONCLICK QUIT
if event.type == pygame.QUIT:
end = True;
# KEYDOWN
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.velX -= 10
if event.key == pygame.K_RIGHT:
player.velX += 10
# KEYUP
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.velX = 0
if event.key == pygame.K_RIGHT:
player.velX = 0
draw()
animate()
collision()
setFPS()
# DRAW
def draw():
global screen, food, player
# fill background
screen.fill((255, 255, 255))
player.draw()
food.draw()
# update
pygame.display.update()
# ANIMATE
def animate():
global food, player
food.animate()
player.animate()
# COLLISION
def collision():
global player, food;
player.collision()
food.collision();
# SETFPS
def setFPS():
global clock, FPS
clock.tick(FPS);
# CLASSES
class Food():
def __init__(self, img="", x=0, h=0, w=0, velY=0, color=()):
global displayW
self.img = pygame.image.load("assets/img/rsz_burger.png")
self.w = 30
self.h = 30
self.x = random.randrange(0, displayW - self.w)
self.y = -100
self.velY = 3
self.color = (255, 0, 0)
def draw(self):
global screen
screen.blit(self.img, (self.x, self.y))
def animate(self):
self.y += self.velY
if self.y >= displayW:
self.reset()
def collision(self):
global displayW, displayH
# collision with player
if self.y >= player.y and self.y <= player.y + player.h and self.x >= player.x and self.x <= player.x + player.w:
player.w += player.increase
player.h += player.increase
player.y - player.increase
print(player.w)
self.reset()
def reset(self):
self.y = -100
self.x = random.randrange(0, displayW - self.w)
self.velY += 1
screen.blit(self.img, (self.x, self.y))
class Player():
def __init__(self, x=0, y=0, velX=0, velY=0, w=0, h=0, increase=0, color=()):
global displayW, displayH
self.w = 20
self.h = 20
self.x = displayW / 2 - self.w / 2
self.y = displayH - 100
self.velX = 0
self.velY = 0
self.increase = 2
self.color = (0, 0, 0)
def draw(self):
global screen
pygame.draw.ellipse(screen, self.color, (self.x, self.y, self.w, self.h))
def animate(self):
self.x += self.velX
self.y += self.velY
def collision(self):
global displayW
# collision to walls
if self.x <= 0:
self.velX = 0
elif self.x + self.h >= displayW:
self.velX = 0
# SETUP
def setup():
pygame.display.set_caption("Food Catcher")
# QUIT GAME
def quitGame():
pygame.quit()
quit()
# CALL MAIN
if __name__ == "__main__":
main()
The problem was that had to minus the player's width
Before:
if self.y >= player.y and self.y <= player.y + player.h and self.x >= player.x and self.x <= player.x + player.w:
player.w += player.increase
player.h += player.increase
player.y - player.increase
After:
if self.y >= player.y - player.h and self.y <= player.y + player.h and self.x >= player.x - player.w and self.x <= player.x + player.w:
player.w += player.increase
player.h += player.increase
player.y - player.increase
I forgot that the objects x and y start at the top left.

Categories

Resources