简体   繁体   English

pygame不填充背景怎么移动角色

[英]How to move character in pygame without filling background

My game has a game board that the programme initially sets up based on the user's desired dimensions.我的游戏有一个游戏板,程序最初根据用户所需的尺寸设置该游戏板。 It looks like this:它看起来像这样:

def drawGrid():
    global BLOCKSIZE
    BLOCKSIZE = 40 #Set the size of the grid block - should be evenly divisible by window size
    for x in range(windowSize[0]):
        for y in range(windowSize[1]):
            rect = pygame.Rect(x * BLOCKSIZE, y * BLOCKSIZE, BLOCKSIZE, BLOCKSIZE)
            pygame.draw.rect(SCREEN, WHITE, rect, 1)

In the game loop, I want to move an image but without filling the screen with black each time (as then I would have to call drawGrid() many times, and is not efficient).在游戏循环中,我想移动图像,但不想每次都用黑色填充屏幕(因为那时我必须多次调用 drawGrid(),而且效率不高)。 How could I do this?我怎么能这样做?

Edit: The image fits in a square.编辑:图像适合正方形。 I also need a pygame.draw.circle() to move along the lines of the grid.我还需要一个 pygame.draw.circle() 来沿着网格线移动。

Moved my comment into an answer to help others who may come across this problem.将我的评论移到答案中,以帮助可能遇到此问题的其他人。

A solution would be just to draw the grid to another Surface and blit that to the screen every frame.一个解决方案是将网格绘制到另一个 Surface,然后每帧将其 blit 到屏幕。 This could be implemented most easily by making a copy of the screen with screenCopy = screen.copy() after drawing the grid and saving it to another variable to blit later.这可以通过在绘制网格后使用screenCopy = screen.copy()制作屏幕副本并将其保存到另一个变量以便稍后进行 blit 来最容易地实现。 Just make sure to re-draw the grid surface when the window size or grid size changes.只要确保在 window 大小或网格大小更改时重新绘制网格表面即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM