3.3. 編程實現
2. 控制摩托車和箱子
在這個步驟中,將編程控制摩托車和箱子角色的運動,讓摩托車在沙漠公路上能夠加速或減速行駛,在碰到箱子時能夠停止,以及顯示麾託車的行駛速度和里程等。
(1) 創建motor_move()函數,實現對摩托的行駛控制,分別使用鍵盤上的 4 個方向鍵控制摩托車向上移動、向下移動、減速和加速。
1 function motor_move(up_max, down_min, speed, key ,dt) 2 #摩托移動''' 3 global motor_speed, motor_bottom 4 if key.UP 5 6 motor_bottom -= 50 * dt 7 #println(motor_bottom) 8 if motor_bottom <HEIGHT-up_max 9 motor_bottom = HEIGHT-up_max 10 end 11 end 12 if key.DOWN 13 motor_bottom += 50 * dt 14 if motor_bottom > down_min 15 motor_bottom = down_min 16 end 17 end 18 if key.LEFT 19 motor_speed -= 324 * dt 20 if motor_speed < 0 21 motor_speed = 0 22 end 23 end 24 if key.RIGHT 25 motor_speed += speed * dt 26 if motor_speed > 1620 27 motor_speed = 1620 #摩托車最大速度 28 end 29 end 30 end
這個函數將會被間接調用,與時間敏感的數據和 dt 參數結合使用,從而精確控制數據的變化。例如,按下向上方向鍵控制麾託車往窗口上方移動時,在代碼 motor_bottom -= 50 * dt 中使用了 dt 參數,假設50是 1s 內的變化量。 也就是說,按下向上方向鍵,變量 motor_bottom 在 lS 內能夠減少50(注意座標軸是位於窗口左上角)。 其他使用了 dt 參數的代碼與之是相同的道理。
(2) 創建motor_control()函數,用於控制麾託車角色運動。根據摩托車與箱子是否碰撞和所處位置使用不同的方式控制摩托車移動,以及顯示摩托車的行駛速度和里程等。
1 function motor_control(g::Game,dt) 2 #摩托車的控制''' 3 global motor_speed, motor_left, motor_bottom, mileag-es,isPlaying,speed_label,mileages_label 4 5 #碰撞檢測和移動控制 6 if collide(motor, box) 7 #println(@sprintf("box.bottom=%2d,motor.bottom=%2d",box.bottom,motor.bottom)) 8 if ( box.bottom - 24< motor.bottom < box.bottom) 9 #println("oo") 10 motor_speed = 0 11 motor_move(50, HEIGHT, motor_speed,g.keyboard, dt) 12 elseif motor.bottom <= box.bottom - 24 13 motor_move(50, box.bottom - 24, 162,g.keyboard, dt) 14 else 15 motor_move(box.bottom, HEIGHT, 162,g.keyboard, dt) 16 end 17 else 18 motor_move(50, HEIGHT, 162,g.keyboard, dt) 19 end 20 motor.bottom = motor_bottom 21 motor_left += motor_speed * dt 22 mileages = motor_left * 0.024 23 speed_label =TextActor(@sprintf("Motor: %.3f km/h",motor_speed * 0.024 * 3.6),"moonhouse") 24 speed_label.pos=(10,15) 25 mileages_label = TextActor(@sprintf("Mileages: %.3f km" ,mileages / 1000),"moonhouse") 26 mileages_label.pos=(10,35) 27 end
關於控制摩托車運動的幾種方式以及將速度和里程轉換成km爲單位等,請查看編程思路中的介紹 這個函數在窗口update()方法中使用。
(3) 創建box_control()函數,用於控制箱子角色運動。當摩托車行駛里程超過100m,並且里程數是 300 的整數倍時,將讓箱子從x座標3000 處向窗口左側移動。同時,箱子出現在麾託車y座標附近。如果玩家不注意控制摩托車行駛,就會撞上箱子。這個函數在窗口update()方法中使用。
1 function box_control(dt) 2 #'''箱子的控制''' 3 global mileages, motor_speed 4 if mileages > 100 && floor(Int,mileages) % 300 == 0 5 println(floor(Int,mileages)) 6 #放置箱子 7 box.left = 3000 8 box.bottom = motor.bottom + 6 9 #播放警報聲 10 play_music(alert_sound,3) 11 else 12 #移動箱子 13 box.left -= motor_speed * dt 14 end 15 end
(4) 窗口update()方法
1 function update(g, dt) 2 bgA.pos=(0 - floor(Int,motor_left) % 600, 0) 3 bgB.pos=(600 - floor(Int,motor_left) % 600, 0) 4 motor_control(g,dt) 5 box_control(dt) 6 end
至此,第2個步驟的工作就完成了。運行程序,就可以使用鍵盤上的4個方向鍵控制摩托車上下移動、加速或減速,當摩托車碰到箱子時,就會停止前進。同時,在摩托車向前行駛時,窗口左上方的速度和里程數會不斷變化。
3. 添加遊戲音效
在這個步驟中,將編程實現讓麾託車行駛時發出轟鳴的引擎聲,以及在靠近箱子時響起警報聲,提醒玩家注意躲避。
(1) 在motor_contro()函數中增加播放摩托車音效的代碼。當摩托車行駛速度大於0 時,就播放轟鳴的引擎聲音效,當行駛速度等於 0 時,則停止聲音。
function motor_control(g::Game,dt) #摩托車的控制''' …… if motor_speed > 0 if !isPlaying play_music(motor_sound) isPlaying=true end elseif motor_speed == 0 if isPlaying==true isPlaying=false end end end
(2) 在 box_control()函數中增加播放警報聲的代碼。當摩托車的行駛里程大於100m,並且是 300 的整數倍時,就播放警報聲音效。
1 function box_control(dt) 2 #'''箱子的控制''' 3 global mileages, motor_speed 4 if mileages > 100 && floor(Int,mileages) % 300 == 0 5 println(floor(Int,mileages)) 6 #放置箱子 7 box.left = 3000 8 box.bottom = motor.bottom + 6 9 #播放警報聲 10 play_music(alert_sound,3) 11 else 12 #移動箱子 13 box.left -= motor_speed * dt 14 end 15 end
至此,這個“瘋狂摩托”遊戲程序編寫完畢。運行程序,玩家就可以聽到摩托車在行駛中發出轟鳴的引擎聲,還可以在聽到警報聲後及時躲避前方出現的箱子。 否則,摩托車在高速行駛時,玩家會很難躲避箱子。