如何从pygame窗口中删除绘图对象? [英] how to remove draw objects from pygame window?

查看:189
本文介绍了如何从pygame窗口中删除绘图对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pygame 和 python 创建游戏.我在窗口中绘制了一个矩形,用作按钮.但是我需要知道如何在单击按钮后将其删除.这是我的代码:

I am creating a game using pygame and python. I have drawn a rectangle in the window which acts as a button. But i need to know how to remove the button once it is clicked. Heres my code:

import pygame, sys
from pygame.locals import *
x = 0
y = 0

#Basic stuff:
pygame.init()
screen=pygame.display.set_mode((640,360),0,32)
pygame.display.set_caption("Some random Title")


while True:
    evc = pygame.event.get()
    for event in evc:
        if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
x,y = pygame.mouse.get_pos()
screen.lock()
#draw button:
my_rect = pygame.draw.rect(screen,(205,201,201),Rect((245,40),(130,80)))
bf1 = pygame.font.SysFont("monospace", 15)
bl = bf1.render("Play!!!", 1, (255, 255, 255))
screen.unlock()
#Check mouse click!!!
if my_rect.collidepoint(x,y):
   for event in evc:
       if event.type ==pygame.MOUSEBUTTONUP:
           screen.fill((255,255,255))



screen.blit(bl, (280, 70))
pygame.display.flip
pygame.display.update()

所以是的,我希望你能帮助我.

So yea i hope you can help me with this.

推荐答案

我认为你应该为每一帧创建一个空白屏幕,然后重新绘制所有内容...

i think you should just create a blank screen for every frame and draw everything over again...

def draw(self):
self.screen.fill(self.color) #clear screen
self.draw(self.screen) # draw updated screen

如果您的代码中有不同的类,请尝试添加该部分:

if you have different classes in your code try adding that part in too:

self.#class#.draw(self.screen)

这篇关于如何从pygame窗口中删除绘图对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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