Python 2.7 我能';不要让我的玩家在Pygame上射击,因为上面写着:';浮动';对象没有属性'__获取项目';
嗯。我只想在比赛中射出一些子弹。我尽了最大的努力,但我做不到 首先,我将项目符号的角度和位置存储在项目符号数组的列表中。第二,我用sin和cos给它速度,我把它的运动限制在640px和480px的循环中。然后我把它显示在player_x,player_y上 它应该从player_x,player_y开始,按照cos和sin法则行走,并在屏幕上破坏bordeline 但它显示了以下句子:Python 2.7 我能';不要让我的玩家在Pygame上射击,因为上面写着:';浮动';对象没有属性'__获取项目';,python-2.7,pygame,Python 2.7,Pygame,嗯。我只想在比赛中射出一些子弹。我尽了最大的努力,但我做不到 首先,我将项目符号的角度和位置存储在项目符号数组的列表中。第二,我用sin和cos给它速度,我把它的运动限制在640px和480px的循环中。然后我把它显示在player_x,player_y上 它应该从player_x,player_y开始,按照cos和sin法则行走,并在屏幕上破坏bordeline 但它显示了以下句子: 第53行 vel_x=math.cos(bullet[0])*10类型错误:“float”对象没有 属性'ge
第53行
vel_x=math.cos(bullet[0])*10
类型错误:“float”对象没有 属性'getitem' 这是代码
#
# Title: Shoot The Zombies
# Author(s): John Redbeard, ("http://john-redbeard.tumblr.com/")
#
import pygame
from pygame.locals import *
import math
# Initiate Pygame
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
pygame.display.set_caption("Shoot the Zombies")
fps = 60
fpsclock = pygame.time.Clock()
# Load images
player = pygame.image.load("images/player.png")
player_x = 100
player_y = 100
posplayer_x = 0
posplayer_y = 0
grass = pygame.image.load("images/grass.png")
bullet = pygame.image.load("images/bullet.png")
bullets_array = []
target = pygame.image.load("images/target.png")
# Main Loop Game
while True:
pygame.mouse.set_visible(False)
screen.fill(False)
# Load on screen the grass in isometric way
for x in range(width/grass.get_width()+1):
for y in range(height/grass.get_height()+1):
screen.blit(grass,(x*100,y*100))
# Load on screen the rotated-positioned-player
mouse_position = pygame.mouse.get_pos()
angle = math.atan2(mouse_position[0] - player_x, mouse_position[1] - player_y)
player_rotate = pygame.transform.rotate(player, 360+angle*57.29)
player_position = (player_x - player_rotate.get_rect().width/2, player_y - player_rotate.get_rect().height/2)
screen.blit(player_rotate, player_position)
# load on screen the rotated-posioted-bullets
for bullet in bullets_array:
vel_x = math.cos(bullet[0])*10
vel_y = math.sin(bullet[1])*10
bullet[0] += vel_x
bullet[1] += vel_y
if bullet[0] >640 or bullet[1] > 480:
bullets_array.remove(bullet)
bullet1 = pygame.transform.rotate(bullet, 360+angle*57.29)
screen.blit(bullet1, (player_x, player_y))
# Load on screen the target
screen.blit(target, (mouse_position))
# Display window
pygame.display.flip()
# Run events
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
exit(False)
# Event: W/A/S/D (Keyboard) moving player
elif event.type == KEYDOWN:
if event.key == K_w:
posplayer_y -= 3
elif event.key == K_a:
posplayer_x -= 3
elif event.key == K_s:
posplayer_y += 3
elif event.key == K_d:
posplayer_x += 3
elif event.type == KEYUP:
if event.key == K_w:
posplayer_y = 0
elif event.key == K_a:
posplayer_x = 0
elif event.key == K_s:
posplayer_y= 0
elif event.key == K_d:
posplayer_x = 0
# Event: Mouse click shoot the bullet
elif event.type == MOUSEBUTTONDOWN:
bullets_array.append(math.atan2(mouse_position[0] - player_x, mouse_position[1] - player_y))
player_x += posplayer_x
player_y +=posplayer_y
fpsclock.tick(fps)
您将
float
值添加到项目符号\u数组中
:
bullets_array.append(math.atan2(mouse_position[0] - player_x, mouse_position[1] - player_y))
那只是一个电话;它产生一个浮点值
然后,您期望有两个值:
for bullet in bullets_array:
vel_x = math.cos(bullet[0])*10
vel_y = math.sin(bullet[1])*10
不能为浮点值编制索引
如果您希望有两个值,那么必须创建一个包含两个浮点的元组,而不是只添加一个浮点。你当然没有
将项目符号的角度和位置存储在项目符号数组的列表中
首先“bullet”是一个PNG文件,然后它是一个浮点数组中的一个项,然后你把这个浮点数组作为两个元素的数组bullet[0]和bullet[1]来使用?