我要向左翻转该兔子,它应该向左翻转并加载相同的动画,但出现此错误
File "C:\Users\Habib\Desktop\PYTHONGGAME\py.py", line 52, in draw
self.playa = pygame.transform.flip(self.playa,True,True)
TypeError: argument 1 must be pygame.Surface, not list
我只希望它翻转相同的动画并执行相同的操作
def draw(self):
self.rect.topleft = (self.x,self.y)
if self.direction == "left":
self.playa = pygame.transform.flip(self.playa,True,True)
window.blit(self.playa[self.so_index],self.rect)
self.so_index += 1
if self.so_index == len(self.playa):
self.so_index = 0
elif self.direction == "right":
window.blit(self.playa[self.to_index],self.rect)
self.to_index += 1
if self.to_index == len(self.playa):
self.to_index = 0
elif self.direction == "standing":
window.blit(self.standing,self.rect)
if self.direction == "Jump":
window.blit(self.jump,self.rect)
我的完整代码
import pygame
pygame.init()
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("Game")
plat = pygame.image.load("gt.png")
coinss = pygame.image.load("coin_gold.png")
bunnystand = pygame.image.load("bunny2_stand.png")
jump = pygame.image.load("bunny2_jump.png")
playa = [pygame.image.load("bunny2_walk2.png"),
pygame.image.load("bunny2_walk2.png"),
]
# player class
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.d_indesx = 0
self.s_index = 0
self.direction = "right"
self.direction = "left"
self.standing = "standing"
self.isJump = False
self.JumpCount = 10
self.speed = 5
self.jump = pygame.image.load("bunny2_jump.png")
self.standing = pygame.image.load("bunny2_stand.png")
self.playa = [
pygame.image.load("bunny2_walk2.png"),
pygame.image.load("bunny2_walk2.png")
]
self.fall = 0
self.so_index = 0
self.do_index = 0
self.to_index = 0
self.direction = "right"
self.direction = "left"
self.direction = "Jump"
self.direction = "standing"
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
if self.direction == "left":
self.playa = pygame.transform.flip(self.playa,True,True)
window.blit(self.playa[self.so_index],self.rect)
self.so_index += 1
if self.so_index == len(self.playa):
self.so_index = 0
elif self.direction == "right":
window.blit(self.playa[self.to_index],self.rect)
self.to_index += 1
if self.to_index == len(self.playa):
self.to_index = 0
elif self.direction == "standing":
window.blit(self.standing,self.rect)
if self.direction == "Jump":
window.blit(self.jump,self.rect)
# platforms
class platform:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.plat = pygame.image.load("gt.png")
self.rect = pygame.Rect(x,y,plat.get_width(), plat.get_height())
self.plat = pygame.transform.scale(self.plat,(self.plat.get_width()//2,self.plat.get_height()//2))
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
window.blit(self.plat,self.rect)
# Coins
class coin:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.coinss = pygame.image.load("coin_gold.png")
self.rect = pygame.Rect(x,y,coinss.get_width(), coinss.get_height())
self.plat = pygame.transform.scale(self.coinss,(self.coinss.get_width()//2,self.coinss.get_height()//2))
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
window.blit(self.coinss,self.rect)
# Floor
class floor:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
font = pygame.font.Font('freesansbold.ttf', 30)
score = 0
text = font.render('Gold = ' + str(score), True, (255,255,255))
textRect = text.get_rect()
textRect.center = (100, 40)
# fps
FPS = 60
clock = pygame.time.Clock()
# colors
Green = (63, 190, 22)
Blue = (22, 190, 175)
white = (240, 240, 240)
# define the enemy player coin classes
playerman = player(40,390,30,30, Blue)
enemy1 = platform(150,390,190,10, Green)
enemy2 = platform(300,310,190,10, Green)
enemy3 = platform(80,260,190,10, Green)
enemy4 = platform(250,180,190,10, Green)
enemy5 = platform(490,120,190,10, Green)
enemy6 = platform(-50,100,190,10, Green)
enemy7 = platform(180,50,190,10, Green)
platforms = [enemy1,enemy2,enemy3,enemy4,enemy5,enemy6,enemy7]
# coin class
coin1 = coin(180,320,50,50, Green)
coin2 = coin(350,250,50,50, Green)
coin3 = coin(150,200,50,50, Green)
Coins_list = [coin1,coin2,coin3]
# floor class
floor1 = floor(-1000,490,9999,50, white)
flories = [floor1]
#main loop
runninggame = True
while runninggame:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
if playerman.y < 250:
playerman.y += 1
for platform in platforms:
platform.y += playerman.speed
for coin in Coins_list:
coin.y += playerman.speed
for floor in flories:
floor.y += playerman.speed
if playerman.y > 450:
playerman.y -= playerman.fall
for platform in platforms:
platform.y -= playerman.fall
for coin in Coins_list:
coin.y -= playerman.fall
for floor in flories:
floor.y -= playerman.fall
keys = pygame.key.get_pressed()
playerman.direction = "standing"
if keys[pygame.K_LEFT]:
playerman.direction = "left"
playerman.x -= playerman.speed
if playerman.x < 100:
playerman.x += playerman.speed
for platform in platforms:
platform.x += playerman.speed
for coin in Coins_list:
coin.x += playerman.speed
if keys[pygame.K_RIGHT]:
playerman.direction = "right"
playerman.x += playerman.speed
if playerman.x > 400:
playerman.x -= playerman.speed
for platform in platforms:
platform.x -= playerman.speed
for coin in Coins_list:
coin.x -= playerman.speed
if not playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
playerman.isJump = False
collide = False
for platform in platforms:
if playerman.rect.colliderect(platform.rect):
collide = True
playerman.isJump = False
playerman.y = platform.rect.top - playerman.height + 1
if playerman.rect.right > platform.rect.left and playerman.rect.left < platform.rect.left - playerman.width:
playerman.x = platform.rect.left - playerman.width
if playerman.rect.left < platform.rect.right and playerman.rect.right > platform.rect.right + playerman.width:
playerman.x = platform.rect.right
for i in range(len(Coins_list)-1,-1,-1):
if playerman.rect.colliderect(Coins_list[i].rect):
del Coins_list[i]
score += 1
text = font.render('Score = ' + str(score), True, (255,255,255))
textRect = text.get_rect()
textRect.center = (100, 40)
for floor in flories:
if playerman.rect.colliderect(floor.rect):
collide = True
playerman.isJump = False
playerman.y = floor.rect.top - playerman.height + 1
if playerman.rect.right > floor.rect.left and playerman.rect.left < floor.rect.left - playerman.width:
playerman.x = floor.rect.left - playerman.width
if playerman.rect.left < floor.rect.right and playerman.rect.right > floor.rect.right + playerman.width:
playerman.x = floor.rect.right
if playerman.rect.bottom >= 500:
collide = True
playerman.isJump = False
playerman.JumpCount = 10
playerman.y = 500 - playerman.height
if collide:
if keys[pygame.K_SPACE]:
playerman.direction = "Jump"
playerman.isJump = True
playerman.fall = 0
else:
if playerman.JumpCount > 0:
playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
playerman.JumpCount -= 1
else:
playerman.JumpCount = 10
playerman.isJump = False
window.fill((74, 107, 104))
window.blit(text,textRect)
for platform in platforms:
platform.draw()
for coin in Coins_list:
coin.draw()
playerman.draw()
for floor in flories:
floor.draw()
pygame.display.update()
pygame.quit()
the
self.playa
is a list of images. it has 2 images called 'bunny2_walk2.png' and the other is the same. Did you want one of them to be flipped? or are you going to add more images to the animation, or was is a typo?.问题是,列表又不是一个表面,而是一个列表,就像试图在打印机上复印一堆纸,然后将整叠纸放入一样,它只是行不通,需要一次做一次,纸叠不是一张纸,而是一张纸的清单,所以
那将翻转列表中的图像,问题是,它将不断翻转每一帧,因此将翻转一帧,然后将其翻转,然后将其翻转,依此类推。因此,如果要使用动画对于两个方向,请执行此操作
这将翻转图像一次,并且不会影响动画中的图像。您还可以创建一个新列表,然后将其翻转即可使用。