嗨我想編程的街機遊戲小行星,並已這樣做,當用戶按空格鍵,創建一個圓形'船'當前位置,並且其位置添加到'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