[英]How do you remove an image from pygame
我试图为我的游戏创建一个自定义菜单,为此我制作了一个光标,以便用户能够从 3 个字符中的一个中进行选择,错误是光标的图像被复制到下一个位置和原来的一个留下来,我想知道是否有办法删除原来的光标。
One_Right_Key = pygame.K_RIGHT
# Creating the customisation menu for the game
def Customisation(players):
#Initiating TwoX in case there is no second player to make sure the program works correctly
TwoX = 100
# Placing the background onto the game
background = pygame.image.load('Menu/Customisation Background.png')
screen.blit(background, (0, 0))
# Creating an array for each of the x positions of the 3 options of characters
x = [157, 545, 954]
# Creating the Cursor for the first player
CursorOne = pygame.image.load('Misc/P1 Cursor.png')
CursorOne = pygame.transform.scale(CursorOne, (124, 100))
CursorOne_move = CursorOne.get_rect()
# Creating a variable to give the position of the cursor and placing it onto the screen
OneX = 0
screen.blit(CursorOne, (x[OneX], 61))
# Checking if there is one or two players playing the game
if players == 2:
# Creating the cursor for the second player
CursorTwo = pygame.image.load('Misc/P2 Cursor.png')
CursorTwo = pygame.transform.scale(CursorTwo, (124, 100))
# Creating a variable to give the position of the second cursor
TwoX = 1
screen.blit(CursorTwo, (x[TwoX], 61))
# Creating the back button
Back_Text = BackButtonFont.render("Back", True, (255, 255, 255))
# Placing the text onto the screen
screen.blit(Back_Text, (37, 628))
# Updating the screen
pygame.display.update()
# Keeps the program running
Running = True
while Running == True:
for event in pygame.event.get():
# If the user clicks on the back button
if event.type == pygame.MOUSEBUTTONUP:
mouse = pygame.mouse.get_pos()
if mouse[0] >= 0 and mouse[0] <= 280 and mouse[1] > 611 and mouse[1] <= 719:
# Goes to the Main Menu Procedure
MainMenu()
# Exits the customisation procedure
exit(Customisation())
# If the player presses a key
if event.type == pygame.KEYDOWN:
if event.key == One_Right_Key:
OneX = OneX + 1
# If OneX is the same as TwoX, it moves to the next one
if OneX == TwoX:
OneX = OneX + 1
# If OneX is over, it goes back to the first position
if OneX == 3:
OneX = 0
CursorOne_move.move_ip(x[OneX], 61)
screen.blit(CursorOne, CursorOne_move)
pygame.display.flip()
# If the user clicks on the cross on the top right of the window
if event.type == pygame.QUIT:
exit()
您无法“删除”屏幕上绘制的图像。 您必须在每一帧中重新绘制完整的场景。 删除(或“移除”)一个对象意味着不在下一帧中绘制它。
典型的 PyGame 应用程序循环必须:
pygame.time.Clock.tick
限制每秒帧数以限制 CPU 使用率pygame.event.pump()
或pygame.event.get()
处理事件。blit
所有对象)pygame.display.update()
或pygame.display.flip()
更新显示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.