我需要我的角色图像在到达某个坐标时发生变化
I need my character image to change when it hits a certain coordinate
import pygame #importing pygame onto this program
pygame.init() #initialising the pygame
display_width = 1000 #making the width for the window 800
display_height = 700 #making the height for the window 600
gameDisplay = pygame.display.set_mode((display_width,display_height)) #storing the
width and height of the window onto a variable
pygame.display.set_caption('The Dark Knight') #setting the name for the window as
the dark knight
background = pygame.image.load('/Users/arnav/Downloads/GothamCity.jpg')
appwallpaper = pygame.image.load('/Users/arnav/Downloads/AppWallpaper.jpeg') #setting
the apps wallpaper storing it in a variable
pygame.display.set_icon(appwallpaper) #setting the apps wallpaper as the poster
Joker = pygame.image.load('/Users/arnav/Downloads/Joker.png') #loading the image of
the joker and storing it in a variable
JokerX = 1000
JokerY = 400
JokerX_change = 0
JokerY_change = 0
def TheJoker(x,y):
gameDisplay.blit(Joker,(x,y))
#looping the game
running = True
while running:
gameDisplay.blit(background, (0,0))
for event in pygame.event.get(): #storing every event in pygame under event
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
JokerX_change = -1
if event.key == pygame.K_RIGHT or event.key == ord('d'):
JokerX_change = 1
if event.key == pygame.K_UP or event.key == ord('w'):
JokerY_change = -1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == ord('a') or event.key ==
pygame.K_RIGHT or event.key == ord('d') or event.key == pygame.K_UP or event.key==
ord('w'):
JokerX_change = 0
JokerY_change = 0
if event.key == pygame.K_UP or event.key == ord('w'):
JokerY_change = 1
JokerX += JokerX_change
JokerY += JokerY_change
spriteWidth, spriteHeight = Joker.get_rect().size
JokerX = min(max(JokerX, 0), display_width - spriteWidth)
JokerY = min(max(JokerY, 150), display_height - spriteHeight)
TheJoker(JokerX,JokerY)
pygame.display.update()
pygame.quit()
quit()
我需要我的角色(小丑)在到达屏幕末尾时反转。反转的 png 是“JokerInverted.png”。屏幕末端的 X 坐标为 0。我还需要它在 X 坐标 850 处恢复正常。我正在考虑使用 if 语句,例如 if JokerX == 0: reload the image 但我没有知道重新加载图像的代码。
加载 2 张图片。引入一个新变量(currentJokerImg
),指示通配符 Joker 的当前表示:
jokerImg = pygame.image.load('/Users/arnav/Downloads/Joker.png')
jokerInvertImg = pygame.image.load('/Users/arnav/Downloads/JokerInverted.png')
currentJokerImg = jokerImg
在TheJoker
函数中使用currentJokerImg
代替Joker
:
def TheJoker(x,y):
gameDisplay.blit(currentJokerImg,(x,y))
现在您可以随时更改 currentJokerImg
。例如:
while running:
# [...]
JokerX += JokerX_change
JokerY += JokerY_change
if JokerX <= 0:
currentJokerImg = jokerInvertImg
elif JokerX >= 850:
currentJokerImg = jokerImg
# [...]
import pygame #importing pygame onto this program
pygame.init() #initialising the pygame
display_width = 1000 #making the width for the window 800
display_height = 700 #making the height for the window 600
gameDisplay = pygame.display.set_mode((display_width,display_height)) #storing the
width and height of the window onto a variable
pygame.display.set_caption('The Dark Knight') #setting the name for the window as
the dark knight
background = pygame.image.load('/Users/arnav/Downloads/GothamCity.jpg')
appwallpaper = pygame.image.load('/Users/arnav/Downloads/AppWallpaper.jpeg') #setting
the apps wallpaper storing it in a variable
pygame.display.set_icon(appwallpaper) #setting the apps wallpaper as the poster
Joker = pygame.image.load('/Users/arnav/Downloads/Joker.png') #loading the image of
the joker and storing it in a variable
JokerX = 1000
JokerY = 400
JokerX_change = 0
JokerY_change = 0
def TheJoker(x,y):
gameDisplay.blit(Joker,(x,y))
#looping the game
running = True
while running:
gameDisplay.blit(background, (0,0))
for event in pygame.event.get(): #storing every event in pygame under event
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
JokerX_change = -1
if event.key == pygame.K_RIGHT or event.key == ord('d'):
JokerX_change = 1
if event.key == pygame.K_UP or event.key == ord('w'):
JokerY_change = -1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == ord('a') or event.key ==
pygame.K_RIGHT or event.key == ord('d') or event.key == pygame.K_UP or event.key==
ord('w'):
JokerX_change = 0
JokerY_change = 0
if event.key == pygame.K_UP or event.key == ord('w'):
JokerY_change = 1
JokerX += JokerX_change
JokerY += JokerY_change
spriteWidth, spriteHeight = Joker.get_rect().size
JokerX = min(max(JokerX, 0), display_width - spriteWidth)
JokerY = min(max(JokerY, 150), display_height - spriteHeight)
TheJoker(JokerX,JokerY)
pygame.display.update()
pygame.quit()
quit()
我需要我的角色(小丑)在到达屏幕末尾时反转。反转的 png 是“JokerInverted.png”。屏幕末端的 X 坐标为 0。我还需要它在 X 坐标 850 处恢复正常。我正在考虑使用 if 语句,例如 if JokerX == 0: reload the image 但我没有知道重新加载图像的代码。
加载 2 张图片。引入一个新变量(currentJokerImg
),指示通配符 Joker 的当前表示:
jokerImg = pygame.image.load('/Users/arnav/Downloads/Joker.png')
jokerInvertImg = pygame.image.load('/Users/arnav/Downloads/JokerInverted.png')
currentJokerImg = jokerImg
在TheJoker
函数中使用currentJokerImg
代替Joker
:
def TheJoker(x,y):
gameDisplay.blit(currentJokerImg,(x,y))
现在您可以随时更改 currentJokerImg
。例如:
while running:
# [...]
JokerX += JokerX_change
JokerY += JokerY_change
if JokerX <= 0:
currentJokerImg = jokerInvertImg
elif JokerX >= 850:
currentJokerImg = jokerImg
# [...]