Python 在游戏中射击不起作用
我正试图在pygame中编写一个代码来射击子弹,但它不起作用,因为它给出了一个索引外错误。这是我的密码。我做错了什么?谢谢Python 在游戏中射击不起作用,python,pygame,Python,Pygame,我正试图在pygame中编写一个代码来射击子弹,但它不起作用,因为它给出了一个索引外错误。这是我的密码。我做错了什么?谢谢 while fire ==True: print 'FIRE' for bullet in bullets[:]: nowtime=pygame.time.get_ticks() if (nowtime-bullets[3])>1000: bullets.pop(bullets.index(bul
while fire ==True:
print 'FIRE'
for bullet in bullets[:]:
nowtime=pygame.time.get_ticks()
if (nowtime-bullets[3])>1000:
bullets.pop(bullets.index(bullet))
if fire and not oldfire:
bullets.append([int(x_coord),int(y_coord),angle,pygame.time.get_ticks()])
oldfire=fire
screen.fill(white)
for bullet in bullets:
bx=bullet[0]
by=bullet[1]
ba=bullet[2]
bt=bullet[3]
rads=-(ba/360)*2*math.pi
x=int(bx+(blsp*bvel*0.36*math.cos(rads)))
y=int(by+(blsp*bvel*0.36*math.sin(rads)))
print x,y,ba,bt
pygame.draw.circle(screen,green,[x,y],8)
bullet[0]=x
bullet[1]=y
pygame.quit()
更新1
这是我的全部代码,可能会有所帮助。谢谢
import pygame,math
from pygame import *
black = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)
arrow=pygame.image.load('plane.png')
explosion=pygame.image.load('explosion.png')
explosion=pygame.transform.scale(explosion,(300,200))
arrow=pygame.transform.scale(arrow,(150,50))
pygame.init()
size=[1100,900]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
done=False
fire=False
clock=pygame.time.Clock()
pygame.mouse.set_visible(1)
x_speed=0
y_speed=0
x_coord=10
y_coord=10
bullets=[]
oldfire=[]
sp=9
angle=0
blsp=1
bvel=30
def pri():
rotarrow=pygame.transform.rotate(arrow, angle)
rotrec=rotarrow.get_rect()
rotrec.center=(x_coord,y_coord)
screen.blit(rotarrow,rotrec)
pygame.display.flip()
clock.tick(60)
def explode():
screen.fill([255,255,255])
screen.blit(explosion,(x_coord-150,700))
pygame.display.flip()
pygame.quit()
while done==False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
angle=angle+1
if event.key == pygame.K_RIGHT:
angle=angle-1
if event.key == pygame.K_UP:
sp=18
if event.key == pygame.K_DOWN:
sp=6
if event.key == pygame.K_SPACE:
fire=True
y_coord += (math.sin(-angle / (180/math.pi)) * sp)
x_coord += (math.cos(-angle / (180/math.pi)) * sp)
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
sp=9
if event.key == pygame.K_DOWN:
sp=9
y_coord+=5#gravity
if x_coord<80:
x_coord=80
if x_coord>size[0]-80:
x_coord=size[0]-80
if y_coord < 80:
y_coord=80
if y_coord>size[0]-80:
explode()
break
screen.fill(white)
pri()
while fire ==True:
print 'FIRE'
for bullet in bullets[:]:
nowtime=pygame.time.get_ticks()
if (nowtime-bullets[2])>1000:
bullets.pop(bullets.index(bullet))
if fire and not oldfire:
bullets.append([int(x_coord),int(y_coord),angle,pygame.time.get_ticks()])
oldfire=fire
screen.fill(white)
for bullet in bullets:
bx=bullet[0]
by=bullet[1]
ba=bullet[2]
bt=bullet[3]
rads=-(ba/360)*2*math.pi
x=int(bx+(blsp*bvel*0.36*math.cos(rads)))
y=int(by+(blsp*bvel*0.36*math.sin(rads)))
print x,y,ba,bt
pygame.draw.circle(screen,green,[x,y],8)
bullet[0]=x
bullet[1]=y
pygame.quit()
导入pygame、数学
从pygame导入*
黑色=(0,0,0)
白色=(255255)
绿色=(0255,0)
红色=(255,0,0)
arrow=pygame.image.load('plane.png')
explosion=pygame.image.load('explosion.png')
爆炸=pygame.transform.scale(爆炸,(300200))
arrow=pygame.transform.scale(箭头,(150,50))
pygame.init()
大小=[1100900]
screen=pygame.display.set_模式(大小)
pygame.display.set_标题(“我的游戏”)
完成=错误
火=假
clock=pygame.time.clock()
pygame.mouse.set_可见(1)
x_速度=0
y_速度=0
x_坐标=10
y_坐标=10
项目符号=[]
oldfire=[]
sp=9
角度=0
blsp=1
bvel=30
def pri():
rotarrow=pygame.transform.rotate(箭头,角度)
rotrec=rotarrow.get_rect()
旋转中心=(x坐标,y坐标)
屏幕。blit(rotarrow,rotrec)
pygame.display.flip()
时钟滴答(60)
def explode():
屏幕填充([255255])
屏幕。blit(爆炸,(x_coord-150700))
pygame.display.flip()
pygame.quit()
完成时==False:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
角度=角度+1
如果event.key==pygame.K_RIGHT:
角度=角度-1
如果event.key==pygame.K_UP:
sp=18
如果event.key==pygame.K_向下:
sp=6
如果event.key==pygame.K_空间:
火=真
y_坐标+=(数学sin(-angle/(180/math.pi))*sp)
x_坐标+=(数学cos(-angle/(180/math.pi))*sp)
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_UP:
sp=9
如果event.key==pygame.K_向下:
sp=9
y_坐标+=5#重力
如果x_坐标大小[0]-80:
x_坐标=尺寸[0]-80
如果y_坐标<80:
y_坐标=80
如果y_坐标>大小[0]-80:
爆炸
打破
屏幕填充(白色)
pri()
当fire==True时:
打印“火”
对于项目符号中的项目符号[:]:
nowtime=pygame.time.get_ticks()
如果(现在项目符号[2])>1000:
子弹.弹孔(子弹.索引(子弹))
如果是火而不是旧火:
bullets.append([int(x_坐标)、int(y_坐标)、angle、pygame.time.get_ticks()]))
旧火
屏幕填充(白色)
对于子弹中的子弹:
bx=项目符号[0]
by=项目符号[1]
ba=项目符号[2]
bt=项目符号[3]
rads=-(ba/360)*2*math.pi
x=int(bx+(blsp*bvel*0.36*math.cos(rads)))
y=int(by+(blsp*bvel*0.36*math.sin(rads)))
打印x、y、ba、bt
pygame.draw.circle(屏幕,绿色,[x,y],8)
项目符号[0]=x
项目符号[1]=y
pygame.quit()
虽然特定的堆栈跟踪会有所帮助,但您的问题可能来自以下几行:
if (nowtime-bullets[3])>1000:
我想您的意思是检查bullet数组的第四个参数(您稍后引用为
bt
)。但是,这将是项目符号[3]
,您有项目符号[3]
,它正在检查第四个项目符号。如果数组中没有那么多项目符号,则会生成索引错误。欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南。适用于这里。在您发布代码并准确描述问题之前,我们无法有效地帮助您。这包括提供完整的错误消息和堆栈跟踪、您使用的任何跟踪打印语句的输出(您在发布之前尝试过自己调试,对吗?),以及您对程序操作的任何其他想法。