0
我寫了一個突破風格的遊戲,它除了球蝙蝠的碰撞和反射之外都起作用。它應該使工作如果球從左向右移動,因爲它擊中了蝙蝠:python - 我的球/蝙蝠碰撞和反射有什麼問題?
如果它擊中左端它反彈回它 來的方向,如果它擊中它反彈的右端在相同的方向
,反之亦然從右到左的方向。並且:
如果它碰到中間區域,它會以相同的角度反彈回來,如果它碰到中心左/右區域,它會在角度發生輕微變化時反彈回去。
它有時也會通過蝙蝠,即使它結束了,應該是反彈,這是令人困惑的,但我認爲可能是由於`BH == Bat.y線,因爲球正在移動角度等都可能略微超過,並繼續進行。
代碼:(BAW/H =蝙蝠寬/高,BW/H =球寬/高)
# check with bat
if theball.y+BH == thebat.y and (theball.x >= thebat.x-5 and theball.x <= thebat.x+BATW):
# collision in centre - rebounds with angle reflection == angle incidence
if theball.cx>= thebat.x+40 and theball.cx<=thebat.x+60:
theball.dy = -theball.dy
return
# else find ball direction, do all areas for each direction
if theball.oldx < theball.x: # ball moving left to right, find which area: ends, mid lef, mid right, centre
# collision with left end
if theball.cx<= thebat.x+10:
# ball rebounds back in direction it came from
theball.dx = - theball.dx
theball.dy = - theball.dy
return
# collision with right end
if theball.cx >= thebat.x+90:
angle -= 10
if angle < MIN_ANGLE:
angle = MIN_ANGLE
theball.dx, theball.dy = calc_dxdy(angle)
return
# middle left and right
# mid left
if (theball.cx > thebat.x+10 and theball.cx < thebat.x+40):
angle -=5
if angle <= MIN_ANGLE:
angle = MIN_ANGLE
theball.dx, theball.dy = calc_dxdy(angle)
return
# mid right
if (theball.cx > thebat.x+60 and theball.cx < thebat.x+90):
angle +=5
if angle >= MAX_ANGLE:
angle = MAX_ANGLE
theball.dx, theball.dy = calc_dxdy(angle)
return
# ball moving right to left
if theball.oldx > theball.x: # ball moving right to left, find which area: ends, mid lef, mid right, centre
# collision with right end
if theball.cx>= thebat.x+90:
# ball rebounds back in direction it came from
theball.dx = - theball.dx
theball.dy = - theball.dy
return
# collision with right end
if theball.cx <= thebat.x+10:
angle += 10
if angle > MAX_ANGLE:
angle = MAX_ANGLE
theball.dx, theball.dy = calc_dxdy(angle)
return
# middle left and right
# mid left
if (theball.cx > thebat.x+10 and theball.cx < thebat.x+40):
angle +=5
if angle <= MAX_ANGLE:
angle = MAX_ANGLE
theball.dx, theball.dy = calc_dxdy(angle)
return
# mid right
if (theball.cx > thebat.x+60 and theball.cx < thebat.x+90):
angle -=5
if angle >= MIN_ANGLE:
angle = MIN_ANGLE
theball.dx, theball.dy = calc_dxdy(angle)
return