Pygame 口吃对象 [英] Pygame stuttering object

查看:63
本文介绍了Pygame 口吃对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 pygame 时遇到了 Python 程序的问题.我想让太阳系中的太阳旋转(旋转).它部分起作用:运行程序时关于太阳的一些奇怪的口吃"的问题.这种口吃一次又一次地循环发生.代码如下:"

I got a problem with a python programm, using pygame. I want to make a sun in a solar system spin (rotate). It works partially: the problem about some strange "stuttering" of the sun when running the programm. This stuttering occurs again and again, in a loop. Heres the code:"

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

推荐答案

我根据你的评论和浏览了 pygame 文档后重写了我的答案

您确实只需要加载一次图像.我在这里所做的总是使用增量旋转将最初加载的太阳旋转到新的表面.旋转总是回绕到 0 度.至于抖动,我确信这与混叠伪影甚至您的居中数学有关.但我的目标是解决最大的性能问题,即每帧(每秒 30 次)从磁盘读取太阳图像

You really do only need to load your images once. What I am doing here is always rotating the originally loaded sun to a new surface, using the incrementing rotation. The rotation always wraps back to 0 degrees. As for the jitter, I am sure that related to an aliasing artifact and maybe even your centering math. But my goal was to address the biggest performance hit which was reading your sun image from disk every frame (30 times a second)

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

这篇关于Pygame 口吃对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