im having issues for loading an image to a certain location in which I choose to the gameboard
编辑:链接图像中的OCR文本。
def drawPieces (gameBoard):
global goblin1a
global goblin1b
global goblin2a
global goblin2b, goblin3a, goblin3b, goblin4a, goblin4b
global xcoordinate
global ycoordinate
global i
global j
for x in range (0,20):
for y in range (0,20):
xcoordinate.append(((margin+width) * x + margin+32)+xDistanceFromEdge)
ycoordinate.append((margintheight) * y + margin+33
#if gameBoard [x] [y]=="NormalBlack"
goblin1a=xcoordinate[2]#goblin 1
goblin1b=ycoordinate[2]#goblin 1
goblin2a=xcoordinate[3]#goblin 1
goblin2b=ycoordinate[3]#goblin 1
goblin3a=xcoordinate[7]#goblin 1
goblin3b=ycoordinate[5]#goblin 1
goblin4a=xcoordinate[9]#goblin 1
goblin4b=ycoordinate[2]#goblin 1
screen.blit(walkLeft, (goblin1a, goblin1b))
print (xcoordinate)
drawPieces (gameBoard)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygameMOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
答案 0 :(得分:1)
所有这些坐标中的代码都陷入困境!
如果有某种方式可以将妖精的一切都存储在一起,保持其整洁,并允许其作为参数传递或存储在列表中,该怎么办?
那么我们对地精有什么了解呢?
可以很容易地将其放入python list(如数组)中:
goblin1 = [ 0, 0, walkLeft ]
goblin2 = [ 1, 0, walkLeft ]
...
这些可以保存在另一个列表中:
goblins = [ goblin1, goblin2, ... ]
简化drawPieces()
:
# window border constants
X_BORDER = 40
Y_BORDER = 40
drawPieces( screen, goblin_list ):
global X_BORDER, Y_BORDER
# paint background white
screen.fill( ( 255,255,255 ) )
# draw each goblin in the list
for single_goblin in goblin_list:
# copy the three parts of the goblin's info from the list
board_x, board_y, bitmap = single_goblin
# convert game grid-position to screen-position
screen_x = X_BORDER + ( board_x * 32 )
screen_y = Y_BORDER + ( board_y * 33 )
screen.blit( bitmap, ( screen_x, screen_y ) )
# flush the updates to the display
pygame.display.flip()
还有PyGame sprites,它们是软件对象,因此它们既封装了对象的变量,又封装了与之配合使用的函数。
class GoblinSprite( pygame.sprite.Sprite ):
def __init__( self, bitmap, board_position ):
self.position = board_position
self.image = bitmap
self.rect = self.image.get_rect()
self.setScreenPosition()
def setScreenPosition( self ):
global X_BORDER, Y_BORDER
screen_x = X_BORDER + ( self.position[0] * 32 )
screen_y = Y_BORDER + ( self.position[1] * 33 )
self.rect.x = screen_x
self.rect.y = screen_y
return (screen_x, screen_y)
PyGame网站上有一个简短的tutorial。