2012-03-24 87 views
1

我使用pygame得到了一个python程序的问题。我想在太阳系中旋转(旋转)。它部分地起作用:在运行程序时关于太阳的一些奇怪的“口吃”问题。这种口吃一次又一次地出现在一个循环中。继承人的代码:”Pygame口吃对象

# -*- 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

外的图片我不能完全肯定,但它似乎像要装入您的每一次循环运行这在很大程度上强化了图像你的CPU

+0

Thx的答案,但如果我把代码加载的图像循环太阳将分裂和旋转真的很奇怪 – ShadowWolf 2012-03-24 11:32:53

+0

我不完全确定它是否会解决您的问题,但尝试传递DOUBLEBUF set_mode()像这样:self.display_surface = pygame.display.set_mode(self.size,pygame .HWSURFACE | pygame.DOUBLEBUF) – 2012-03-24 13:03:37

+0

ShadowWolf:那是因为你正在旋转之前旋转的图像。旋转后的图像会变大,然后旋转*,使其变大,等等。您的图像似乎会分开。 – imallett 2012-03-24 21:37:53

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() 
+0

Thx为答案,但我需要xres和yres将太阳置于屏幕中央 – ShadowWolf 2012-03-24 11:33:45

+0

@ShadowWolf:好的,我刚刚重写了我的答案,以解决您如何加载太阳。 – jdi 2012-03-24 19:20:51

+0

非常感谢:D代码看起来非常好 – ShadowWolf 2012-03-24 20:20:21