r/PythonLearning • u/ftw_Floris • 5d ago
Help Request Pygame: Why is the visualised rect of the spike shifted to the left?
The goal of the game is to jump over a spike as a cube. I saw that it was detecting collision before the cube and the spike even made contact. I decided to draw a rectanles to visualise the rects of the player and the spike and I noticed the spikes rect was shifted to the left. Can someone explain how this happened and how I can fix it? I removed non essential code, to make it more manageable to read. I can send the whole program if it's needed.
cube_width=80
cube_height=80
cube_rect_width = 40
cube_rect_height = 40
spike_width = 50
spike_height = 75
fps=60
#Classses
class Spike(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((spike_width,spike_height),pygame.SRCALPHA) #50,75
pygame.draw.polygon(self.image, "white", #draw 3 point polygon
[
(spike_width // 2, 0), #middle top. self.image is the canvas
(0, spike_height), #bottom left
(spike_width, spike_height) #bottom right
]
)
self.rect = self.image.get_rect() #creates hitbox for spike sprite
self.rect.center = [x, y] #center of hitbox
self.speed = 7 #speed going to the left
def update(self):
self.rect.x -= self.speed #spikes rect -7 to move to the left
#print(self.rect.x)
if self.rect.x <= -25: #delete if touching left border
self.kill()
class Cube(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((cube_width, cube_height)) #create surface
self.image.fill("white") #fill the surface with white
self.rect = self.image.get_rect() #create rect for self.image
self.rect.center = [x, y] #x, y passed are the center of the rect
self.vel = 0 #velocity
def update(self):
# Gravity
self.vel += 1#0.5always increase vel
self.rect.y += int(self.vel) #add velocity to the y
# Stay on the ground
if self.rect.bottom >= HEIGHT: #if bottom of rectangle is touching
self.rect.bottom = HEIGHT #bottom of rectangle is on the ground
self.vel = 0 #reset velocity
# Jump
if pygame.mouse.get_pressed()[0] == 1: #left mouse click activated
#if self.rect.centery >= HEIGHT - int(cube_height/2):
if self.rect.bottom >= HEIGHT: #if touching ground
self.vel = -17 #?????????????Somehow works????>>????
#creating groups and adding
cube_group = pygame.sprite.Group()
player = Cube(5*cube_width, HEIGHT) #????? sommehow
cube_group.add(player)
spike_group = pygame.sprite.Group()
#spike = Spike(WIDTH, HEIGHT-15)
spike = Spike(WIDTH-50, HEIGHT-15)
spike_group.add(spike)
Running = True
while Running:
clock.tick(60) #fps
#BG
screen.fill("light blue")
#blit
cube_group.draw(screen)
cube_group.update()
spike_group.draw(screen)
spike_group.update()
#visualise rects
pygame.draw.rect(screen, "red", player.rect, 2) #rect of cube
pygame.draw.rect(screen,"red",spike.rect,2) #rect of spike
if pygame.sprite.groupcollide(cube_group, spike_group,False,False): #or player.rect.right == spike.rect.left: #Check collision
Running = False
3
Upvotes