Warum nur schwarzer Screen beim Start von PyGame Script?

Hi,

Ich habe ein kleines Spiel angefangen zu schreiben jedoch wenn ich versuche es zu starten kriege ich nur ein schwarzen screen.

Hier ist der Code

import pygame

import sys

# pygame function

pygame.display.set_caption(“Knight Slasher”)

pygame.init()

screen = pygame.display.set_mode((1280, 720))

clock = pygame.time.Clock()

running = True

dt = 0

color = (255,0,0)

blue = (0,0,255)

rect = (20,20)

point = pygame.mouse.get_pos()

player_1 = pygame.draw.rect(screen, color, pygame.Rect(600,360,100,100))

#game loop

while running:

   for event in pygame.event.get():

       if event.type == pygame.QUIT:

           running = False

   # fill the screen with a color to wipe away anything from last frame

   screen.fill(“green”)

   player_1

   #movement

   key= pygame.key.get_pressed()

if key[pygame.K_a] == True:

 player_1.move_ip(-5, 0)

if key[pygame.K_d] == True:

 player_1.move_ip(5, 0)

if key[pygame.K_w] == True:

 player_1.move_ip(0, -5)

if key[pygame.K_s] == True:

 player_1.move_ip(0, 5)

   # flip() the display to put your work on screen

pygame.display.flip()

   # limits FPS to 60

   # dt is delta time in seconds since last frame, used for framerate-

   # independent physics.

dt = clock.tick(60) / 1000

pygame.quit()

(Ich programmiere in Sublime Text ) ich hoffe jemand kann mir helfen

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
2 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
johndont
7 months ago

ChatGPT:

Your code contains some errors that cause the black screen. Here is a corrected version:

1. The rectangle player_1 must be redrawn in the game loop.

2. The engagement of motion controls is not correct.

3. The move_ip method should be applied to the rectangle.

4. The call of pygame.display.flip() and clock.tick() should take place within the game loop.

Here is the corrected code:

import pygame

import sys

# initialize pygame

pygame.init()

pygame.display.set_caption(“Knight Slasher”)

# Create screen and watch

screen = pygame.display.set_mode(1280, 720)

clock = pygame.time.Clock()

running = True

# Define colors and player corner

color = (255, 0, 0)

player_1 = pygame.Rect(600, 360, 100, 100)

# Game loop

while running:

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

# Fill screen with a color

screen.fill(“green”)

# Draw the player’s right

pygame.draw.rect(screen, color, player_1)

# Move

keys = pygame.key.get_pressed()

if keys[pygame.K_a]:

player_1.move_ip(-5, 0)

if keys[pygame.K_d]:

player_1.move_ip(5, 0)

if keys[pygame.K_w]:

player_1.move_ip(0, -5)

if keys[pygame.K_s]:

player_1.move_ip(0, 5)

# Update display

pygame.display.flip()

# Limit FPS to 60

clock.tick(60)

pygame.quit()

Make sure you insert the code correctly, especially in the game loop. This should fix the problem with the black screen and display your game correctly.

SouthStick
7 months ago

The reason why your PyGame script only displays a black screen is due to multiple problems in the code, including identification errors and the wrong handling of the

“pygame.draw.rect( )”

Function. I will correct the code and add some improvements to make it work as expected.

Here is the revised code:

import pygame

import sys

# Initialization of pygame

pygame.init()

# Screen and title

screen = pygame.display.set_mode(1280, 720)

pygame.display.set_caption(“Knight Slasher”)

# Color definitions

color = (255, 0, 0)

# Player initialization

player_1 = pygame.Rect(600, 360, 100, 100)

# music box

clock = pygame.time.Clock()

# Main game loop

running = True

while running:

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

# Fill screen

screen.fill(“green”)

# Player movement

keys = pygame.key.get_pressed()

if keys[pygame.K_a]:

player_1.move_ip(-5, 0)

if keys[pygame.K_d]:

player_1.move_ip(5, 0)

if keys[pygame.K_w]:

player_1.move_ip(0, -5)

if keys[pygame.K_s]:

player_1.move_ip(0, 5)

# Drawing the player

pygame.draw.rect(screen, color, player_1)

# Update screen

pygame.display.flip()

# Limit FPS

clock.tick(60)

pygame.quit()