【问题标题】:Looking to add an AI player in Python Pong using Turtle希望使用 Turtle 在 Python Pong 中添加 AI 播放器
【发布时间】:2019-09-12 03:28:22
【问题描述】:

我似乎无法让我的 AI pong 桨在 Python 3.7.1 中工作。我承认我是新手,所以修复可能比我做的更容易。

我尝试了各种形式的循环调整,但我认为我错过了一些在代码处理中起重要作用的小东西。

import turtle

# build window
win = turtle.Screen()

# window title/color/size
win.title("csc2280 Project")
win.bgcolor("black")
win.setup(width=800, height=800)
win.tracer(0)

# user paddle
user_paddle = turtle.Turtle()
# user paddle build
user_paddle.shape('square')
user_paddle.shapesize(stretch_wid=5, stretch_len=1)
user_paddle.speed('fastest')
user_paddle.color("white")
# user paddle position
user_paddle.penup()
user_paddle.goto(-350, 0)

# ai paddle
AI_paddle = turtle.Turtle()
# ai paddle build
AI_paddle.shape('square')
AI_paddle.shapesize(stretch_wid=5, stretch_len=1)
AI_paddle.speed('fastest')
AI_paddle.color('white')
#ai paddle position
AI_paddle.penup()
AI_paddle.goto(350, 0)
AI_paddle.speed = 15

# ball 
ball = turtle.Turtle()
ball.speed(0)
# ball shape
ball.shape('circle')
ball.color('white')
# ball position
ball.penup()
ball.goto(0, 0)
# ball movement
ball.dx = 2
ball.dy = 2

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Player A: 0  Player B: 0", align = "center", font = ("Calibri", 24, "normal"))

# Keeping Score
score_p1 = 0
score_p2 = 0

# move user paddle up and down

# user paddle up
def user_paddle_up():
    y = user_paddle.ycor()
    y += 20
    user_paddle.sety(y)

# user paddle down
def user_paddle_down():
    y = user_paddle.ycor()
    y -= 20
    user_paddle.sety(y)

# actually moving the paddle
win.listen()
win.onkeypress(user_paddle_up, "Up")
win.onkeypress(user_paddle_down, "Down")

AI_paddle.forward(AI_paddle.speed)
y = AI_paddle.ycor()
if y < -300 or y > 300:
    AI_paddle.speed *= -1

我认为问题就在这里。

# game loop
while True:
    win.update()

# moving the ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    # create border
    if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1

    if ball.ycor() < -290:
        ball.sety(-290)
        ball.dy *= -1

    if ball.xcor() > 390:
        ball.goto(0,0)
        ball.dx *= -1

        # updating score
        score_p1 += 1    
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_p1, score_p2), align = "center", font = ("Calibri", 24, "normal"))

    if ball.xcor() < -400:
        ball.goto(0,0)
        ball.dx *= -1

        # updating score
        score_p2 += 1
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_p1, score_p2), align = "center", font = ("Calibri", 24, "normal"))


    # paddle/ball interaction
    if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < AI_paddle.ycor() + 40 and ball.ycor() > AI_paddle.ycor() -40):
        ball.setx(340)
        ball.dx *= -1

    if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < user_paddle.ycor() + 40 and ball.ycor() > user_paddle.ycor() -40):
        ball.setx(-340)
        ball.dx *= -1

任何帮助将不胜感激,因为这是我本学期的最后一个项目,这实际上是唯一让我受挫的事情。

【问题讨论】:

  • 您在这里没有描述任何具体问题或提出任何问题。

标签: python turtle-graphics


【解决方案1】:

我发现你的 AI 播放器的问题是这段代码放错了地方:

AI_paddle.forward(AI_paddle.speed)
y = AI_paddle.ycor()
if y < -300 or y > 300:
    AI_paddle.speed *= -1

它就在主循环之前,所以它只执行一次。它应该在 inside 主循环中,例如作为最后一项。

我发现你的游戏很难玩,所以我在下面调整了一些东西以使其更可玩,并且我已经完成了上面描述的修复:

from turtle import Screen, Turtle

FONT = ("Calibri", 24, "normal")

# move user paddle up and down

# user paddle up
def user_paddle_up():
    y = user_paddle.ycor() + 20
    user_paddle.sety(y)

# user paddle down
def user_paddle_down():
    y = user_paddle.ycor() - 20
    user_paddle.sety(y)

# build window
win = Screen()

# window title/color/size
win.title("csc2280 Project")
win.bgcolor("black")
win.setup(width=800, height=800)
win.tracer(0)

# user paddle
user_paddle = Turtle('square')
# user paddle build
user_paddle.shapesize(stretch_wid=5, stretch_len=1)
user_paddle.color("white")
# user paddle position
user_paddle.penup()
user_paddle.setx(-350)

# AI paddle
AI_paddle = Turtle('square')
# AI paddle build
AI_paddle.shapesize(stretch_wid=5, stretch_len=1)
AI_paddle.color('white')
# AI paddle position
AI_paddle.penup()
AI_paddle.setx(350)
AI_paddle.speed = 10

# ball
ball = Turtle('circle')
# ball shape
ball.color('white')
# ball position
ball.penup()
# ball movement
ball.dx = 2
ball.dy = 2

# Court
court = Turtle(visible=False)
court.color("white")
court.penup()
court.goto(-400, -300)
court.pendown()
court.forward(800)
court.penup()
court.goto(-400, 300)
court.pendown()
court.forward(800)
court.penup()

# Pen
pen = Turtle(visible=False)
pen.color("white")
pen.penup()
pen.sety(325)
pen.write("Player A: 0  Player B: 0", align="center", font=("Calibri", 24, "normal"))

# Keeping Score
score_p1 = 0
score_p2 = 0

# actually moving the paddle
win.onkeypress(user_paddle_up, "Up")
win.onkeypress(user_paddle_down, "Down")
win.listen()

# game loop
while True:
    win.update()

    # moving the ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    # enforce boundaries
    if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1
    elif ball.ycor() < -290:
        ball.sety(-290)
        ball.dy *= -1

    if ball.xcor() > 390:
        ball.goto(0, 0)
        ball.dx *= -1

        # updating score
        score_p1 += 1
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_p1, score_p2), align="center", font=FONT)

    elif ball.xcor() < -390:
        ball.goto(0, 0)
        ball.dx *= -1

        # updating score
        score_p2 += 1
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_p1, score_p2), align="center", font=FONT)

    # paddle/ball interaction
    if (330 < ball.xcor() < 370) and (AI_paddle.ycor() - 60 < ball.ycor() < AI_paddle.ycor() + 60):
        ball.setx(330)
        ball.dx = - abs(ball.dx)

    if (-370 < ball.xcor() < -330) and (user_paddle.ycor() - 60 < ball.ycor() < user_paddle.ycor() + 60):
        ball.setx(-330)
        ball.dx = abs(ball.dx)

    y = AI_paddle.ycor() + AI_paddle.speed
    if y < -240 or y > 240:
        AI_paddle.speed *= -1
    AI_paddle.sety(y)

祝你的 AI 播放器更聪明!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多