如何从 pygame 中删除图像
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 应用程序循环必须:
- 限制每秒帧数以限制 CPU 使用
pygame.time.Clock.tick
- 通过调用
pygame.event.pump()
or pygame.event.get()
. 来处理事件
- 根据输入事件和时间(分别为帧)更新游戏状态和对象位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象)
- 通过调用
pygame.display.update()
or pygame.display.flip()
更新显示
我试图为我的游戏创建一个自定义菜单,为此,我制作了一个光标,以便用户能够从 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 应用程序循环必须:
- 限制每秒帧数以限制 CPU 使用
pygame.time.Clock.tick
- 通过调用
pygame.event.pump()
orpygame.event.get()
. 来处理事件
- 根据输入事件和时间(分别为帧)更新游戏状态和对象位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象) - 通过调用
pygame.display.update()
orpygame.display.flip()
更新显示