1

I'm making a game and I tried to add some images instead of surfaces but I don't really know how it works I was wondering if some could explain to how its work cause I'm getting pygame.error: Unsupported image format and I don't know what that means or what I've done wrong.

Below is an image of the code in question as well as my directory files (on the left)

also im using a program called Pycharm idk if that might affect at all or not

# import images for sprites
img_dir = path.join(path.dirname(__file__), 'Img')

# load images
player_img = py.image.load(path.join(img_dir, "Playerimg.png")).convert()

class Player(py.sprite.Sprite):
    def __init__(self):
        py.sprite.Sprite.__init__(self)
        self.image = player_img
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH / 2
        self.rect.bottom = HEIGHT / 2
        self.Yspeed = 0
        self.rotatableimage = self.image

enter image description here

The rest of the code is below if you need it also I know it might be hard to troubleshoot image-based stuff soo here the code but without the images (two parts btw)

import random
import math
import pygame as py
from PlayerSprite import *

WIDTH = 800
HEIGHT = 600
FPS = 60

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)

# initialize pygame and create window
py.init()
py.mixer.init()
screen = py.display.set_mode((WIDTH, HEIGHT))
py.display.set_caption("Dimensional Drifter")
clock = py.time.Clock()

all_sprites = py.sprite.Group()
NPCs = py.sprite.Group()
bullets = py.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(14):
    n = NPC(player)
    all_sprites.add(n)
    NPCs.add(n)
# Game loop
running = True
while running:
    # keep loop running at the right speed
    clock.tick(FPS)

    for event in py.event.get():
        # check for closing window
        if event.type == py.QUIT:
            running = False
        elif event.type == py.MOUSEBUTTONDOWN:
            if event.button == 1:
                New_bullet = player.Shoot()
                all_sprites.add(New_bullet)
                bullets.add(New_bullet)


    # Update
    all_sprites.update()
    # # check if there a collision between the bullet and NPC
    hits = py.sprite.groupcollide(NPCs, bullets, True, True)
    # check if there a collision between the player and NPC
    hits = py.sprite.spritecollide(player, NPCs, True)
    if hits:
        running = False

    # updates the position of of mouse and rotates it towards the mouse position
    mouse_x, mouse_y = py.mouse.get_pos()
    player.rotate(mouse_x, mouse_y)
    # render
    screen.fill(BLACK)
    all_sprites.draw(screen)
    # flip the display
    py.display.flip()

py.quit()
import pygame as py
import math
import random

WIDTH = 800
HEIGHT = 600
FPS = 60

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)


class Player(py.sprite.Sprite):
    def __init__(self):
        py.sprite.Sprite.__init__(self)
        self.image = py.Surface((40, 40), py.SRCALPHA)
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH / 2
        self.rect.bottom = HEIGHT / 2
        self.Yspeed = 0
        self.rotatableimage = self.image

    def update(self):
        self.Xspeed = 0
        self.Yspeed = 0
        # line below allow for key press to equate to move of sprite
        keypreesed = py.key.get_pressed()
        if keypreesed[py.K_a]:
            self.Xspeed = - 11
        if keypreesed[py.K_d]:
            self.Xspeed = 11
        if keypreesed[py.K_w]:
            self.Yspeed = - 11
        if keypreesed[py.K_s]:
            self.Yspeed = 11
        self.rect.x += self.Xspeed
        self.rect.y += self.Yspeed
        # line below allow the sprite to wrap around the screen
        if self.rect.left > WIDTH:
            self.rect.right = 0
        if self.rect.right < 0:
            self.rect.left = WIDTH
        if self.rect.top > HEIGHT:
            self.rect.top = 0
        if self.rect.bottom < 0:
            self.rect.bottom = HEIGHT

    def rotate(self, mouse_x, mouse_y):
        rel_x = mouse_x - self.rect.x
        rel_y = mouse_y - self.rect.y
        angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)

        self.image = py.transform.rotate(self.rotatableimage, int(angle))
        self.rect = self.image.get_rect(center=(self.rect.centerx, self.rect.centery))
        return

    def Shoot(self):
        pos = self.rect.centerx, self.rect.centery
        mpos = py.mouse.get_pos()
        direction = py.math.Vector2(mpos[0] - pos[0], mpos[1] - pos[1])
        direction.scale_to_length(10)
        return Bullet(pos[0], pos[1], round(direction[0]), round(direction[1]))


