Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

How to use 2 diffrent keypresses to move 2 diffrent shapes at once in python turtle

What im trying to do here is that if 'w' key pressed and 'Up' key pressed both player1 and player2 go up. Right now, if you press both of them, only the one you pressed first will go up. Of curse I still want that if only one of the keys get pressed, only the turtle that connect to him move. How can I do that?

That's my Code:

import turtle
import random

turtle.screensize(1000,1000)
turtle.bgcolor("black")

ball = turtle.Turtle()
ball.color("orange")
ball.shape("circle")
ball.shapesize(1.2)
ball.speed(3)
ball.penup()

player1 = turtle.Turtle()
player1.color("blue")
player1.shape("arrow")
player1.shapesize(5)
player1.speed(3)
player1.penup()
player1.setpos(350,0)

player2 = turtle.Turtle()
player2.color("cyan")
player2.shape("arrow")
player2.shapesize(5)
player2.speed(3)
player2.penup()
player2.setheading(180)
player2.setpos(-350,0)

player1y = 0
player2y = 0

def player1up():
    global player1y
    player1y += 12
    player1.goto(350,player1y)

def player1down():
    global player1y
    player1y -= 12
    player1.goto(350,player1y)

def player2up():
    global player2y
    player2y += 12
    player2.goto(-350,player2y)

def player2down():
    global player2y
    player2y -= 12
    player2.goto(-350,player2y)




turtle.listen()
turtle.onkeypress(player1up, 'Up')
turtle.onkeypress(player1down, 'Down')
turtle.onkeypress(player2up, 'w')
turtle.onkeypress(player2down, 's')
question from:https://stackoverflow.com/questions/65842007/how-to-use-2-diffrent-keypresses-to-move-2-diffrent-shapes-at-once-in-python-tur

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you need is what is known as Game Loop. Your code is working perfectly, but you application is exiting after the first render. So, to keep it rendering frames you need to keep your application alive, running. To do so you can do something like this:

import turtle

#set up the screen
app = turtle.Screen()
app.title("Your app")
app.bgcolor("black")
app.setup(width=1000, height=1000)
app.tracer(0)

ball = turtle.Turtle()
ball.color("orange")
ball.shape("circle")
ball.shapesize(1.2)
ball.speed(3)
ball.penup()

player1 = turtle.Turtle()
player1.color("blue")
player1.shape("arrow")
player1.shapesize(5)
player1.speed(3)
player1.penup()
player1.setpos(350,0)

player2 = turtle.Turtle()
player2.color("cyan")
player2.shape("arrow")
player2.shapesize(5)
player2.speed(3)
player2.penup()
player2.setheading(180)
player2.setpos(-350,0)

player1y = 0
player2y = 0

def player1up():
    global player1y
    player1y += 12
    player1.goto(350,player1y)

def player1down():
    global player1y
    player1y -= 12
    player1.goto(350,player1y)

def player2up():
    global player2y
    player2y += 12
    player2.goto(-350,player2y)

def player2down():
    global player2y
    player2y -= 12
    player2.goto(-350,player2y)


turtle.listen()
turtle.onkeypress(player1up, 'Up')
turtle.onkeypress(player1down, 'Down')

turtle.onkeypress(player2up, 'w')
turtle.onkeypress(player2down, 's')

while True:
    app.update()

Basically, get the instance of the Screen, and call the update() method inside a while True loop, which will maintain your app running.

And all the code you have written is working as you expected.

Here is a little tutorial of another game made with the Turtle package. I believe it's not what you want to do but it could help as a guide.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...