【问题标题】:Screen not refreshing in pygame [duplicate]pygame中的屏幕不刷新[重复]
【发布时间】:2021-10-30 04:01:53
【问题描述】:

所以我是 python 新手,我正在编写代码。该代码应该使螺旋计移动,并且我几乎完成了最后一步,但是当我运行它时,即使我通过打印出坐标确定圆圈正在移动,屏幕也不会刷新。我真的不知道发生了什么,因为我确定我拼写正确。有什么帮助吗?

import pygame
import math
import sys
import time
#setting colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255,  0)
RED = (255, 0, 0)
ORANGE = (255, 127, 0)
YELLOW = (255, 255, 0)
PURPLE = (160, 32, 240)
#setting what order the colors go in
listCircleColor = (RED, BLUE, GREEN, ORANGE, YELLOW, PURPLE, WHITE)
#how many circles per color
intGroup = 5
#the space between each circle
turnangle = 360/35
#width of screen
width = 600
#height of screen
height = 600
#radius of circles
radius = 100
#making the screen
screen = pygame.display.set_mode((width, height))
#if the code is running, then continue
running = True
##.draw.circle(screen, BLUE, (0, 0), radius, width=2)
circles = []

#draw
alpha = turnangle
for i in range(intGroup):
    for cl in listCircleColor:
        surfacetemp = pygame.Surface((width, height))

        ##circlerect = pygame.rect
        if alpha > 0 and alpha < 90:
            circlerect = pygame.draw.circle(surfacetemp, cl, (300 + radius * math.cos(math.radians(alpha)), 300 + radius * math.sin(math.radians(alpha))), radius, width=2)
            # second quarter of circles
        if alpha > 90 and alpha < 180:
            circlerect = pygame.draw.circle(surfacetemp, cl, (300 - radius * math.cos(math.radians(180 - alpha)), 300 + radius * math.sin(math.radians(180 - alpha))), radius, width=2)
            # third quarter of circles
        if alpha > 180 and alpha < 270:
            circlerect = pygame.draw.circle(surfacetemp, cl, (300 - radius * math.cos(math.radians(270 - alpha)), 300 - radius * math.sin(math.radians(270 - alpha))), radius, width=2)
            # last quarter of circles
        if alpha > 270 and alpha < 360:
            circlerect = pygame.draw.circle(surfacetemp, cl, (300 + radius * math.cos(math.radians(360 - alpha)), 300 - radius * math.sin(math.radians(360 - alpha))), radius, width=2)

        alpha = alpha + turnangle
        ##circles.append(circlerect)
        circles.append(surfacetemp)

#move"

#exit only when user clicks on exit button
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()


    for crect in circles:
        ret = crect.get_rect()
        ret.right += 5
        ret.left += 5

        screen.blit(screen, ret)

    ##screen.blit(crect,crect)
    pygame.time.Clock().tick(20)
     pygame.display.update()

##for center, color in circles:
##    pygame.draw.circle(screen, color, center, radius, 2)
##pygame.display.flip()

【问题讨论】:

  • 请先修复代码,缩进级别错误。例如,带有 pygame.quit() 的行与前面的 if 处于同一级别。无论如何,它看起来好像你没有画任何东西。

标签: python pygame pygame2


【解决方案1】:

有很多代码有错误的缩进:

if alpha > 0 and alpha < 90:
            circlerect = pygame.draw.circle(screen, cl, (300 + radius * 
math.cos(math.radians(alpha)), 300 + radius * math.sin(math.radians(alpha))), radius, width=2)

和

if alpha > 90 and alpha < 180:
            circlerect = pygame.draw.circle(screen, cl, (300 - radius * 
math.cos(math.radians(180 - alpha)), 300 + radius * math.sin(math.radians(180 - alpha))), 
radius, width=2)

和

if alpha > 180 and alpha < 270:
        circlerect = pygame.draw.circle(screen, cl, (300 - radius * math.cos(math.radians(270 - alpha)), 300 - radius * math.sin(math.radians(270 - alpha))), radius, width=2)
    # last quarter of circles
if alpha > 270 and alpha < 360:
    circlerect = pygame.draw.circle(screen, cl, (300 + radius * math.cos(math.radians(360 - alpha)), 300 - radius * math.sin(math.radians(360 - alpha))), radius, width=2)

和

if event.type == pygame.QUIT:
    pygame.quit()
    exit()

修复这些部分的缩进,以便我们可以看到问题所在。

【讨论】:

    【解决方案2】:

    代码中有很多问题,主要是缩进,正如Dhrumil Dave 所提到的,但是要修复显示不更新的问题,这是您需要做的。而不是这个

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    
    "for circle in circles:"
    "print(circle[0][0])"
    "print(circle[0][1])"
    "time.sleep(50)"
    "circle[0][0] += 1"
    
    for crect in circles:
        crect.right += 20
    
       ## screen.fill(0)
        ##for center, color in circles:
        ##    pygame.draw.circle(screen, color, center, radius, 2)
        ##pygame.display.flip()
        pygame.display.update()
    

    您需要将pygame.display.update() 移动到while running 循环内,像这样

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        pygame.display.update()
    

    我也会考虑实现一个时钟来标准化帧速率,就像这样......

    FPS = 60
    clock = pygame.time.Clock()
    running = True
    while running:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        pygame.display.update()
    

    如果你这样做,游戏每秒只会更新 60 次(while 循环每秒只会运行 60 次)。这将使许多动作的速度标准化,并在游戏更加完整时提供显着帮助。您还可以查看称为 delta time 的内容,您可以了解如何操作 here。

    【讨论】:

    • 谢谢!我试过了,但屏幕仍然没有刷新。我该怎么办?
    • 运行程序时,最初的圆圈是否会在屏幕上绘制?
    • 是的,但他们不动
    • 尝试将draw rect命令移动到while running循环中,这样每次都会绘制
    • 好的。另外,我对代码进行了更改。当我尝试将 rect 命令移动到运行时,它只是给了我一个空白屏幕
    猜你喜欢
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2019-08-09
    • 1970-01-01
    相关资源
    最近更新 更多