0

I was trying to understand a pygame code but I couldn't run it on my computer.I am very new in python.The portion of the code that is giving error is here..

from pygame.locals import *
from random import randint
import pygame
import time

class App:

windowWidth = 800
windowHeight = 600
player = 0
apple = 0

def __init__(self):
    self._running = True
    self._display_surf = None
    self._image_surf = None
    self._apple_surf = None
    self.game = Game()
    self.player = Player(5) 
    self.apple = Apple(8,5)
    self.computer = Computer(5)

def on_init(self):
    pygame.init()
    self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)

    pygame.display.set_caption('Pygame pythonspot.com example')
    self._running = True
    self._image_surf = pygame.image.load("pygame.png").convert()
    self._apple_surf = pygame.image.load("apple.png").convert()
if __name__ == "__main__" :
    theApp = App()
    theApp.on_execute()

the error message is exactly like this...

RESTART: C:/Users/Dipto/AppData/Local/Programs/Python/Python36/gametest1.py 
Traceback (most recent call last):
File "C:/Users/Dipto/AppData/Local/Programs/Python/Python36/gametest1.py", 
line 263, in <module>
theApp.on_execute()
File "C:/Users/Dipto/AppData/Local/Programs/Python/Python36/gametest1.py", 
line 233, in on_execute
if self.on_init() == False:
File "C:/Users/Dipto/AppData/Local/Programs/Python/Python36/gametest1.py", 
line 185, in on_init
self._image_surf = pygame.image.load("pygame.png").convert()
pygame.error: Couldn't open pygame.png
Hamim
  • 1
  • 2
  • Which directory is `pygame.png` in? What's the current working directory you're trying to run the program from? If they're not the same, you can't just use `"pygame.png"` as a path. – abarnert Apr 29 '18 at 03:43
  • @abarnert I have used this,but it also didn't work.. folder = os.path.dirname(os.path.realpath(__file__)) self._image_surf = pygame.image.load(os.path.join(folder, "small-mario.png")) – Hamim Apr 29 '18 at 06:35
  • @Hamim That still doesn't give me enough information to help yu. Is `small-mario.jpg` in the same directory as that script? – abarnert Apr 29 '18 at 06:38
  • Also, please edit your question to include all the information needed to debug it, don't try to answer it all in comments. The comment system here isn't great, and comments also aren't visible to people trying to decide which questions to answer. – abarnert Apr 29 '18 at 06:39
  • @abarnert now it works.I was not aware of the directory.thanks – Hamim Apr 29 '18 at 06:46

0 Answers0