Seite 1 von 1

pygame

Verfasst: Sonntag 10. April 2022, 09:19
von Tim0123
Hallo, Programm funktioniert nicht wenn ich es starte kommen nur Fehlermeldungen. Könnte mir da jemand helfen ?
main:

Code: Alles auswählen

import pygame, sys, time
from player import Player
import obstacle
from alien import Alien
from settings import *

class Game():
	def __init__(self):
		# Game setup
		pygame.init()
		self.display_surface = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
		pygame.display.set_caption('Space Invader')
		self.clock = pygame.time.Clock()

		# Player setup
		player_sprite = Player((WINDOW_WIDTH / 2,WINDOW_HEIGHT),WINDOW_WIDTH,5)
		self.player = pygame.sprite.GroupSingle(player_sprite)

		# Obstacle setup
		self.shape = obstacle.shape
		self.block_size = 6
		self.blocks = pygame.sprite.Group()
		self.obstacle_amount = 4
		self.obstacle_x_positions = [num * (WINDOW_WIDTH / self.obstacle_amount) for num in range(self.obstacle_amount)]
		self.create_multiple_obstacles(*self.obstacle_x_positions, x_start = 42, y_start = 480,)
		
		# Alien setup
		self.aliens = pygame.sprite.Group()
		self.alien_setup(6,8)

	def create_obstacle(self,x_start,y_start,offset_x):
		for row_index, row in enumerate(self.shape):
			for col_index,col in enumerate(row):
				if col == 'x':
					x =  x_start + col_index * self.block_size + offset_x
					y =  y_start + row_index * self.block_size
					block = obstacle.Block(self.block_size,(241,79,80),x,y)
					self.blocks.add(block)

	def create_multiple_obstacles(self,*offset,x_start,y_start,):
		for offset_x in offset:
			self.create_obstacle(x_start,y_start,offset_x)

	def alien_setup(self,rows,cols):
		for row_index, row in enumerate(range(rows)):
			for col_index, col in enumerate(range(cols)):
				x = col_index
				y = row_index
				alien_sprite = Alien('red',x,y)
				self.aliens.add(alien_sprite)

	def run(self):
		last_time = time.time()
		while True:
			# delta time
			dt = time.time() - last_time
			last_time = time.time()

			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					pygame.quit()
					sys.exit()

			# game logic
			self.display_surface.fill(GREY)
			self.player.draw(self.display_surface)
			self.player.sprite.lasers.draw(self.display_surface)
			self.blocks.draw(self.display_surface)
			self.aliens.draw(self.display_surface)

			self.player.update()
			pygame.display.update()
			self.clock.tick(FRAMERATE)

if __name__ == '__main__':
	game = Game()
	game.run()

alien:

Code: Alles auswählen

import pygame

class Alien(pygame.sprite.Sprite):
	def __init__(self,color,x,y):
		super().__init__()
		file_path = '..graphics/' + color + '.png'
		self.image = pygame.image.load(file_path).convert_alpha()
		self.rect = self.image.get_rect(topleft = (x,y))
Fehlermeldungen:
Traceback (most recent call last):
File "E:\Coding\Atom Projects\Atom Projekt 1\Code\main.py", line 76, in <module>
game = Game()
File "E:\Coding\Atom Projects\Atom Projekt 1\Code\main.py", line 29, in __init__
self.alien_setup(6,8)
File "E:\Coding\Atom Projects\Atom Projekt 1\Code\main.py", line 49, in alien_setup
alien_sprite = Alien('red',x,y)
File "E:\Coding\Atom Projects\Atom Projekt 1\Code\alien.py", line 7, in __init__
self.image = pygame.image.load(file_path).convert_alpha()
FileNotFoundError: No file '..graphics/red.png' found in working directory 'E:\Coding\Atom Projects\Atom Projekt 1\Code'.

Re: pygame

Verfasst: Sonntag 10. April 2022, 09:22
von ThomasL

Code: Alles auswählen

file_path = '..graphics/' + color + '.png'
ob das so richtig ist?

Re: pygame

Verfasst: Sonntag 10. April 2022, 09:26
von Sirius3
Der Fehler ist doch eindeutig, relativ zu Deinem aktuellen Arbeitsverzeichnis gibt es mit großer Wahrscheinlichkeit kein Verzeichnis ..graphics. Wo die Dateien wirklich liegen, kannst nur Du wissen.

Re: pygame

Verfasst: Sonntag 10. April 2022, 09:41
von Tim0123
Danke der pfad war falsch vor dem graphics hat noch ein / gefehlt