[Python-Praxisbedienung] Wie schreibt man schnell ein Tischtennisspiel?


Vorwort

Dieser Artikel enthält eine Python-Implementierung des Tischtennis-Spielcodes. Sie müssen den Code nur kopieren und in den Editor einfügen.
Hallo liebe Python-Liebhaber, heute möchte ich mit euch teilen, wie man ein Pong-Spiel mit einer benutzerdefinierten Roboter-KI in Python schreibt. Unsere Herausforderung besteht darin, zwei Benutzer zu erstellen, der erste Benutzer sind Sie und der zweite Benutzer ist ein KI-Roboter mit 100% Genauigkeit.
Bildbeschreibung hier einfügen

1. Schildkröte und Bildschirm importieren


# Step 1 import set up turtle and Screen
import turtle
import random
s = turtle.Screen()
s.title("Pong")
s.bgcolor("black")
s.setup(width=600, height=400)

2. Erstellen Sie einen Ball


# Step 2 Create ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 4
ball.dy = 4

3. Erstellen Sie eine AI-Blende

Der Code lautet wie folgt (Beispiel):


# Step 3 Create AI paddle
ai = turtle.Turtle()
ai.speed(0)
ai.shape("square")
ai.color("white")
ai.penup()
ai.goto(-250, 0)
ai.shapesize(stretch_wid=5, stretch_len=1)

4. Erstellen Sie Ihre eigene Lünette


# Step 4 Create a paddle For You
you = turtle.Turtle()
you.speed(0)
you.shape("square")
you.color("white")
you.penup()
you.goto(250, 0)
you.shapesize(stretch_wid=5, stretch_len=1)

5. Erstellen Sie eine Funktion, um die KI-Barriere zu verschieben

# Step 5 Create Function to move AI paddle
def move_ai_paddle():
    y = ball.ycor()
    if y > 0:
        ai.sety(ai.ycor() + 2)
    else:
        ai.sety(ai.ycor() - 2)

6. Erstellen Sie eine Funktion, um Ihre Lünette zu bewegen und mit der Tastatur zu steuern


# Step 6 Create a Function to move the your paddle with up and down key
def paddle2_up():
    y = you.ycor()
    y += 20
    you.sety(y)

def paddle2_down():
    y = you.ycor()
    y -= 20
    you.sety(y)
# Your Paddle control it with key
s.listen()
s.onkeypress(paddle2_up, "Up")
s.onkeypress(paddle2_down, "Down")
Step 7 使用 while 循环开始游戏
# Step 7 Start the game with a while loop
while True:
    s.update()
    
    # Move the ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)
    
    # Check for collisions with the walls
    if ball.ycor() > 190 or ball.ycor() < -190:
        ball.dy *= -1
    
    # Move the robot paddle towards the ball
    if ball.ycor() > ai.ycor():
        ai.sety(ai.ycor() + 4)
    elif ball.ycor() < ai.ycor():
        ai.sety(ai.ycor() - 4)
    
           # Check for end game conditions
    if ball.xcor() > 300:
        turtle.textinput("Game End", "You Loss Pong Game With AI!")
        break
    if ball.xcor() < -300:
        turtle.textinput("Game End", "You Win Pong Game With AI!")
        break
    
    # Check for collisions with the robot paddle
    if (ball.xcor() < -240 and ball.xcor() > -250) and (ball.ycor() < ai.ycor() + 40 and ball.ycor() > ai.ycor() - 40):
        if random.random() < 0.9: # 90% chance of collision
            ball.dx *= -1
    
        # Check for collisions with the user paddle
    if (ball.xcor() > 240 and ball.xcor() < 250) and (ball.ycor() < you.ycor() + 40 and ball.ycor() > you.ycor() - 40):
        ball.dx *= -1

7. Alle Codes


# Step 1 import set up turtle and Screen
import turtle
import random
s = turtle.Screen()
s.title("Pong")
s.bgcolor("black")
s.setup(width=600, height=400)

# Step 2 Create ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 4
ball.dy = 4

# Step 3 Create AI paddle
ai = turtle.Turtle()
ai.speed(0)
ai.shape("square")
ai.color("white")
ai.penup()
ai.goto(-250, 0)
ai.shapesize(stretch_wid=5, stretch_len=1)

# Step 4 Create a paddle For You
you = turtle.Turtle()
you.speed(0)
you.shape("square")
you.color("white")
you.penup()
you.goto(250, 0)
you.shapesize(stretch_wid=5, stretch_len=1)

# Step 5 Create Function to move AI paddle
def move_ai_paddle():
    y = ball.ycor()
    if y > 0:
        ai.sety(ai.ycor() + 2)
    else:
        ai.sety(ai.ycor() - 2)

# Step 6 Create a Function to move the your paddle
def paddle2_up():
    y = you.ycor()
    y += 20
    you.sety(y)

def paddle2_down():
    y = you.ycor()
    y -= 20
    you.sety(y)
# Your Paddle control it with key
s.listen()
s.onkeypress(paddle2_up, "Up")
s.onkeypress(paddle2_down, "Down")

# Step 7 Start the game with a while loop
while True:
    s.update()
    
    # Move the ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)
    
    # Check for collisions with the walls
    if ball.ycor() > 190 or ball.ycor() < -190:
        ball.dy *= -1
    
    # Move the robot paddle towards the ball
    if ball.ycor() > ai.ycor():
        ai.sety(ai.ycor() + 4)
    elif ball.ycor() < ai.ycor():
        ai.sety(ai.ycor() - 4)
    
           # Check for end game conditions
    if ball.xcor() > 300:
        turtle.textinput("Game End", "You Loss Pong Game With AI!")
        break
    if ball.xcor() < -300:
        turtle.textinput("Game End", "You Win Pong Game With AI!")
        break
    
    # Check for collisions with the robot paddle
    if (ball.xcor() < -240 and ball.xcor() > -250) and (ball.ycor() < ai.ycor() + 40 and ball.ycor() > ai.ycor() - 40):
        if random.random() < 0.9: # 90% chance of collision
            ball.dx *= -1
    
        # Check for collisions with the user paddle
    if (ball.xcor() > 240 and ball.xcor() < 250) and (ball.ycor() < you.ycor() + 40 and ball.ycor() > you.ycor() - 40):
        ball.dx *= -1
 
turtle.exitonclick()

Zusammenfassen

Darüber möchte ich heute sprechen und hoffe, dass alle Xu Langs Vortragssaal weiterhin unterstützen werden

おすすめ

転載: blog.csdn.net/liaozp88/article/details/130539520