I want to build Geometry Dash sort of game, but i'm stuck on this element where the enemy collide with a character. I've done both enemy and character as classes and so apparently x.colliderect() doesn't work. Any idea how I could implement this featuer in my code?
DISPLAYING THE WINDOW
#DISPLAY WINDOW
def draw_window(square, triangle, score):
#SCREEEN
WIN.fill(black)
#SCORE
score_text = FONT.render("Score: " +str(score), 1, white)
WIN.blit(score_text, (width - score_text.get_width() - 10, 10))
#OBJECTS
#LINE
pygame.draw.line(WIN, blue, (0, 400), (900, 400), 10)
#SQUARE
square.draw()
#TRIANGLE
triangle.draw()
pygame.display.update()
TRIANGLE (ENEMY) CLASS
#TRIANGLE
class Triangle():
def __init__(self):
self.x = 700
self.vel = 10
def draw(self):
pygame.draw.polygon(WIN, green, [[self.x, 395], [self.x + 50, 395][self.x + 25, 345]])
def move(self, score):
self.x -= 5
if self.x == -50:
self.x = 900
score += 1
print(score)
return score
SQUARE (CHARACTER) CLASS
#SQUARE
class Square():
def __init__(self):
self.y = 295
self.vel = 10
def draw(self):
pygame.draw.rect(WIN, red, pygame.Rect(50, self.y, 100, 100))
def jump(self, jumpCount):
jump = True
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
self.y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 0.5
else:
jump = False
jumpCount = 10
return jump, jumpCount
MAIN GAME LOOP
def main():
score = 0
jump = False
jumpCount = 10
triangle = Triangle()
square = Square()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#JUMPING
if event.type == pygame.KEYDOWN:
if not jump and event.key == pygame.K_SPACE:
jump = True
if event.type == square_hit:
pass #HERE IS THE COLLISION PROBLEM
if jump:
jump, jumpCount = square.jump(jumpCount)
draw_window(square, triangle, score)
score = triangle.move(score)
pygame.quit()
if __name__ == "__main__":
main()