我开始用Python中的PyGame创建一个新游戏,我把我的精灵的所有图像都放在两个列表中,所以当我用箭头移动精灵时,它会读取包含精灵图像的列表,它会产生真实效果(因为我们会看到精灵的腿在移动(。问题是显然我们不能对两个列表中的图像使用self.rect.x
和self.rect.y
|错误:AttributeError: 'list' object has no attribute 'x'
这是我的代码:
文件:main.py
import os
from player import Player
import pygame
pygame.init()
current_path = os.path.dirname(__file__)
image_path = os.path.join(current_path, 'images')
screen = pygame.display.set_mode((1280,720))
pygame.display.set_caption("Shoot The Villains")
persoDroit = pygame.image.load(os.path.join(image_path, 'Sprite_soldatDroitSansFeu.png'))
enemyRight = pygame.image.load(os.path.join(image_path, 'Sprite_ennemiDroit.png'))
enemyLeft = pygame.image.load(os.path.join(image_path, 'Sprite_ennemiGauche.png'))
backgroundGame = pygame.image.load(os.path.join(image_path, 'background.png'))
def affichage(health_Player, health_Enemy):
global countSteps
if player.countSteps >= 4:
player.countSteps = 0
if left:
screen.blit(player.go_left[player.countSteps], player.rect.x) #Here I tried to call rect.x, in __init__ and move_left function, it doesn't work
player.countSteps += 1
elif right:
screen.blit(player.go_right[player.countSteps], (player.x,player.y))
player.countSteps += 1
else:
screen.blit(persoDroit, (player.x,player.y))
player = Player()
palying = True
while playing:
screen.blit(backgroundGame, [0,0])
for event in pygame.event.get():
touche = pygame.key.get_pressed()
if event.type == pygame.QUIT or touche[pygame.K_ESCAPE]:
playing = False
if touche[pygame.K_LEFT] and player.rect.x > 0:
left = True
right = False
player.move_left() #I tried to call the function move_left in the Player() class
elif touche[pygame.K_RIGHT] and player.x < screen.get_width()-64:
left = False
right = True
player.x += player.speed
else:
left = False
right = False
player.countSteps = 0
display(player.health_Player, health_Enemy)
pygame.display.update()
pygame.quit()
第二个文件:player.py
import os
import pygame
current_path = os.path.dirname(__file__)
image_path = os.path.join(current_path, 'images')
class Player (pygame.sprite.Sprite):
def __init__(self):
super().__init__() # On initialise 'pygame.sprite.Sprite'
self.health_Player = 100
self.reamaning_health_Player = self.health_Player / (20/13)
self.attackDMG = 10
self.speed = 7.5
self.jumpCount = self.speed
self.countSteps = 0
self.go_left = [pygame.image.load(os.path.join(image_path, 'Sprite_soldatGauche1-4.png')),pygame.image.load(os.path.join(image_path, 'Sprite_soldatGauche2-4.png')),pygame.image.load(os.path.join(image_path, 'Sprite_soldatGauche3-4.png')), pygame.image.load(os.path.join(image_path, 'Sprite_soldatGauche4-4.png'))]
self.go_right = [pygame.image.load(os.path.join(image_path, 'Sprite_soldatDroit1-4.png')),pygame.image.load(os.path.join(image_path, 'Sprite_soldatDroit2-4.png')),pygame.image.load(os.path.join(image_path, 'Sprite_soldatDroit3-4.png')), pygame.image.load(os.path.join(image_path, 'Sprite_soldatDroit4-4.png'))]
self.rect = [img.get_rect() for img in self.go_left and self.go_right]
self.rect.x = 25 #The problem is here
self.rect.y = 640 #The problem is here
def move_left (self):
self.rect.x -= self.speed #The problem is here
问题出在第二个文件中,我用#那么,我能做些什么,将两个列表的每个图片定位在两个变量中:self.rect.x
和self.rect.y
?感谢您花时间回顾我的问题!
当您基于pygame.sprite.Sprite
制作PyGameSprite
时,子画面.image
和.rect
是定义子画面所在位置和外观的关键组件。
CCD_ 10是单个CCD_。
Sprite.rect
是单个pygame.Rect
您的代码正在将Rect列表分配给Sprite.rect
,这将不起作用。大多数sprite实现都会更改sprite.update()
函数中的图像和矩形,该函数在每帧调用一次。
那么,如何前进。首先修复精灵矩形:
class Player( pygame.sprite.Sprite ):
LEFT = 0 # enumerated direction types
RIGHT = 1
def __init__( self, image_path, x, y ):
pygame.sprite.Sprite.__init__( self )
# Load animation
self.go_left = [pygame.image.load(os.path.join(image_path, 'Sprite_soldatGauche1-4.png')),pygame.image.load(os.path.join(image_path, 'Sprite_soldatGauche2-4.png')),pygame.image.load(os.path.join(image_path, 'Sprite_soldatGauche3-4.png')), pygame.image.load(os.path.join(image_path, 'Sprite_soldatGauche4-4.png'))]
self.go_right = [pygame.image.load(os.path.join(image_path, 'Sprite_soldatDroit1-4.png')),pygame.image.load(os.path.join(image_path, 'Sprite_soldatDroit2-4.png')),pygame.image.load(os.path.join(image_path, 'Sprite_soldatDroit3-4.png')), pygame.image.load(os.path.join(image_path, 'Sprite_soldatDroit4-4.png'))]
self.image = self.go_left[0]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.direction = self.LEFT # initially heading left
self.frame_index= 0 # which animation frame is used
这将正确地将图像和矩形设置为第一个动画帧,假设玩家开始面向左侧。
然后,当玩家移动时,按正确的方向循环播放动画。这里的关键思想是,要使精灵动画化,代码只需为方向选择下一个图像,然后重新定位sprite.rect
,以匹配图像和新位置。
def move( self, direction, x_delta ):
# use the next animation index
if ( direction != self.direction ): # player turned?
self.frame_index = 0
else:
self.frame_index += 1 # player continued
self.direction = direction
if ( direction == Player.LEFT ):
# loop at the last frame
self.frame_index %= len( self.go_left )
self.image = self.go_left[ self.frame_index ]
else: # direction == Player.RIGHT
self.frame_index %= len( self.go_right )
self.image = self.go_right[ self.frame_index ]
# re-create the rect with the current co-ordinates
x, y = self.rect.center
self.rect = self.image.get_rect()
self.rect.center = ( x + x_delta, y )
在您当前的代码中,看起来您正试图维护一个rect列表。但你如何保持他们的立场更新?它不会为你节省任何东西,因为图像已经有了矩形。
此外,我只是猜测这就是你的动画应该如何工作,也许它是一个基于时间的动画。所以,这只是为了说明一种方法。也许它会给你一些帮助的想法。