0

so I made a box in my game and I wanted to collide its 2 centerx and centery like collid and not make my player go through the square but instead of that its teleporting my player of each sides I want it to collide as I did it for its top

if playerman.rect.centerx > assets.rect.centerx and playerman.rect.centerx < assets.rect.centerx - playerman.width:
   playerman.x = assets.rect.centerx + playerman.width
if playerman.rect.centery < floor.rect.centery and playerman.rect.centery > assets.rect.centery + playerman.width:
  playerman.x = assets.rect.centery     

like I just want to to collide without teleporting me like for example I have an apple and I can't go through it because the apple has collision and it won't let me go through it so I can ago around the apple.

and this is my full code

import pygame
pygame.init()

window = pygame.display.set_mode((700,700))
pygame.display.set_caption("Game")

plat = pygame.image.load("gt.png")
coinss = pygame.image.load("coin_gold.png")
slash1 = pygame.image.load("slash.png")
box = pygame.image.load("box.png")

lefts = [pygame.image.load("Sprite-0001.png"),
        pygame.image.load("Sprite-0002.png"),
        pygame.image.load("Sprite-0003.png"),
        pygame.image.load("Sprite-0004.png"),
        pygame.image.load("Sprite-0005.png"),
        pygame.image.load("Sprite-0006.png"),
        pygame.image.load("Sprite-0007.png"),
        pygame.image.load("Sprite-0008.png"),
        pygame.image.load("Sprite-0009.png")

         ]
toxics = pygame.image.load("toxic.png")

stand = pygame.image.load("stands.png")


rights = [pygame.image.load("Sprite-50.png"),
         pygame.image.load("Sprite-51.png"),
         pygame.image.load("Sprite-52.png"),
         pygame.image.load("Sprite-53.png"),
         pygame.image.load("Sprite-54.png"),
         pygame.image.load("Sprite-55.png"),
         pygame.image.load("Sprite-56.png"),
         pygame.image.load("Sprite-57.png"),
         pygame.image.load("Sprite-58.png")


         ]

right = [
            pygame.image.load("enemy.png"),
            pygame.image.load("enemys1.png"),
             pygame.image.load("enemys2.png"),
             pygame.image.load("enemys3.png"),
             pygame.image.load("enemys4.png"),
             pygame.image.load("enemys5.png")
            ]

