简体   繁体   中英

Why does my Python pygame program close immediately after launch?

I'm trying to make a game using Pygame. But the program closes immediately after it launches.

I followed a tutorial in YT, and copied the function exactly as it was, but still got the error.


Here is the code:

import pygame
import sys
import random as rd

pygame.init()

width = 800
height = 600

red = (255, 0, 0)
black = (0, 0, 0)
blue = (0, 0, 255)

playerPosition = [400, 500]
playerSize = 35

enemySize = 50
enemyPosition = [rd.randint(0, width - enemySize), 0]
enemySpeed = 10

screen = pygame.display.set_mode((width, height))
title = pygame.display.set_caption("Dodge Game by Ishak")


def collision(playerPosition, enemyPosition):
    playerX = playerPosition[0]  # player x coordinate
    playerY = playerPosition[1]  # player y coordinate

    enemyX = enemyPosition[0]  # enemy x coordinate
    enemyY = enemyPosition[1]  # enemy y coordinate

    if (enemyX >= playerX and enemyX < (playerX + playerSize)) or (playerX >= enemyX and playerX < (enemyX + enemySize)):
        if (enemyY >= playerY and enemyY < (playerY + playerSize)) or (playerY >= enemyY and playerY < (enemyY + enemySize)):
            return False
    return True


clock = pygame.time.Clock()

gameOver = False
# game loop
while not gameOver:

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

    # keyboard
    if event.type == pygame.KEYDOWN:

        x = playerPosition[0]
        y = playerPosition[1]

        if event.key == pygame.K_RIGHT:
            x += 13
        elif event.key == pygame.K_LEFT:
            x -= 13

        playerPosition = [x, y]

    if enemyPosition[1] >= 0 and enemyPosition[1] < height:
        enemyPosition[1] += enemySpeed
    else:
        enemyPosition[0] = rd.randint(0, width - enemySize)  # sets a random postion of the enemy
        enemyPosition[1] = 0

    if collision(playerPosition, enemyPosition):
        gameOver = True

    screen.fill(black)

    pygame.draw.rect(screen, red, (playerPosition[0], playerPosition[1], playerSize, playerSize))  # player shape
    pygame.draw.rect(screen, blue, (enemyPosition[0], enemyPosition[1], enemySize, enemySize))  # enemy shape

    clock.tick(30)

    pygame.display.update()

This problem occurred after I added the collision function and implemented it on the main game loop, but I can't figure out what is wrong with it.

This is the only place I see where the loop will stop:

if collision(playerPosition, enemyPosition):
        gameOver = True

So I would predict that you have a collision between your player and enemy when they spawn. To see for sure I would suggest printing the positions of player and enemy to see if they do in fact collide.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM