My code shows a black screen

when I run my cod in pygame it shows a black screen

Heres my code

import pygame
import random

# Initialize pygame
pygame.init()

# Set up the game window
screen_width = 700
screen_height = 500
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("2D Shooter")

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# Player
player_width = 50
player_height = 50
player_x = screen_width // 2 - player_width // 2
player_y = screen_height - player_height - 10
player_speed = 1
player_hp = 3

# Bullets
bullet_width = 3
bullet_height = 15
bullet_color = WHITE
bullet_speed = 8
bullets = []

# Enemy
enemy_width = 40
enemy_height = 40
enemy_color = RED
enemy_speed = 0.1
enemies = []
enemy = []

# Score
score = 0
score_font = pygame.font.SysFont(None, 24)

# Game loop
running = True
game_over = False

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_UP:
            bullet_x = player_x + player_width // 2 - bullet_width // 2
            bullet_y = player_y
            bullets.append([bullet_x, bullet_y])

            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT] and player_x > 0:
                player_x -= player_speed
                if keys[pygame.K_RIGHT] and player_x < screen_width - player_width:
                    player_x += player_speed
                for enemy in enemies:
                    enemy_x, enemy_y = enemy
                # Collision detection between player and enemies
                if (
                    player_x < enemy_x + enemy_width
                    and player_x + player_width > enemy_x
                    and player_y < enemy_y + enemy_height
                    and player_y + player_height > enemy_y
                ):
                    # Player-enemy collision detected
                    player_hp -= 1
if player_hp <= 0:
    game_over = True
if game_over:
    screen.fill(BLACK)
    # Display game over screen and handle restart/quit logic
    restart_text = score_font.render("Press R to restart", True, WHITE)
    quit_text = score_font.render("Press Q to quit", True, WHITE)

    screen.blit(restart_text, (screen_width // 2 - 95, screen_height // 2 + 50))
    screen.blit(quit_text, (screen_width // 2 - 75, screen_height // 2 + 100))
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                running = False
            elif event.key == pygame.K_r:
                player_hp = 3
                score = 0
                game_over = False
                enemies.clear()
                bullets.clear()
else:
    for enemy in enemies:
        enemy_x, enemy_y = enemy
        # Collision detection between bullets and enemies
        for bullet in bullets:
            bullet_x, bullet_y = bullet
            if (
                bullet_x >= enemy_x
                and bullet_x <= enemy_x + enemy_width
                and bullet_y >= enemy_y
                and bullet_y <= enemy_y + enemy_height
            ):
                # Bullet-enemy collision detected
                bullets.remove(bullet)
                enemies.remove(enemy)
                score += 1
        pygame.draw.rect(
            screen, enemy_color, (enemy_x, enemy_y, enemy_width, enemy_height)
        )
        enemy_y += enemy_speed
        enemy[1] = enemy_y
        if enemy_y > screen_height:
            enemies.remove(enemy)
    # Render and display score
    score_text = score_font.render("Score: " + str(score), True, WHITE)
    screen.blit(score_text, (10, 10))

    # Render and display player_hp
    player_hp_text = score_font.render("Player HP: " + str(player_hp), True, WHITE)
    screen.blit(player_hp_text, (10, 50))

    pygame.display.flip()
# waint until user quits
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

link is here https://replit.com/@galhotrarishit/shooter#main.py

The main logic of the game is outside the main loop. You should put the code from lines 49 to 131 instead of lines 137 to 139.

3 Likes

and know it just closes instantly

It still dosent work

I might be wrong but it still doesnt work

You put all of your code outside the game loop, so all of that only runs once, in less than a second.
You have to put a lot of your code, the code that needs to be run for a while, inside the while loop.

3 Likes

You didn’t do what I told you to do.

2 Likes

What am i supposed to do then?

I’d assume what Alex suggested:

1 Like

I made a new repl and now it runs the code immidtly but then closes

God where is my grammer?

Never mind I fixed it

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.