class projectile(object):

    def __init__(self,x,y,color):
        self.toxic = pygame.image.load("toxic.png")
        self.rect  = self.toxic.get_rect()
        self.toxic = pygame.transform.scale(self.toxic,(self.toxic.get_width()//2,self.toxic.get_height()//2))
        self.x = x
        self.y = y
        self.speed = 10
        self.color = color

    def draw(self):
        self.rect.topleft = (self.x,self.y)
        window.blit(self.toxic,self.rect)
class assets:
    def __init__(self,x,y,height,width,color):
        self.box = pygame.image.load("box.png")
        self.x = x
        self.y =y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        window.blit(self.box,self.rect)
# define the assets
Blue = [28,155,10]
box1 = assets(50,420,50,50, Blue)
boxes = [box1]




# the enemy
class enes:
    def __init__(self,x,y,width,height,end):
        self.right = [
             pygame.image.load("enemy.png"),
             pygame.image.load("enemys1.png"),
             pygame.image.load("enemys2.png"),
             pygame.image.load("enemys3.png"),
             pygame.image.load("enemys4.png"),
             pygame.image.load("enemys5.png")]
        self.right = [pygame.transform.scale(image,(image.get_width()*4,image.get_height()*4)) for image in self.right]
        self.x = x
        self.y = y
        self.height = height
        self.path = [x,end]
        self.walkCount = 0
        self.walk_so = 0
        self.so_walk = 0
        self.speed = 3
    def draw(self,window):
        self.move()
        if self.walkCount + 1 >= 33:
            self.walkCount = 0
        if self.speed > 0:
            window.blit(self.right[self.so_walk//3], (self.x,self.y))
            self.walkCount += 1
        else:
            window.blit(self.right[self.so_walk//3],(self.x,self.y))
            self.walkCount += 1
    def move(self):
        if self.speed > 0:  
            if self.x < self.path[1] + self.speed: 
                self.x += self.speed
            else:
                self.speed = self.speed * -1
                self.x += self.speed
                self.walkCount = 0
        else: 
            if self.x > self.path[0] - self.speed: 
                self.x += self.speed
            else:  
                self.speed = self.speed * -1
                self.x += self.speed
                self.walkCount = 0
    def hitbox(self):
        print('hit')
goblin = enes(100,280,64,64,200)
goby = [goblin]



# color for enems
green = (63, 190, 22)
enems1 = enes(350,259,50,50, green)
florida = [enems1]

# player class

class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height  = height
        self.width = width
        self.speed = 5
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        #hit box
        self.hitbox = (self.x + 20, self.y, 28, 60)
        self.stand = pygame.image.load("stands.png")
        self.lefts = [
        pygame.image.load("Sprite-0001.png"),
        pygame.image.load("Sprite-0002.png"),
        pygame.image.load("Sprite-0003.png"),
        pygame.image.load("Sprite-0004.png"),
        pygame.image.load("Sprite-0005.png"),
        pygame.image.load("Sprite-0006.png"),
        pygame.image.load("Sprite-0007.png"),
        pygame.image.load("Sprite-0008.png"),
        pygame.image.load("Sprite-0009.png")
         ]
        self.rights = [
        pygame.image.load("Sprite-50.png"),
         pygame.image.load("Sprite-51.png"),
         pygame.image.load("Sprite-52.png"),
         pygame.image.load("Sprite-53.png"),
         pygame.image.load("Sprite-54.png"),
         pygame.image.load("Sprite-55.png"),
         pygame.image.load("Sprite-56.png"),
         pygame.image.load("Sprite-57.png"),
         pygame.image.load("Sprite-58.png")
         ]
        self.rights = [pygame.transform.scale(image,(image.get_width()*4,image.get_height()*4)) for image in self.rights]
        self.lefts = [pygame.transform.scale(image,(image.get_width()*4,image.get_height()*4)) for image in self.lefts]
        self.stand = pygame.transform.scale(self.stand,(self.stand.get_width()*4,self.stand.get_height()*4))
        self.bo_index = 0
        self.start_time = pygame.time.get_ticks()
        self.anim_fps = 400
        self.anim_index = 0
        self.stans_index = 0
        self.direction = "right"
        self.direction = "left"
        self.direction = "standing"

        self.rect = pygame.rect = pygame.Rect(self.x,self.y,width, height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)

        if self.direction == "left":
             player_image = self.lefts[self.anim_index]
             self.anim_index += 1
             if self.anim_index == len(self.lefts):
                 self.anim_index = 0
        elif self.direction == "right":
            player_image = self.rights[self.anim_index]
            self.anim_index += 1
            if self.anim_index == len(self.rights):
                self.anim_index = 0
        else:
            player_image = self.stand

        player_rect = player_image.get_rect(center = self.rect.center) 
        player_rect.centerx += 10 # 10 is just an example
        player_rect.centery += -20 # 15 is just an example
        window.blit(player_image, player_rect)





# platforms
class platform:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.plat = pygame.image.load("gt.png")
        self.rect = pygame.Rect(x,y,plat.get_width(), plat.get_height())
        self.plat = pygame.transform.scale(self.plat,(self.plat.get_width()//2,self.plat.get_height()//2))
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        window.blit(self.plat,self.rect)

# Coins
class coin:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.coinss = pygame.image.load("coin_gold.png")
        self.rect = pygame.Rect(x,y,coinss.get_width(), coinss.get_height())
        self.plat = pygame.transform.scale(self.coinss,(self.coinss.get_width()//2,self.coinss.get_height()//2))
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        window.blit(self.coinss,self.rect)


# Floor
class floor:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)


font = pygame.font.Font('freesansbold.ttf', 30)
score = 0
text = font.render('Gold  = ' + str(score), True, (255,255,255))
textRect = text.get_rect()  
textRect.center = (100, 40)

# enemy
Sfont = pygame.font.Font('freesansbold.ttf', 30)
Kills = 0
Stext = Sfont.render('Kills  = ' + str(score), True, (255,255,255))
textRectS = Stext.get_rect()  
textRectS.center = (400, 100)


# fps
FPS = 60
clock = pygame.time.Clock()

# colors
Green = (63, 190, 22)
Blue = (22, 190, 175)
white = (240, 240, 240)

# define the enemy player coin classes
playerman = player(150,350,30,30, Blue)
enemy1 = platform(150,390,190,10, Green)
enemy2 = platform(300,310,190,10, Green)
enemy3 = platform(80,260,190,10, Green)
enemy4 = platform(250,180,190,10, Green)
enemy5 = platform(490,120,190,10, Green)
enemy6 = platform(-50,100,190,10, Green)
enemy7 = platform(180,50,190,10, Green)
platforms = [enemy1,enemy2,enemy3,enemy4,enemy5,enemy6,enemy7]

# coin class
coin1 = coin(180,320,150,150, Green)
coin2 = coin(350,250,50,50, Green)
coin3 = coin(150,200,50,50, Green)


Coins_list = [coin1,coin2,coin3]

# floor class
floor1 = floor(-1000,490,9999,50, white)
flories = [floor1]
# shoot the enmey loop

#main loop
bullets = []
runninggame = True
while runninggame:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False


    if playerman.y < 250:
        playerman.y += 1
        for platform in platforms:
            platform.y += playerman.speed
        for coin in Coins_list:
            coin.y += playerman.speed
        for floor in flories:
            floor.y += playerman.speed
        for enes in goby:
            enes.y += playerman.speed
        for assets in boxes:
            assets.y += playerman.speed







    if playerman.y > 450:

        playerman.y -= playerman.fall
        for platform in platforms:
            platform.y -= playerman.fall
        for coin in Coins_list:
            coin.y -= playerman.fall
        for floor in flories:
            floor.y -= playerman.fall
        for enes in goby:
            enes.y -= playerman.fall
        for assets in boxes:
            assets.y -= playerman.fall




    keys = pygame.key.get_pressed()
    playerman.direction = "standing"

    if keys[pygame.K_a]:     
        for bullet in bullets:
            if bullet.x < 500 and bullet.x > 0:
                bullet.x -= bullet.speed 
            else:
                bullets.pop(bullets.index(bullet))
        if len(bullets) < 1:  
            bullets.append(projectile(round(playerman.x+playerman.width//5),round(playerman.y + playerman.height//5),(0,0,0)))
    if keys[pygame.K_d]:   
        for bullet in bullets:
            if bullet.x < 900 and bullet.x > 0:
                bullet.x += bullet.spseed 
            else:
                bullets.pop(bullets.index(bullet))
        if len(bullets) < 2:  
            bullets.append(projectile(round(playerman.x+playerman.width//15),round(playerman.y + playerman.height//5),(0,0,0)))


    if keys[pygame.K_LEFT]:
        playerman.direction = "right"
        playerman.x -= playerman.speed
        if playerman.x < 100:
            playerman.x += playerman.speed
            for platform in platforms:
                platform.x += playerman.speed
            for coin in Coins_list:
                coin.x += playerman.speed
            for enes in goby:
                enes.x += playerman.speed
            for assets in boxes:
                assets.x += playerman.speed


    if keys[pygame.K_RIGHT]:
        playerman.direction = "left"
        playerman.x += playerman.speed
        if playerman.x > 400:
            playerman.x -= playerman.speed
            for platform in platforms:
                platform.x -= playerman.speed
            for coin in Coins_list:
                coin.x -= playerman.speed
            for enes in goby:
                enes.x -= playerman.speed
            for assets in boxes:
                assets.x -= playerman.speed




    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False
        collide = False
        for platform in platforms:
            if playerman.rect.colliderect(platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = platform.rect.top - playerman.height + 1
                if playerman.rect.right > platform.rect.left and playerman.rect.left < platform.rect.left - playerman.width:
                    playerman.x = platform.rect.left - playerman.width
                if playerman.rect.left < platform.rect.right and playerman.rect.right > platform.rect.right + playerman.width:
                    playerman.x = platform.rect.right


            for i in range(len(Coins_list)-1,-1,-1):
                if playerman.rect.colliderect(Coins_list[i].rect):
                    del Coins_list[i]
                    score += 1
                    text = font.render('Score = ' + str(score), True, (255,255,255))
                    textRect = text.get_rect()  
                    textRect.center = (100, 40)





        for floor in flories:
            if playerman.rect.colliderect(floor.rect):
                collide = True
                playerman.isJump = False
                playerman.y = floor.rect.top - playerman.height + 1
                if playerman.rect.right > floor.rect.left and playerman.rect.left < floor.rect.left - playerman.width:
                    playerman.x = floor.rect.left - playerman.width
                if playerman.rect.left < floor.rect.right and playerman.rect.right > floor.rect.right + playerman.width:
                    playerman.x = floor.rect.right




        for assets in boxes:
            if playerman.rect.colliderect(assets.rect):
                collide = True
                playerman.isJump = False
                playerman.y = assets.rect.top - playerman.height + 1
                if playerman.rect.right > assets.rect.left and playerman.rect.left < assets.rect.left - playerman.width:
                    playerman.x = assets.rect.left - playerman.width
                if playerman.rect.left < floor.rect.right and playerman.rect.right > assets.rect.right + playerman.width:
                    playerman.x = assets.rect.right

                if playerman.rect.centerx > assets.rect.centerx and playerman.rect.centerx < assets.rect.centerx - playerman.width:
                    playerman.x = assets.rect.centerx + playerman.width
                if playerman.rect.centery < floor.rect.centery and playerman.rect.centery > assets.rect.centery + playerman.width:
                    playerman.x = assets.rect.centery













            if playerman.rect.bottom >= 490:
                collide = True
                playerman.isJump = False
                playerman.JumpCount = 10
                playerman.y = 490 - playerman.height

            if collide:
                if keys[pygame.K_UP]:
                    playerman.isJump = True
                playerman.fall = 0


    else:
        if playerman.JumpCount > 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.JumpCount = 10
            playerman.isJump = False

    window.fill((74, 107, 104))
    for assets in boxes:
        assets.draw()
    goblin.draw(window)
    window.blit(Stext,textRectS)
    for bullet in bullets:
        bullet.draw()
    window.blit(text,textRect)
    for platform in platforms:
        platform.draw()
    for coin in Coins_list:
        coin.draw()
    playerman.draw()
    for floor in flories:
        floor.draw()



    pygame.display.update()
pygame.quit()
















Glenn Mackintosh
  • 2,765
  • 1
  • 10
  • 18
Habib Ismail
  • 69
  • 5
  • 16
  • 2
    This is a general comment rather than a comment specific to your question. I am sorry but I really was not able to understand your question. You really should look into using pygame's `Rect` class rather than maintaining the x, y, height, and width as separate attributes. They are also very helpful when doing collision detection which you are asking about. You should also look at using the pygame Sprite and Sprite Groups mechanics. If you are working with pygame they are quite helpful. – Glenn Mackintosh May 21 '20 at 17:42
  • Do you mean 'through' instead of 'throw'? – Glenn Mackintosh May 21 '20 at 17:43
  • I mean through like I dont want it to teleport me and I dont want it to go through the square just collid with it – Habib Ismail May 21 '20 at 17:47
  • like how I did it for the top of my player having trouble with centers – Habib Ismail May 21 '20 at 17:48
  • 3
    As @Glenn already suggested, use [`pygame.Rect`](https://www.pygame.org/docs/ref/rect.html) objects which have a number of methods for dealing with collisions — don't reinvent the rectangle. – martineau May 21 '20 at 18:03

0 Answers0