python - 程序不断崩溃(Python + pygame

标签 python crash pygame

我在pygame中制作了一个非常简单的动画,但是它一直使我崩溃,我无法弄清它的生命。我已经看了无数遍了,但没有发现任何错误。它应该工作。

我的代码如下:

import pygame
import random

#consants
SIZE = (800,600)
WHITE = (255,255,255)
BLACK = (0,0,0)
BROWN = (139,69,19)
YELLOW = (255,255,0)
MAX_SNOWFLAKES = 800

#functions
x_sun = -40
y_sun = 55

def sun():
    x_sun = -40
    y_sun = 55    
for i in range(1000000):
    screen.fill(BLACK)
    for i in range(1):
        pygame.draw.circle(screen, YELLOW, (x_sun, y_sun), 40)
        x_sun +=1
        if x_sun > 640:
            x_sun = 0
    pygame.display.flip()
    pygame.time.wait(10)


def snowman(x,y):
    #snowman body
    pygame.draw.circle(screen, WHITE, (x,y), 30)
    pygame.draw.circle(screen, WHITE, (x,y+50),40)
    pygame.draw.circle(screen, WHITE, (x, y+110), 50)

    #snowman arms+hands
    pygame.draw.line(screen, BROWN, (x+40, y+50), (x+75, y+50), 2.5)
    pygame.draw.line(screen, BROWN, (x-40, y+50), (x-75, y+50), 2.5)
    pygame.draw.line(screen, BROWN, (x+70, y+50), (x+75, y+45), 2.5)
    pygame.draw.line(screen, BROWN, (x-70, y+50), (x-75, y+45), 2)
    pygame.draw.line(screen, BROWN, (x-70, y+50), (x-75, y+55), 2)
    pygame.draw.line(screen, BROWN, (x+70, y+50), (x+75, y+55), 2)

    #draws eyes
    pygame.draw.circle(screen, BLACK, (x+10, y-10), 4)
    pygame.draw.circle(screen, BLACK, (x-10, y-10), 4)
    pygame.display.flip()

#x and y list
xlist_snow = []
ylist_snow = []

#display screen
screen = pygame.display.set_mode(SIZE)

#makes x + y list for snowflake
for i in range(MAX_SNOWFLAKES):
    x_num = random.randint(0,MAX_SNOWFLAKES)
    xlist_snow += [x_num]
    y_num = random.randint(0,(MAX_SNOWFLAKES-200))
    ylist_snow += [y_num]

#loop to print out the snowflakes
for i in range(10000):
    screen.fill(BLACK)

    #animates snowflakes by moving them downwards
    for i in range(len(xlist_snow)):
        pygame.draw.circle(screen, WHITE, (xlist_snow[i], ylist_snow[i]), 3)
        ylist_snow[i] = ylist_snow[i]+1

        #resets ypos if it equals 0
        if ylist_snow[i] >= 600:
            ylist_snow[i] = 0


    pygame.draw.circle(screen, YELLOW, (x_sun, y_sun), 40)

    x_sun += 1
    if x_sun > 600:
        x_sun = 0

    #snowman(300,300)

    pygame.display.flip()
    pygame.time.wait(50)




pygame.time.wait(3000)
pygame.quit()

最佳答案

如果您提供给我们的代码是完整的代码,那么您还没有告诉Python什么是BLACK,YELLOW,WHITE等。

如果您从pygame.locals导入所有内容,则可以使用以下命令:

from pygame.locals import *

关于python - 程序不断崩溃(Python + pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20451446/

相关文章:

python - 自修改Python类

java - 我想以编程方式获取 Android 中的应用程序崩溃信息,而不使用第 3 方应用程序或 bugsense 等 jars

python - 尝试在一定时间后延迟生成敌人的特定功能

python - mongodb 或 redis 用于类似 facebook 的网站?

python - 错误解包需要长度为 16 的字符串参数

Python AWS SQS 与 MOTO 模拟

ios - react native ios : App crashes when it sent to the background

c# - WPF4/C# - System.StackOverflowException 崩溃应用程序

Python错误: 'None Type' object does not support item assignment

python - 如何在 Pygame 上用操纵杆同时移动和射击?