class NPC(py.sprite.Sprite):
    def __init__(self, player):
        py.sprite.Sprite.__init__(self)
        self.player = player
        self.image = py.Surface((30, 30)).convert_alpha()
        self.image.fill(RED)
        self.originalimage = self.image
        self.rect = self.image.get_rect()
        self.spawn()

    # allows of spawning from all four side of the screen and set the x, y speed and spawn position
    def spawn(self):
        self.direction = random.randrange(4)
        if self.direction == 0:
            self.rect.x = random.randrange(WIDTH - self.rect.width)
            self.rect.y = random.randrange(-100, -40)
            self.Xspeed = random.randrange(-2, 2)
            self.Yspeed = random.randrange(4, 8)
        elif self.direction == 1:
            self.rect.x = random.randrange(WIDTH - self.rect.width)
            self.rect.y = random.randrange(HEIGHT, HEIGHT + 60)
            self.Xspeed = random.randrange(-2, 2)
            self.Yspeed = -random.randrange(4, 8)
        elif self.direction == 2:
            self.rect.x = random.randrange(-100, -40)
            self.rect.y = random.randrange(HEIGHT - self.rect.height)
            self.Xspeed = random.randrange(4, 8)
            self.Yspeed = random.randrange(-2, 2)
        elif self.direction == 3:
            self.rect.x = random.randrange(WIDTH, WIDTH + 60)
            self.rect.y = random.randrange(HEIGHT - self.rect.height)
            self.Xspeed = -random.randrange(4, 8)
            self.Yspeed = random.randrange(-2, 2)

    def update(self):
        self.rect.x += self.Xspeed
        self.rect.y += self.Yspeed
        # makes it so that NPC point to wards the player as it passes from side to side
        dir_x, dir_y = self.player.rect.x - self.rect.x, self.player.rect.y - self.rect.y
        self.rot = (180 / math.pi) * math.atan2(-dir_x, -dir_y)
        self.image = py.transform.rotate(self.originalimage, self.rot)
        # Respawns the NPC when they hit an side
        if self.direction == 0:
            if self.rect.top > HEIGHT + 10:
                self.spawn()
        elif self.direction == 1:
            if self.rect.bottom < -10:
                self.spawn()
        elif self.direction == 2:
            if self.rect.left > WIDTH + 10:
                self.spawn()
        elif self.direction == 3:
            if self.rect.right < -10:
                self.spawn()


class Bullet(py.sprite.Sprite):
    def __init__(self, x, y, Xspeed, Yspeed):
        py.sprite.Sprite.__init__(self)
        self.image = py.Surface((5, 5))
        self.image.fill(YELLOW)
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.Xspeed = Xspeed
        self.Yspeed = Yspeed

    def update(self):
        self.rect.x += self.Xspeed
        self.rect.y += self.Yspeed
        # kill if moved of screen
        if self.rect.bottom > HEIGHT or self.rect.top < 0:
            self.kill()
        if self.rect.right > WIDTH or self.rect.left < 0:
            self.kill()

it might be hard to help me on this one so because of images and what not but if could just explain it to me then i could implement on my own that could work too :)

edit 3: enter image description here

Edit 2: enter image description here Edit: image of error

