vielleicht könnt ihr mir mit folgender Frage helfen:
Ich möchte ein das Image ship_rocket.png laden, wenn ich die Klasse rocket aufrufe. Leider bekomme ich die Fehlermeldung
pygame.error: Couldn't open ship_rocket.png.
Die Python-Modul rocket.py und das Bild ship_rocket.png liegen im selben Verzeichnis. Habt ihr eine Idee was falsch laufen könnte?
Verzeichnisstruktur
Code: Alles auswählen
├── modul
│ ├── __init__.py
│ ├── rocket.py
│ └── ship_rocket.png
├── space_rocket_game.py
Code: Alles auswählen
import pygame
from pygame.locals import *
import random
from modul.rocket import Rocket
class App:
'''
Main class for Application. This class creates the window for the game play.
The window will be closed when self._running changes its value to False
'''
#resolution for gaming window
windowWidth = 800
windowHeight = 600
def __init__(self):
self.on_init()
def on_init(self):
pygame.init()
#pygame module to work with the keyboard
#control how held keys are repeated
pygame.key.set_repeat(1, 1)
#pygame module to control the display window and screen
#Initialize a window or screen for display
self._gameDisplay = pygame.display.set_mode((self.windowWidth, self.windowHeight))
#Title for window
pygame.display.set_caption('Space Rocket')
#create an object to help track time
self._clock = pygame.time.Clock()
self._running = True
#Initialize additional classes
self.rocket = Rocket(self._gameDisplay)
self.game_loop()
def on_event(self):
#on_event() reacts on key events
while self._running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self._running = False
self.rocket.draw_line()
pygame.display.update()
self._clock=60
def game_loop(self):
self.on_event()
if __name__== '__main__':
theApp = App()
Code: Alles auswählen
import pygame
class Rocket():
def __init__(self, window):
pygame.init
self._window = window
self._rocket_image = pygame.image.load('ship_rocket.png')
def draw_line(self, x=(20,100), y=(20,200)):
print('draw_line')
pygame.draw.line(self._window, (255,0,0), x, y, 5)
def draw_rocket(self, x=(20,100), y=(20,200)):
print('draw_rocket')