我使用pygame遇到了python程序的问题。我想让太阳系中的太阳旋转(旋转)。它部分工作:在运行程序时出现一些奇怪的“断断续续”的太阳问题。这种口吃在一个循环中一次又一次地发生。下面是代码:“
# -*- coding: utf-8 -*-
import pygame as pg
import pygame.locals as local
import sys
def rot_center(image, angle):
"""rotate an image while keeping its center and size"""
orig_rect = image.get_rect()
rot_image = pg.transform.rotate(image, angle)
rot_rect = orig_rect.copy()
rot_rect.center = rot_image.get_rect().center
rot_image = rot_image.subsurface(rot_rect).copy()
return rot_image
pg.init()
#deklaration
xres=0
yres=0
#auflösungseinstellungen
try:
xres=int(sys.argv[1]) #auflösung von der kommandozeile, parameter 1
yres=int(sys.argv[2]) #auflösung von der kammondozeile, parameter 2
except IndexError:
xres=800
yres=600
screen = pg.display.set_mode((xres,yres)) #coords nicht hart coden, variablen nutzen
pg.display.set_caption("future rpg prepreprepalphawhatever")
pg.mouse.set_visible(1)
pg.key.set_repeat(1,30)
clock = pg.time.Clock()
running = 1
rotation_stat = 0.0
while running:
planet01 = pg.image.load("grafik/menu/planet02.png")
planet01.set_colorkey((251,0,250), local.RLEACCEL) #load planet01
sun = pg.image.load("grafik/menu/sun.png") #load sun
bg = pg.image.load("grafik/menu/bg.png") #load background
#den hintergrund skalieren, falls auflösung zu hoch
sizedbg = pg.transform.smoothscale(bg, (xres, yres))
rotation_stat += 1
clock.tick(30)
screen.fill((0,0,0))
screen.blit(sizedbg, (0,0))
screen.blit(planet01, (xres/5-planet01.get_width()/2,yres/2-planet01.get_height()/2))
orig_rect = sun.get_rect()
sun = pg.transform.rotate(sun, rotation_stat)
screen.blit(sun, (xres/2-sun.get_width()/2,yres/2-sun.get_height()/2))
rot_rect = orig_rect.copy()
rot_rect.center = sun.get_rect().center
sun = sun.subsurface(rot_rect).copy()
for event in pg.event.get():
if event.type == local.QUIT:
running = 0
if event.type == local.KEYDOWN:
if event.key == local.K_ESCAPE:
pg.event.post(pg.event.Event(local.QUIT))
pg.display.flip()
答案 0 :(得分:0)
将用于加载图像的代码放在循环之外
答案 1 :(得分:0)
我不完全确定,但似乎每次循环运行时都会加载图像,这对CPU来说是非常密集的
答案 2 :(得分:0)
我根据你的评论改写了我的答案,并在浏览了pygame文档后
你真的只需要加载一次图像。我在这里做的总是使用递增旋转将原始加载的太阳旋转到新表面。旋转总是回绕到0度。至于抖动,我确信这与锯齿伪像相关,甚至可能是你的居中数学。但我的目标是解决每一帧(每秒30次)从磁盘读取太阳图像的最大性能影响
import pygame as pg
import pygame.locals as local
import sys
def main():
pg.init()
xres=800
yres=600
try:
xres=int(sys.argv[1])
yres=int(sys.argv[2])
except IndexError:
pass
screen = pg.display.set_mode((xres,yres))
pg.display.set_caption("future rpg prepreprepalphawhatever")
pg.mouse.set_visible(1)
pg.key.set_repeat(1,30)
clock = pg.time.Clock()
rotation_stat = 0.0
planet01 = pg.image.load("earth.png")
planet01.set_colorkey((251,0,250), local.RLEACCEL) #load planet01
sun = pg.image.load("sun.png") #load sun
bg = pg.image.load("bg.png") #load background
sizedbg = pg.transform.smoothscale(bg, (xres, yres))
planet_pos = (xres/5-planet01.get_width()/2,yres/2-planet01.get_height()/2)
running = 1
xres_cent, yres_cent = xres/2, yres/2
while running:
clock.tick(30)
for event in pg.event.get():
if event.type == local.QUIT:
running = 0
if event.type == local.KEYDOWN:
if event.key == local.K_ESCAPE:
pg.event.post(pg.event.Event(local.QUIT))
rotation_stat += 1
if rotation_stat % 360 == 0:
rotation_stat = 0.0
screen.blit(sizedbg, (0,0))
screen.blit(planet01, planet_pos)
sun_rect = sun.get_rect()
sun_rotated = pg.transform.rotate(sun, rotation_stat)
center = sun_rotated.get_rect().center
sun_pos = (xres_cent-center[0], yres_cent-center[1])
screen.blit(sun_rotated, sun_pos)
pg.display.flip()
pg.quit()
if __name__ == "__main__":
main()