我是Python 3的初学者,最近我开始考虑用tkinter创建一个游戏Space Invaders。不幸的是,当我创建一些代码来检测太空船射击的子弹是否击中入侵者时,游戏有时会发出错误,
Traceback (most recent call last):
File "C:\Python32\Space Invaders.py", line 151, in <module>
i.update()
File "C:\Python32\Space Invaders.py", line 98, in update
if self.hit_enemy(pos) == True:
File "C:\Python32\Space Invaders.py", line 86, in hit_enemy
if pos[2] >= value[0] and pos[0] <= value[2]:
IndexError: list index out of range
我能解决这个问题吗?此外,有时候游戏没有检测到子弹击中入侵者的时间。我的代码很可能是错误的,但现在是:
debug = False
from tkinter import *
import easygui
import random
import time
from pygame import mixer
start_time = time.time()
tk = Tk()
tk.title('Space Invaders')
tk.wm_attributes('-topmost', 1)
canvas = Canvas(tk,width = 550, height=400,bd=0,highlightthickness = 0)
canvas.pack()
canvas.update()
canvas.create_rectangle(0,0,600,400,fill ='black')
movementcounter = 0
class Spaceship:
def __init__(self, canvas, colour):
self.canvas = canvas
self.id = canvas.create_rectangle(0,0,30,20, fill=colour)
self.canvas.move(self.id, 245, 300)
self.x = 0
self.canvas_width = self.canvas.winfo_width()
self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
def update(self):
global movementcounter
self.canvas.move(self.id, self.x, 0)
movementcounter=movementcounter+self.x
pos = self.canvas.coords(self.id)
if pos[0] <= 0:
self.x = 0
elif pos[2] >= self.canvas_width:
self.x = 0
def turn_left(self, evt):
self.x = -2
def turn_right(self, evt):
self.x = 2
def stop(self, evt):
self.x = 0
class Bullet:
def __init__(self, canvas, colour):
self.paddle = Spaceship
global movementcounter
self.canvas = canvas
self.id = canvas.create_rectangle(0, 0, 15,20,fill=colour)
self.canvas.move(self.id, 252.5, 300)
self.canvas.move(self.id,movementcounter, 0)
self.x = 0
self.y = -1
self.canvas_height = self.canvas.winfo_height()
def update(self):
self.canvas.move(self.id, 0, -1)
class Space_invader:
def __init__(self, canvas, colour,pos):
self.canvas = canvas
self.enemy = bullets
self.id = canvas.create_rectangle(0,0,30,20, fill=colour)
self.canvas.move(self.id, 245, 0)
self.canvas.move(self.id,pos,0)
self.x = 0
self.y = -1
self.canvas_height = self.canvas.winfo_height()
def hit_enemy(self, pos):
enemy_pos_list = {}
s = 0
for i in self.enemy:
enemy_pos = self.canvas.coords(i.id)
enemy_pos_list[i] =enemy_pos
s = s+1
for key,value in enemy_pos_list.items():
if pos[2] >= value[0] and pos[0] <= value[2]:
if pos[3] >= value[1] and pos[3] <= value[3]:
del enemy_pos_list[key]
self.enemy.remove(key)
canvas.delete(key)
return True
return False
def update(self):
self.canvas.move(self.id, 0, 1)
pos = self.canvas.coords(self.id)
for i in self.enemy:
if self.hit_enemy(pos) == True:
canvas.delete(self.id)
bullets = []
spaceship = Spaceship(canvas, 'white')
invaders = []
def add_bullet(event):
if event.keysym == 'Up':
global end_time
global start_time
end_time = time.time()
if debug == True:
print('End_time =',end_time)
print('Start_time =', start_time)
print('Difference =', end_time-start_time)
if end_time-start_time >= 0.3:
bullet = Bullet(canvas,'white')
bullets.append(bullet)
start_time = time.time()
else:
print('Wait!!!')
position = random.randint(-245,245)
while 1:
canvas.bind_all('<KeyPress-Up>', add_bullet)
canvas.bind_all('<KeyPress-Down>', spaceship.stop)
spaceship.update()
if random.randint(1,150) == 7:
invader = Space_invader(canvas, 'red',position)
invaders.append(invader)
posiaddsub = random.randint(-25,25)
if random.choice([True, False]) == True:
position = position+posiaddsub
if position <=265:
rand = random.randint(-1,40)
position = position+rand
elif position <=-265:
rand = random.randint(1,40)
position = position+rand
else:
position = position +posiaddsub
for i in bullets:
i.update()
for i in invaders:
i.update()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
顺便说一句,如果你不能识别错误代码,我正在使用PTVS和Microsoft Visual Studio 2013。
可能有一种简单的方法可以解决 问题。