简体   繁体   English

如何在Pygame中的矩形框上放置图像?

[英]How to blit an image over a rectangle in pygame?

I am trying to blit a image over rectangle. 我正在尝试在矩形上绘制图像。
Here is my function for creating a rectangle. 这是我创建矩形的功能。

def drawCell(coords,ccolor):
    #Here coords is the coordnnates (x and y axis) for rectangle
    #cclor is colour for the rectangle , white in my program.

    global score
    for coord in coords:
        x=coord['x']*cellSize
        y=coord['y']*cellSize
        makeCell=pygame.Rect(x,y,cellSize,cellSize)
        pygame.draw.rect(gameDisplay,ccolor,makeCell)

        if makeCell.colliderect(foodrect):                          #If the rectangle collides with food
                                                                    #score gets added and the food is blitted
                                                                    #to a random
            foodrect.x = random.randint(0, display_width-25)
            foodrect.y = random.randint(0, display_height-25)
            score+=1
    scoredisplay(score) #Displays Score

This is the function call in game loop: 这是游戏循环中的函数调用:

drawCell(coords,white)

Now what i want it to do is blit this on the rectangle so that it looks like the apple is moving (move function is different and not provided here). 现在,我希望它做的是块传输这个矩形上,这样它看起来像苹果移动(移动功能是不同的,这里不提供)。

当然你可以将gameDisplay.blit(imgname, (x,y)) pygame.draw.rect(gameDisplay,ccolor,makeCell)

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

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