Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当我打开Pygame时,它总是崩溃_Python_Crash_Pygame - Fatal编程技术网

Python 当我打开Pygame时,它总是崩溃

Python 当我打开Pygame时,它总是崩溃,python,crash,pygame,Python,Crash,Pygame,我有一个躲避外星人的游戏,但它不起作用。我可以打开前开始屏幕,但当我按enter键开始时,它会崩溃并冻结。我试着从python.exe运行它,而不是空闲运行,但在这种情况下,它会弹出然后立即关闭。在我尝试运行它的最初几次时,出现了一些错误,但现在没有错误指示可能的错误。它只是停止响应。我做错了什么 import pygame, random, sys from pygame.locals import * def startGame(): if event.type == K_ENT

我有一个躲避外星人的游戏,但它不起作用。我可以打开前开始屏幕,但当我按enter键开始时,它会崩溃并冻结。我试着从python.exe运行它,而不是空闲运行,但在这种情况下,它会弹出然后立即关闭。在我尝试运行它的最初几次时,出现了一些错误,但现在没有错误指示可能的错误。它只是停止响应。我做错了什么

import pygame, random, sys
from pygame.locals import *


def startGame():
    if event.type == K_ENTER:
        if event.key == K_ESCAPE:
            sys.exit()
        return

def playerCollision():
    for a in aliens:
        if playerRect.colliderect(b['rect']):
            return True
    return False

pygame.init()

screen = pygame.display.set_mode((750,750))
clock = pygame.time.Clock()
pygame.display.set_caption('Dodge the Aliens')

font = pygame.font.SysFont(None, 55)

playerImage = pygame.image.load('')
playerRect = playerImage.get_rect()
alienImage = pygame.image.load('')

drawText('Dodge the Aliens!', font, screen, (750 / 3), (750 / 3))
drawText('Press ENTER to start.', font, screen, (750 / 3) - 45, (750 / 3) +    65)
pygame.display.update()

topScore = 0
while True:
    aliens = []
    score = 0
    playerRect.topleft = (750 /2, 750 - 50)
    alienAdd = 0
    while True:
        score += 1
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_LEFT]: x -=3
        if pressed[pygame.K_RIGHT]: x += 3
        if pressed[pygame.K_ESCAPE]: sys.exit()
    alienAdd += 1

    if alienAdd == addedaliens:
        aliendAdd = 0
        alienSize = random.randint(10, 40)
        newAlien = {'rect': pygame.Rect(random.randint(0, 750 - alienSize), 0 -alienSize, alienSize, alienSize), 'speed': random.randint(1, 8), 'surface':pygame.transform.scale(alienImage, (alienSize, alienSize)), }
        aliens.append(newAlien)
    for a in aliens[:]:
        if a['rect'].top > 750:
            aliens.remove(a)
    screen.fill(0,0,0)
    drawText('Score %s' % (score), font, screen, 10, 0)
    screen.blit(playerImage, playerRect)
    for a in aliens:
        screen.blit(b['surface'], b['rect'])
    pygame.display.update()


    if playerCollision(playerRect, aliens):
        if score > topScore:
            topScore = score
        break

    clock.tick(60)

    drawText('Game Over!', font, screen, (750 / 3), ( 750 / 3))
    drawText('Press ENTER To Play Again.', font, screen, ( 750 / 3) - 80, (750 / 3) + 50)
    pygame.display.update()
    startGame()
这是我修改了一些代码后的新代码 导入pygame、random、sys 从pygame.locals导入* alienimg=pygame.image.load('C:\Python27\alien.png') playerimg=pygame.image.load('C:\Python27\spaceship.png')


我仍然看到您的代码存在一些问题,我认为您在一开始就试图一次完成太多的工作。尽量使它尽可能简单。尝试创建显示并绘制一些图像:

import pygame

pygame.init()

display = pygame.display.set_mode((750, 750))
img = pygame.image.load("""C:\\Game Dev\\alien.png""")
display.blit(img, (0, 0))
pygame.display.flip()
当然,您必须调整img路径。运行这个程序,你要么会得到一个明确的错误(然后你应该在另一个线程中发布),要么会在屏幕上看到你的img。但是程序不会响应,因为根本没有事件处理和主循环

为了避免这种情况,可以引入如下主循环:

import sys
import pygame

pygame.init()

RESOLUTION = (750, 750)
FPS = 60

display = pygame.display.set_mode(RESOLUTION)
clock = pygame.time.Clock()
img = pygame.image.load("""C:\\Game Dev\\alien.png""")

while True:  # <--- game loop

    # check quit program
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    # clear the screen
    display.fill((0, 0, 0))
    # draw the image
    display.blit(img, (0, 0))
    # update the screen
    pygame.display.flip()
    # tick the clock
    clock.tick(FPS)
导入系统 导入pygame pygame.init() 分辨率=(750750) FPS=60 display=pygame.display.set_模式(分辨率) clock=pygame.time.clock() img=pygame.image.load(““C:\\Game Dev\\alien.png”)
虽然是真的:#我可以建议你给@ndoll举个例子吗?谢谢。我还不是python的高手,但我正在努力学习。为了澄清一下,while循环应该在我的主函数中?我将第一个while循环变成了一个主函数,但我仍然有同样的问题:(
import sys
import pygame

pygame.init()

RESOLUTION = (750, 750)
FPS = 60

display = pygame.display.set_mode(RESOLUTION)
clock = pygame.time.Clock()
img = pygame.image.load("""C:\\Game Dev\\alien.png""")

while True:  # <--- game loop

    # check quit program
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    # clear the screen
    display.fill((0, 0, 0))
    # draw the image
    display.blit(img, (0, 0))
    # update the screen
    pygame.display.flip()
    # tick the clock
    clock.tick(FPS)
import sys
import pygame

#defining some constants
RESOLUTION = (750, 750)
FPS = 60

def main(): # <--- program starts here

    # setting things up
    pygame.init()
    display = pygame.display.set_mode(RESOLUTION)
    clock = pygame.time.Clock()
    img = pygame.image.load("""C:\\Game Dev\\alien.png""")

    while True:  # <--- game loop

        # check quit program
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        # clear the screen
        display.fill((0, 0, 0))
        # draw the image
        display.blit(img, (0, 0))
        # update the screen
        pygame.display.flip()
        # tick the clock
        clock.tick(FPS)

if __name__ == "__main__":
    main()