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

查看:116
本文介绍了为什么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,从而创建了"trail".

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天全站免登陆