为什么 PyGame 会跟踪图像? [英] Why Does PyGame Trail the Image?

查看:35
本文介绍了为什么 PyGame 会跟踪图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 pygame 和 IDLE 在 python 中开发一个简单的游戏.从昨天开始,我浏览了各种资源以了解这门语言(以及一般的编程),即便如此,我还是遇到了一些问题和对其运作方式的误解.因此,如果有人可以就如何继续向我提出建议(或为我指明一些好的学习材料的方向),那么我将不胜感激.

I'm trying to develop a simple game in python using pygame and IDLE. I have, since yesterday, looked through a variety of sources in order to learn about the language (and programming in general), even so I have encountered some problems and misunderstandings of how it all works. So, if anyone could please advise me on how to proceed (or point me in the direction of some good learning material) then I would appreciate it greatly.

到目前为止,我有一小段代码构成了我的游戏创意的基础,所以我会在这里发布它并列出我的一些问题.

So far, I've got a small bit of code that forms the basis of my game idea, so I will post it here and list some of my problems.

import pygame

def main():


pygame.init()


logo = pygame.image.load("coolblack.jpg")
pygame.display.set_icon(logo)
pygame.display.set_caption("Battleship")


screenWidth = 800
screenHeight = 600
screen = pygame.display.set_mode((screenWidth, screenHeight))


bgd_image = pygame.image.load("grid.png")
#--------------------------------------------------------------------
#the image named 'image' should be above 'bgd_image' but below 'cv9'
#in fact, everything should be above bgd_image, especially 'cv9'
#--------------------------------------------------------------------
image = pygame.image.load("coolblack.jpg")
cv9 = pygame.image.load("ussessexcv9.gif").convert_alpha()


xposCv9 = 400
yposCv9 = 510


step_xCv9 = 1
step_yCv9 = 1


screen.blit(bgd_image, (0,0))
screen.blit(image, (400,300))
screen.blit(cv9, (xposCv9, yposCv9))


pygame.display.flip()
clock = pygame.time.Clock()
running = True

#---------------------------------------------
#I've got a pretty good idea (sort of) about
#what is happening in the section
#below this point, however it seems that
#the image 'cv9' creates a trail of itself
#every time it moves, so how could I make it 
#so that it doesn't do so?
#---------------------------------------------
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
    if xposCv9>screenWidth-64 or xposCv9<0:
        step_xCv9 = -step_xCv9
    if yposCv9>screenHeight-64 or yposCv9<0:
        step_yCv9 = -step_yCv9
    xposCv9 += step_xCv9
    yposCv9 += step_yCv9
    screen.blit(cv9, (xposCv9, yposCv9))
    pygame.display.flip()
    clock.tick(60)


if __name__=="__main__":
    main()

推荐答案

pygame 的工作方式是它在内部具有您正在更新的屏幕的表示.所以,它开始完全是黑色的,然后你做你的第一个blit".这将更新内部表示.然后当您调用pygame.display.flip"时,它会在屏幕上显示该表示.但是,这不会自动将您的表示清除"到下一帧的全黑.所以,在下一帧,你再次 blit(比方说稍微向左),第一个 blit 仍然存在,创建你的轨迹".

The way that pygame works is that it has internally a representation of the screen which you are updating. So, it starts entirely black, then you do your first "blit". This will update the internal representation. Then when you call "pygame.display.flip" it shows that representation on the screen. However, this will not automatically "clear" the representation for you back to all black for your next frame. So, on the next frame, you blit again (slightly to the left, say), and the first blit remains, creating your "trail".

因此,对于您正在做的事情,最好的办法是在您的循环中,在开始绘制下一帧之前清除屏幕的内部表示.您可以通过填充单一颜色来清除"屏幕,就像这样......

Therefore, for what you're doing, the best thing would be to in your loop, clear the internal representation of the screen before you start drawing the next frame. You can "clear" the screen by filling it with a single color, like so...

BLACK = (0,0,0)
....
screen.blit(cv9, (xposCv9, yposCv9))
pygame.display.flip()
clock.tick(60)
screen.fill(BLACK) # Add this to "clear" the screen.

请注意,如果您选择走这条路线,这意味着您将需要在每一帧中重绘所有元素(不仅仅是自上一帧以来发生变化的元素).

Note that if you chose to go this route, this means you will need to redraw ALL of the elements every single frame (not just the ones that changed since the last frame).

顺便说一句,如果您想知道,为什么最后没有自动清除帧是有充分理由的.在某些情况下,仅更新屏幕的更新部分可能会更快.这可能会导致某些应用程序的性能加速.但是,最好从清除屏幕开始,如上例所示.

BTW, in case you are wondering, there is a good reason for why the frame is not automatically cleared at the end. In some cases, it might be faster to only update the parts of the screen that update. This can cause performance speedups in some applications. However, it's probably best to start with clearing the screen as the example shows above.

这篇关于为什么 PyGame 会跟踪图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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