我有一个列表可以在我的游戏中存储子弹的X和Y,当按下空格键时,它会在列表中添加一个x和y,然后它应该调用这两个值并绘制它们。我知道我需要在他们离开屏幕后删除它们,但是当我点击空间时会弹出这个错误
TypeError:'int' object has no attribute '__getitem__'
这是我的代码:
#start system
import pygame, sys, random
from pygame.locals import*
pygame.init()
fps=50
Clock=pygame.time.Clock()#use fps as system time
#set up a list of STUFF
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 100, 0)
RED = ( 100, 0, 0)
YELLOW= (255,255,0)
ORANGE= (255,165,0)
screensizex=800
screensizey=600
bullets=[]
screenarea=pygame.display.set_mode((screensizex,screensizey))
pygame.display.set_caption("space invaders")
player_x=250
# end of variable init, begin main loop!
while 1==1:
#key roster
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_d and player_x<screensizex-25:
player_x+=25
if event.key==K_a:
player_x-=25
if event.key==K_SPACE:
bullets.insert(player_x,665)
#drawing to screen
screenarea.fill(WHITE)
for bullet in bullets:
pygame.draw.rect(screenarea,ORANGE,Rect(bullet[0],bullet[1],5,10))
pygame.draw.rect(screenarea,GREEN,(0,screensizey-100,screensizex,100))
pygame.draw.rect(screenarea,RED,(player_x,screensizey-125,25,25))
pygame.display.update()
Clock.tick(fps)
你可以帮帮我吗?我如何称呼两个值?
答案 0 :(得分:1)
看起来你打算把元组放到你的列表中,所以这个:
bullets.insert(player_x,665)
应该是:
bullets.append((player_x,665))