嗨我想编程的街机游戏小行星,并已这样做,当用户按空格键,创建一个圆形'船'当前位置,并且其位置添加到'ball_list',而船舶的水平和垂直速度是存储在“ball_vlist”新圈子的速度,如图Python .append改变输入列表?
def draw(canvas):
global ship_pos, ship_vel, ball_list
if current_key=='32': # if spacebar is pressed
ball_list.append(ship_pos) # create a new circle and store is position
ball_vlist.append(ship_vel) # add a velocity to this new circle
当我运行整个程序,在速度的船移动我最初给它,因为我所期望的。但是,当我按空格键时,速度会加快,而我不知道为什么。我发现这条线是造成这个问题的原因:
ball_list.append(ship_pos)
因为当我评论它的船继续正常时,空格键被按下。是否以某种方式改变船的位置?我已经检查过船的速度(ship_vel)保持恒定,即使船在加速。
谢谢你的帮助!如果您需要更多的上下文,这里是整个程序:
import simplegui
ball_list = []
ball_vlist = []
ship_pos = [200, 400]
ship_vel = [.5, -.5]
current_key=' '
frame = simplegui.create_frame("Asteroids", 800, 500)
def tick():
global ball_list, ball_vlist, ship_pos
# update the ship position
ship_pos[0] += ship_vel[0]
ship_pos[1] += ship_vel[1]
# update the ball positions
for i in range(len(ball_list)):
ball_list[i][0]+=ball_vlist[i][0]
ball_list[i][1]+=ball_vlist[i][1]
def draw(canvas):
global ship_pos, ship_vel, ball_list
if current_key=='32':
ball_list.append(ship_pos)
ball_vlist.append(ship_vel)
for ball_pos in ball_list:
canvas.draw_circle(ball_pos, 1, 1, "white", "white") # these are the circles the ship shoots
canvas.draw_circle(ship_pos, 4, 1, "red", "green") # this is my 'ship' (just to test)
def keydown(key):
global current_key
current_key = str(key)
def keyup(key):
global current_key
current_key=' '
timer = simplegui.create_timer(10, tick)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
frame.set_draw_handler(draw)
frame.start()
timer.start()
啊这是有道理的!谢谢一堆! – kylecblyth