I have a list to store the X and Y of bullets in my game, and when spacebar is pushed, it adds an x and y to the list, and then it should call both values and draw them. i know i need to delete them after they leave the screen, but atm when i hit space this error pops up
在我的游戏中,我有一个列表来存储子弹的X和Y,当空格键被推时,它将X和Y添加到列表中,然后它应该调用两个值并绘制它们。我知道我需要在他们离开屏幕后删除它们,但是当我点击空格时,这个错误会弹出。
TypeError:'int' object has no attribute '__getitem__'
here's my code:
这是我的代码:
#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)
can you help me out? how do i call both values?
你能帮我吗?我如何调用这两个值?
1 个解决方案
#1
1
It looks like you meant to put tuples into your list, so this:
看起来你想把元组放到你的列表里,所以这个:
bullets.insert(player_x,665)
Should be:
应该是:
bullets.append((player_x,665))
#1
1
It looks like you meant to put tuples into your list, so this:
看起来你想把元组放到你的列表里,所以这个:
bullets.insert(player_x,665)
Should be:
应该是:
bullets.append((player_x,665))