enter image description here

  • That is odd, .png is a supported image type. Maybe [this](https://stackoverflow.com/questions/18318857/pygame-error-unsupported-image-format) can help – The Big Kahuna May 07 '20 at 02:09
  • @TheBigKahuna do you think it might be the `__file__` because pycharm turns methods like `__init__` purple but the `__file___` is just grey. could it be that? – Sir Braindmage May 07 '20 at 02:49
  • @TheBigKahuna or could something be wrong with The Pycharm folders/directory? – Sir Braindmage May 07 '20 at 02:52
  • Yeah, i suspect it might be `__file__`. maybe try `py.image.load("Img/Playerimg.png")`. But i dont know Pycharm enough to really help tho – The Big Kahuna May 07 '20 at 03:47
  • ight i'll see if that works – Sir Braindmage May 07 '20 at 03:49
  • nope still unsupported image format – Sir Braindmage May 07 '20 at 03:51
  • idk I'll try maybe a jpg instead? – Sir Braindmage May 07 '20 at 03:53
  • 1
    Yeah, you can also try giveing the full path, not sure what else sorry – The Big Kahuna May 07 '20 at 04:22
  • Could you elaborate about the Full path? – Sir Braindmage May 07 '20 at 04:28
  • So if your project was in your documents folder, the full path would be `C:/Users/**you**/Documents/Game Project/Img/PLayerimg.png`. – The Big Kahuna May 07 '20 at 04:31
  • Ok so instead of `just img_dir = path.join(path.dirname(__file__), 'Img')` it would be `img_dir = path.join(path.dirname(__file__), 'C:/Users/**you**/Documents/Game Project/Img/PLayerimg.png')` ?? – Sir Braindmage May 07 '20 at 04:34
  • Not quite, scrap that idea, and `print(path.join(img_dir, "Playerimg.png"))`. – The Big Kahuna May 07 '20 at 04:40
  • @TheBigKahuna wait were am I putting this? are you saying to replace this `py.image.load(path.join(img_dir, "Playerimg.png")).convert() ` with that or this with that `path.join(path.dirname(__file__), 'Img')` – Sir Braindmage May 07 '20 at 05:37
  • 1
    nah dont take out anything, just add it before the line with the error. trying to debug this by seeing what path it is trying to open – The Big Kahuna May 07 '20 at 05:42
  • Same thing :P `player_img = py.image.load(path.join(img_dir, "Playerimg.png")).convert() pygame.error: Unsupported image format` – Sir Braindmage May 07 '20 at 05:51
  • No idea what to do sorry – The Big Kahuna May 07 '20 at 06:00
  • @TheBigKahuna is how I was supposed to have it? look at edit 2 in post above – Sir Braindmage May 07 '20 at 06:27
  • Yes. What did it print – The Big Kahuna May 07 '20 at 06:53
  • yeah, I have no idea samething. could it be related to the two files? I'm just thinking why it ain't working – Sir Braindmage May 07 '20 at 06:55
  • The `print` line that @TheBigKahuna was *not supposed to fix the problem*! It's just so you can verify it prints out the correct path. Not "something" or "nothing", but *the actual path*. If your code cannot find the image, then it fails. This is just to **verify** it *should* be able to find the image. That said : try another image, not one you created (you may have done it wrong). And/or add the image to your post – possibly there *is* something wrong with the file. – Jongware May 07 '20 at 06:58
  • @usr2564301 yeah I know where trying to bug fix it it's not printing anything so – Sir Braindmage May 07 '20 at 07:01
  • @usr2564301 ok I've got something I changed out the image and got a different error check edit: 3 – Sir Braindmage May 07 '20 at 07:04
  • That is not possible. Python does not print nothing if you explicitly tell it to print the path. I am sorry to say, but it seems you are doing something fundamentally unexpected – tons of possibilities here. (Are you running the right program, are you sure that line throws the error, are you sure you modified the code, etc. ... We cannot tell!) Remove all of the redundant code and just leave enough to load that image. Add `print('hello')` before and `print('done')` after – zoom in on the problematic code *only*. – Jongware May 07 '20 at 07:06
  • @usr2564301 I Think I was just the image idk why but after removing the .convert with a new image it fixed itself. – Sir Braindmage May 07 '20 at 07:10

0 Answers0