0

lol I just copied the whole code in here and wanted to ask why isnt my player and obstale colliding? it doesnt print out anything?

import random
import pygame
import time
import os

pygame.init()
pygame.font.init()



# import pics
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
player_sprite = pygame.image.load(os.path.join(sourceFileDir, "assets", "player.png"))

# window setup
WIDTH = 1080
HEIGHT = 540
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("random game")

# enemy_sprite
enemy_sprite = pygame.Surface((25,25))
enemy_sprite.fill((0,255,0))

# obstacle
obstacle = pygame.Rect(400, 200, 80, 80)

# obj
class Object:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def render(self, window):
        WIN.blit(self.image, (self.x, self.y))

# player
class Player(Object):
    def __init__(self, x, y):
        super().__init__(x, y)
        self.image = player_sprite
        self.rect = self.image.get_rect()
        

# enemy
class Enemy(Object):
    def __init__(self, x, y):
        super().__init__(x, y)
        self.image = enemy_sprite
        self.rect = self.image.get_rect()

    def move_down(self, vel):
        self.y += vel

def main():
    run = True
    FPS = 60
    clock = pygame.time.Clock()
    wave_length = 10
    enemies = []
    enemy_vel = 10

    player_vel = 5
    player = Player(100,100)


    def render_window():
        WIN.fill((0,0,0))
        player.render(WIN)
        pygame.draw.rect(WIN, (255,255,255), obstacle, 4) 
        for enemy in enemies:
            enemy.render(WIN)
        


        pygame.display.update()
    while run:
        clock.tick(FPS)
        render_window()
                
        if player.rect.colliderect(obstacle):
            print("collision")



        if len(enemies) == 0:
            wave_length += 0

            for i in range(wave_length):
                enemy = Enemy(random.randrange(80, 1000), random.randrange(-250, 0))
                enemies.append(enemy)


                       

        

        # player movement
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            player.x -= player_vel
        if keys[pygame.K_d]:
            player.x += player_vel
        if keys[pygame.K_w]:
            player.y -= player_vel
        if keys[pygame.K_s]:
            player.y += player_vel

        # enemy movement
        for enemy in enemies:
            enemy.move_down(enemy_vel)
            if enemy.y > HEIGHT:
                enemies.remove(enemy)


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False



main()

This site asks me for more detail but I think that is already enough the question is shorter in relevanz to the code so Imma fill this up here with this text ty for helping

robot
  • 11
  • 1
  • *"why isnt my player and obstale colliding"* - where do you try to detect the the collision? – Rabbid76 Dec 12 '20 at 16:01
  • 1
    Please read https://stackoverflow.com/help/how-to-ask which specifically says NOT to 'just paste all your code in' – JeffUK Dec 12 '20 at 16:02
  • As far as I can see, there is no code here that would detect collisions, and no code that would print anything... – John Gordon Dec 12 '20 at 16:04
  • ye I fixed it and added the line I deleted before copieng it my bad – robot Dec 12 '20 at 16:12
  • @robot Read [Why is my collision test always returning 'true' and why is the position of the rectangle of the image always wrong (0, 0)?](https://stackoverflow.com/questions/57730329/why-is-my-collision-test-always-returning-true-and-why-is-the-position-of-the) – Rabbid76 Dec 12 '20 at 16:45

0 Answers0