我想知道在 pygame 中单击按钮时是否可以删除图像
I would like to know if i can remove an image when button is clicked in pygame
我试图在单击 1 个键时删除图像。
import pygame
import pygame.freetype
import random
# Intialise pygame
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) # Fullscreen
卡片
cards = (pygame.image.load('image/london.png'), pygame.image.load('image/america.png'))
def card():
screen.blit(card_size[0], (100, 40))
screen.blit(card_size[1], (330, 40))
游戏循环
程序的循环从这里开始
running = True
while running:
screen.fill((255, 194, 102)) # RGB
for event in pygame.event.get(): # Event handler
if event.type == pygame.QUIT:
running = False
在这里,我尝试从列表中删除项目,但没有成功。
if event.type == pygame.KEYDOWN:
if event.key == ord('1'):
cards.remove()
if event.key == ord('2'):
print('gone')
card()
pygame.display.update()
删除图像意味着不绘制图像。有不同的解决方案。
解决方案 1:
循环绘制图像:
cards = [
pygame.image.load('image/london.png'),
pygame.image.load('image/america.png')]
def card():
for i in range(len(cards)):
screen.blit(cards[i], (100 + i*230, 40))
pop
按键时的图像:
while running:
# [...]
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1 and len(cards) > 0:
cards.pop(0)
if event.key == pygame.K_2 and len(cards) > 1:
cards.pop(1)
解决方案 2:
添加一个列表,指示是否需要绘制图像:
cards_active = [True, True]
def card():
for i in range(len(cards)):
if cards_active[i]:
screen.blit(cards[i], (100 + i*230, 40))
按下某个键时切换列表中的状态:
while running:
# [...]
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1 and len(cards) > 0:
cards_active[0] = not cards_active[0]
if event.key == pygame.K_2 and len(cards) > 1:
cards_active[1] = not cards_active[1]
我试图在单击 1 个键时删除图像。
import pygame
import pygame.freetype
import random
# Intialise pygame
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) # Fullscreen
卡片
cards = (pygame.image.load('image/london.png'), pygame.image.load('image/america.png'))
def card():
screen.blit(card_size[0], (100, 40))
screen.blit(card_size[1], (330, 40))
游戏循环
程序的循环从这里开始
running = True
while running:
screen.fill((255, 194, 102)) # RGB
for event in pygame.event.get(): # Event handler
if event.type == pygame.QUIT:
running = False
在这里,我尝试从列表中删除项目,但没有成功。
if event.type == pygame.KEYDOWN:
if event.key == ord('1'):
cards.remove()
if event.key == ord('2'):
print('gone')
card()
pygame.display.update()
删除图像意味着不绘制图像。有不同的解决方案。
解决方案 1:
循环绘制图像:
cards = [
pygame.image.load('image/london.png'),
pygame.image.load('image/america.png')]
def card():
for i in range(len(cards)):
screen.blit(cards[i], (100 + i*230, 40))
pop
按键时的图像:
while running:
# [...]
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1 and len(cards) > 0:
cards.pop(0)
if event.key == pygame.K_2 and len(cards) > 1:
cards.pop(1)
解决方案 2:
添加一个列表,指示是否需要绘制图像:
cards_active = [True, True]
def card():
for i in range(len(cards)):
if cards_active[i]:
screen.blit(cards[i], (100 + i*230, 40))
按下某个键时切换列表中的状态:
while running:
# [...]
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1 and len(cards) > 0:
cards_active[0] = not cards_active[0]
if event.key == pygame.K_2 and len(cards) > 1:
cards_active[1] = not cards_active[1]