pygame 屏幕不刷新
pygame screen not refreshing
所以我是 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()
你 blit 正确 screen
但你 blit 错误的对象。
您应该使用 crect
而不是 screen
作为第一个参数
screen.blit(crect, ret)
但你有
screen.blit(screen, ret)
所以你 blit black screen
在 black screen
Surface
默认使用 24 位颜色 R,G,B
并且它有黑色背景所以你的代码只显示一个圆圈因为其他圆圈在其他表面的黑色背景后面。
要制作透明背景,您必须将表面转换为 32 位颜色R,G,B,Alpha
surfacetemp = surfacetemp.convert_alpha()
并用 transparent
颜色填充背景 - 最后一个值 (alpha
) 必须是 0
,其他值无关紧要(如果你不需要半透明颜色)
surfacetemp.fill((0,0,0,0))
现在你可以看到所有的圈子了。
还有其他问题。
Surface
可以保留图像但不能保留位置。 surface.get_rect()
总是只给出 (0, 0, width, height)
。如果你想动画那么你需要另一个列表来保持位置 - 你可以使用 pygame.Rect()
来实现这个。
一开始我用所有 get_rect()
创建 circles_rect
,后来我用这个列表改变位置并 blit 它。
动画还需要在每个循环中清除屏幕以删除旧位置的圆圈。我使用 screen.fill((0,0,0))
绘制黑色背景。
如果您更改 rec.right += 5
则不必更改 rec.left
因为它会自动更改(与 rec.centerx
相同)
运行
rec.right += 5
rec.left += 5
给出与
相同的结果
rec.right += 10
编辑:
您不必针对不同的季度进行不同的计算。
import pygame
import math
# 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))
# .draw.circle(screen, BLUE, (0, 0), radius, width=2)
circles = []
circles_rect = []
# draw
alpha = turnangle
for i in range(intGroup):
for cl in listCircleColor:
surfacetemp = pygame.Surface((width, height))
surfacetemp = surfacetemp.convert_alpha()
surfacetemp.fill((0,0,0,0))
x = 300 + radius * math.cos(math.radians(alpha))
y = 300 + radius * math.sin(math.radians(alpha))
circlerect = pygame.draw.circle(surfacetemp, cl, (x, y), radius, width=2)
alpha = alpha + turnangle
circles.append(surfacetemp)
circles_rect.append(surfacetemp.get_rect())
# move
# exit only when user clicks on exit button
# if the code is running, then continue
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#running = False
screen.fill((0,0,0)) # remove previous circles
for crect, ret in zip(circles, circles_rect):
ret.right += 5
#ret.left += 5
screen.blit(crect, ret)
result = clock.tick(25)
fps = clock.get_fps()
text = f'FRAME TIME: {result:.02f} ms | FPS: {fps:.02f}'
pygame.display.set_caption(text)
#print(text)
#pygame.display.update()
pygame.display.flip()
所以我是 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()
你 blit 正确 screen
但你 blit 错误的对象。
您应该使用 crect
而不是 screen
作为第一个参数
screen.blit(crect, ret)
但你有
screen.blit(screen, ret)
所以你 blit black screen
在 black screen
Surface
默认使用 24 位颜色 R,G,B
并且它有黑色背景所以你的代码只显示一个圆圈因为其他圆圈在其他表面的黑色背景后面。
要制作透明背景,您必须将表面转换为 32 位颜色R,G,B,Alpha
surfacetemp = surfacetemp.convert_alpha()
并用 transparent
颜色填充背景 - 最后一个值 (alpha
) 必须是 0
,其他值无关紧要(如果你不需要半透明颜色)
surfacetemp.fill((0,0,0,0))
现在你可以看到所有的圈子了。
还有其他问题。
Surface
可以保留图像但不能保留位置。 surface.get_rect()
总是只给出 (0, 0, width, height)
。如果你想动画那么你需要另一个列表来保持位置 - 你可以使用 pygame.Rect()
来实现这个。
一开始我用所有 get_rect()
创建 circles_rect
,后来我用这个列表改变位置并 blit 它。
动画还需要在每个循环中清除屏幕以删除旧位置的圆圈。我使用 screen.fill((0,0,0))
绘制黑色背景。
如果您更改 rec.right += 5
则不必更改 rec.left
因为它会自动更改(与 rec.centerx
相同)
运行
rec.right += 5
rec.left += 5
给出与
相同的结果rec.right += 10
编辑:
您不必针对不同的季度进行不同的计算。
import pygame
import math
# 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))
# .draw.circle(screen, BLUE, (0, 0), radius, width=2)
circles = []
circles_rect = []
# draw
alpha = turnangle
for i in range(intGroup):
for cl in listCircleColor:
surfacetemp = pygame.Surface((width, height))
surfacetemp = surfacetemp.convert_alpha()
surfacetemp.fill((0,0,0,0))
x = 300 + radius * math.cos(math.radians(alpha))
y = 300 + radius * math.sin(math.radians(alpha))
circlerect = pygame.draw.circle(surfacetemp, cl, (x, y), radius, width=2)
alpha = alpha + turnangle
circles.append(surfacetemp)
circles_rect.append(surfacetemp.get_rect())
# move
# exit only when user clicks on exit button
# if the code is running, then continue
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#running = False
screen.fill((0,0,0)) # remove previous circles
for crect, ret in zip(circles, circles_rect):
ret.right += 5
#ret.left += 5
screen.blit(crect, ret)
result = clock.tick(25)
fps = clock.get_fps()
text = f'FRAME TIME: {result:.02f} ms | FPS: {fps:.02f}'
pygame.display.set_caption(text)
#print(text)
#pygame.display.update()
pygame.display.flip()