我正在使用pygame和python创建游戏。我在窗口中画了一个矩形作为按钮。但我需要知道如何在单击按钮后删除按钮。继承我的代码:
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()
所以,我希望你能帮助我。
答案 0 :(得分:0)
您需要再次将所有内容绘制到屏幕上,除了您不再需要的部分。
pygame.init()
screen = pygame.display.set_mode((700, 700))
# set background or fill
background = pygame.image.load("/background.png")
screen.blit(background, (0, 0))
my_btn = pygame.image.load("/button.png")
btn_rect = my_btn.get_rect()
screen.blit(my_btn, (0, 0))
for evt in pygame.event.get():
if evt.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if btn_rect.collidepoint(pos):
# do stuff
# apply your background fill or image
screen.blit(background, (0, 0))
for element in elements_you_want_on_screen:
screen.blit(element, (location))
pygame.display.flip()
如果您没有要进行blit的曲面阵列:
pygame.init()
screen = pygame.display.set_mode((700, 700))
# set background or fill
background = pygame.image.load("/background.png")
screen.blit(background, (0, 0))
my_btn = pygame.image.load("/button.png")
btn_rect = my_btn.get_rect()
screen.blit(my_btn, (0, 0))
for evt in pygame.event.get():
if evt.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if btn_rect.collidepoint(pos):
# do stuff
# apply your background fill or image
screen.blit(background, (0, 0))
screen.blit(your_image, (location))
# ...and others if you have them
pygame.display.flip()
答案 1 :(得分:0)
我认为您应该为每个帧创建一个空白屏幕,然后重新绘制所有内容...
def draw(self):
self.screen.fill(self.color) #clear screen
self.draw(self.screen) # draw updated screen
如果您的代码中包含不同的类,请尝试也添加该部分:
self.#class#.draw(self.screen)