0
import pygame
import os

pygame.init()
# Images
bgImg = pygame.image.load("Images/background.png")
playerImgRight = (pygame.image.load("Images/playerRight.png"))
playerImgLeft = pygame.image.load("Images/playerLeft.png")
playerImgBack = pygame.image.load("Images/playerBack.png")
chestClosed = pygame.transform.scale(pygame.image.load("Images/chestsClosed.png"), (30, 40))
chestOpened = pygame.transform.scale(pygame.image.load("Images/chestOpen.png"), (30, 40))
currentPlayerImg = playerImgBack

screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("Richmonia")
pygame.display.set_icon(currentPlayerImg)
clock = pygame.time.Clock()

playerX = 380
playerY = 380
playerXSpeed = 0
playerYSpeed = 0

Chest Class I want but the collision detection in the chest update function

class Chest:
    def __init__(self, x, y, openImage, closeImage, playerX, playerY):
        self.x = x
        self.y = y
        self.openImage = openImage
        self.closeImage = closeImage
        self.currentImage = self.closeImage
        self.playerX = playerX
        self.playerY = playerY


    def update(self):
        print("Placeholder")

    def show(self):
        screen.blit(self.currentImage, (self.x, self.y))


chestOne = Chest(100, 100, chestOpened, chestClosed, playerX, playerY)

running = True
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                currentPlayerImg = playerImgBack
                playerYSpeed = -5
            if event.key == pygame.K_DOWN:
                currentPlayerImg = playerImgBack
                playerYSpeed = 5
            if event.key == pygame.K_LEFT:
                currentPlayerImg = playerImgLeft
                playerXSpeed = -5
            if event.key == pygame.K_RIGHT:
                currentPlayerImg = playerImgRight
                playerXSpeed = 5

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                playerYSpeed = 0
            if event.key == pygame.K_DOWN:
                playerYSpeed = 0
            if event.key == pygame.K_LEFT:
                playerXSpeed = 0
            if event.key == pygame.K_RIGHT:
                playerXSpeed = 0

    if playerY <= 0:
        playerY = 0
    if playerY >= 740:
        playerY = 740
    if playerX <= 0:
        playerX = 0
    if playerX >= 760:
        playerX = 760

    playerX += playerXSpeed
    playerY += playerYSpeed
    screen.blit(bgImg, (0, 0))
    chestOne.show()
    chestOne.update()

    screen.blit(currentPlayerImg, (playerX, playerY))
    pygame.display.update()
pygame.quit()
quit()

I know there is a way with sprites but I want to know if there is a way to do it with just the images. Also bonus question how to I get the fps.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Elfed
  • 23
  • 4

1 Answers1

1

Use a pygame.Rect object and colliderect() to find a collision between a rectangle and an object.
pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. The position of the rectangle can be specified by a keyword argument:

running = True
while running:
    # [...]

    player_rect = currentPlayerImg.get_rect(topleft = (playerX, playerY))
    chestOne_rect = chestOne.currentImage.get_rect(topleft = (self.x, self.y))
    if player_rect .colliderect(chestOne_rect):
        print("hit")

    # [...]

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time.
So in your case the frames per second are 60:

while running:
   clock.tick(60)

pygame.time.Clock.tick returns the number of milliseconds passed since the previous call. If you call it in the application loop, then this is the number of milliseconds passed since the last frame.

while run:
    ms_since_last_frame = clock.tick(60)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174