简体   繁体   English

pygame.draw.rect()没有使另一个矩形消失

[英]pygame.draw.rect() not drwaing another rectangle

I am making a simple graphical game in pygame. 我正在pygame中制作一个简单的图形游戏。 I wanted to draw 2 rectangles but it only draws one not the other. 我想绘制2个矩形,但它只绘制一个,而另一个不绘制。

I made a class of drawing different figures. 我做了一类绘制不同的数字。 To fix that issue I removed the class and made a plain function. 为了解决该问题,我删除了该类并做了一个简单的函数。 Id didn't work too. ID也没有用。 Then I just put the code of function statements in the program. 然后,我只将函数语句的代码放入程序中。

The final code I get is this 我得到的最终代码是

import pygame

black = (0,0,0)
yellow = (200,200,0)

def drawShopButton():
    pygame.draw.rect(gameDisplay, black, (690,435,110,65))
    pygame.display.update()
    pygame.draw.rect(gameDisplay, yellow, (0,500,110,65))
    pygame.display.update()

pygame.init()
gameDisplay = pygame.display.set_mode((800, 500))
gameDisplay.fill((40, 120, 0))
pygame.display.update()
pygame.draw.rect(gameDisplay, black, (690,435,110,65))
pygame.display.update()
clock = pygame.time.Clock()
clock.tick(40)

pygame.draw.rect(gameDisplay, yellow, (0,500,110,65))
pygame.display.update()
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    pygame.display.update()

The left top corner of the game board is (0,0) and the bottom right corner is (800,500). 游戏板的左上角为(0,0),右下角为(800,500)。 So, pygame.draw.rect(gameDisplay, yellow, (0,500,110,65)) is trying to draw a rectangle that is starting at the right top of the board and is (110, 65) wide and tall. 因此, pygame.draw.rect(gameDisplay, yellow, (0,500,110,65))试图绘制一个矩形,该矩形从棋盘的右上角开始,宽pygame.draw.rect(gameDisplay, yellow, (0,500,110,65))高。 Your code is drawing two rectangles alright, but beyond the visibility scope. 您的代码正在绘制两个矩形,但是超出了可见性范围。

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

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