我目前正在做一個學校專案,想要在 Visual Python 中制作 2D pong。現在,我已經在代碼底部設定了 2 個槳的當前碰撞。當它運行時,它決定穿過它并擊中綠色墻壁(稍后我將更改為清除墻壁)。即使槳不在球附近,它仍然會檢測到“墻”存在并反彈,我很確定它會在槳所在的 x 軸上創建一個巨大的瘦墻,但垂直拉伸。除了這種方式,我不知道我的老師教過任何其他方式。任何幫助將不勝感激!
from visual import *
# Title Screen
def TitleScreen():
# Display
sceneTest = display(title='Pong', x=0, y=0, width=950,
height=950, center=(5,0,0), background=(1,1,1))
#autoscale
sceneTest.autoscale = False
#Back Wall
wallBack = box(pos = vector(2,0,-2), size=(50,50,0.2), color = color.black)
#Ball
ballPong = box(pos = vector(5,1,0), size=(0.5,0.5,0.01), color = color.white)
#Left Stick
stick1 = box(pos = vector(-6,0,0), size=(0.5, 5, 0.2), color = color.white)
#Right Stick
stick2 = box(pos = vector(16,0,0), size=(0.5, 5, 0.2), color = color.white)
# Title Text
titleScreenText = text(text = "Pong", pos = (5,9, 0), align = "center",
depth = 0.01, height = 1.5, width = 1.5,
color = color.white)
# Start Game
titleScreenText = text(text = "Press E to Start", pos = (-1.5,-8.5, 0),
depth = 0.01, height = 1.5, width = 1.5,
color = color.white)
# Main Loop for starting game
while True:
rate(30)
if sceneTest.kb.keys:
key = sceneTest.kb.getkey()
if key == "e":
sceneTest.delete()
pongGame()
# Main Game Loop
def pongGame():
# Game Display
newDisplay = display(title='Pong', x=0, y=0,width=950,
height=950, center=(5,0,0), background=(1,1,1))
newDisplay.autoscale = False
# BlackWall
wallBack = box(pos = vector(2.5,0,-2), size=(50,50,0.2), color = color.black)
ball2 = box(pos = vector(5,0,0), size=(0.5,0.5,0.5), color = color.white)
# Stick Paddle 1
stick1New = box(pos = vector(-6,0,0), size=(0.5, 5, 0.2), color = color.white)
# Stick Paddle 2
stick2New = box(pos = vector(16,0,0), size=(0.5, 5, 0.2), color = color.white)
#Clear Wall Left Boundary color = color.green) opacity= 0.01
wallClear1 = box(pos=vector(-6.5,0,0), size=(0.2,22,0.3), color = color.green)
#Clear Wall Right Boundary
wallClear2 = box(pos=vector(16.5,0,0), size=(0.2,22,0.3), color = color.green)
# Clear Wall Top Boundary
wallClear3 = box(pos=vector(5,11,0), size=(23,0.2,0.3), color = color.green)
# Clear Bottom Boudary
wallClear4 = box(pos=vector(5,-11,0), size=(23,0.2,0.3), color = color.green)
# Ball Velocity Initial
ball2.velocity = vector(11,0)
deltaT = 0.005
t = 0.0
# Scoreboard
player1Score = 0
player2Score = 0
scoreText = ("{} : {}".format(player1Score, player2Score))
scoreTextDisplay = text(text = scoreText, pos = (5,9, 0), align = "center", depth = 0.1, height = 1.5, width = 1.5, color = color.white)
while True:
# Refresh
rate(100)
# Ball Velocity while Running
t = t deltaT
ball2.pos = ball2.pos ball2.velocity*deltaT
##
## # Literal Aimbot
## stick2New.velocity = vector(0,3)
## stick2New.pos = stick2New.pos stick2New.velocity*deltaT
## stick2New.pos.y = ball2.pos.y
##
# Player 1 Input/Controls
if newDisplay.kb.keys:
keyNew = newDisplay.kb.getkey()
# Moving Up
if keyNew == "w":
stick1New.pos.y = stick1New.pos.y 0.25
# Moving Down
if keyNew == "s":
stick1New.pos.y = stick1New.pos.y - 0.25
# If ball hits Right wall
if ball2.pos.x > wallClear2.pos.x:
ball2.velocity.x = ball2.velocity.x
player1Score = player1Score 1
ball2.pos = (5,0,0)
# If ball hits Left wall
if ball2.pos.x < wallClear1.pos.x:
ball2.velocity.x = ball2.velocity.x
player2Score = player2Score 1
ball2.pos = (5,0,0)
# If the Ball hits the top of the Wall
if ball2.pos.y > wallClear3.pos.y:
ball2.velocity.y = -ball2.velocity.y
# If the Ball hits the bottom of the Wall
if ball2.pos.y < wallClear4.pos.y:
ball2.velocity.y = -ball2.velocity.y
# Collisions Check OLD
# if ball hits right paddle
## if ball2.pos.x < stick2New.pos.x: #or ball2.pos.x > stick2New.pos.x:
## ball2.velocity.x = -ball2.velocity.x
## if ball2.pos.x > (stick2New.pos.x == -6) or ball2.pos.x < (stick2New.pos.x > -6):
## ball2.velocity.x = -ball2.velocity.x
# if ball hits left paddle
if ball2.pos.x < stick1New.pos.x:
ball2.velocity.x = -ball2.velocity.x
if ball2.pos.x > stick2New.pos.x:
ball2.velocity.x = -ball2.velocity.x
TitleScreen()
uj5u.com熱心網友回復:
在檢查與槳的碰撞時,您沒有任何代碼來檢查球的 y 坐標。這將使槳看起來無限高。您可以合并一些代碼,例如if (ball2.pos.x < stick1New.pos.x) and abs(ball2.pos.y-stick1New.pos.y) < 2.5: ball2.velocity.x*=-1; ball2.velocity.y *=-1;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/376790.html
標籤:Python
