基本上我要做的就是使用Python的海龟模块创建一个双人游戏。底部有两只乌龟是玩家,顶部有两个圆圈作为检查站。一只乌龟应该在两个检查站之间来回走动 - 这只乌龟是目标。底部海龟向目标射击圈。
我的问题:
注意:代码的某些部分可能会造成混淆和/或不需要。
以下是代码:
from turtle import*
import math
def k1():#Enemy Key Commands
enemy.left(90)
enemy.forward(10)
enemy.right(90)
def k2():
enemy.left(90)
enemy.backward(10)
enemy.right(90)
def k3():#Yertle Key Commands
yertle.left(90)
yertle.forward(10)
yertle.right(90)
def k4():
yertle.left(90)
yertle.backward(10)
yertle.right(90)
def k5():
a=bullet_red()
a.speed(10)
a.forward(400)
collision= math.sqrt(math.pow(a.xcor()-yertle.xcor(),2)+math.pow(a.ycor()-yertle.ycor(),2))
if(collision<10):
text=enemy.write("Game Over", align="center" , font=("Arial", 16, "normal"))
a.hideturtle()
def k6():
a=bullet_blue()
a.speed(10)
a.forward(400)
collision= math.sqrt(math.pow(a.xcor()-yertle.xcor(),2)+math.pow(a.ycor()-yertle.ycor(),2))
if(collision<10):
text=enemy.write("Game Over", align="center" , font=("Arial", 16, "normal"))
a.hideturtle()
def collision(a, b):
collision= math.sqrt(math.pow(a.xcor()-b.xcor(),2)+math.pow(a.ycor()-b.ycor(),2))
if(collision<5):
print("Bottom Player Wins")
print("Game Over")
def bullet_red():
bullet=Turtle("circle")#Description For Bullet
bullet.color("red")
bullet.penup()
bullet.goto(enemy.pos())
bullet.setheading(180)
bullet.right(90)
return bullet
def bullet_blue():
bullet=Turtle("circle")#Description For Bullet
bullet.color("blue")
bullet.penup()
bullet.goto(yertle.pos())
bullet.setheading(180)
bullet.right(90)
return bullet
ops = Screen()
ops.setup(500, 500)
ops.onkey(k1, "a")#Enemy
ops.onkey(k2, "d")#Enemy
ops.onkey(k3, "Left")#Yertle
ops.onkey(k4, "Right")#Yertle
ops.onkey(k5, "w")#Shoot(Enemy)
ops.onkey(k6, "Up")#Shoot(Enemy)
ops.listen()
checkpoint_1=Turtle(shape="circle")#Turtle Description for Checkpoint 1
checkpoint_1.color("red")
checkpoint_1.setheading(180)
checkpoint_1.right(90)
checkpoint_1.penup()
checkpoint_1.setx(-220)
checkpoint_1.sety(230)
checkpoint_1.speed(0)
#_____________________________
checkpoint_2=Turtle(shape="circle")#Turtle Description for Checkpoint 2
checkpoint_2.color("red")
checkpoint_2.setheading(180)
checkpoint_2.right(90)
checkpoint_2.penup()
checkpoint_2.setx(220)
checkpoint_2.sety(230)
checkpoint_2.speed(0)
#____________________________
runner=Turtle(shape="turtle")#Turtle Description for Checkpoint 2
runner.color("yellow")
while(runner!=collision):
runner.penup()
runner.goto(checkpoint_1.pos())
runner.goto(checkpoint_2.pos())
runner.speed(0)
#_____________________________
enemy=Turtle(shape="turtle")#Turtle Description for Player 1
enemy.color("red")
enemy.setheading(180)
enemy.right(90)
enemy.penup()
enemy.setx(-20)
enemy.sety(-200)
enemy.speed(0)
#_____________________________
yertle = Turtle(shape="turtle")#Turtle Description for Player 2
yertle.color("blue")
yertle.setheading(180)
yertle.right(90)
yertle.penup()
yertle.setx(20)
yertle.sety(-200)
yertle.speed(0)
#_____________________________
答案 0 :(得分:1)
下面是我对你的代码的修改,让它基本上只是玩游戏 - 它仍然需要工作才能成为一个完成的程序。
from turtle import Turtle, Screen
FONT = ("Arial", 32, "bold")
def k1(): # Enemy Key Commands
enemy.backward(10)
def k2():
enemy.forward(10)
def k3(): # Yertle Key Commands
yertle.backward(10)
def k4():
yertle.forward(10)
def k5(): # enemy shoot
bullet.color("red")
bullet.goto(enemy.position())
bullet.showturtle()
bullet.forward(430)
if bullet.distance(runner) < 10:
magic_marker.write("Game Over", align="center", font=FONT)
bullet.hideturtle()
def k6(): # yertle shoot
bullet.color("blue")
bullet.goto(yertle.position())
bullet.showturtle()
bullet.forward(430)
if bullet.distance(runner) < 10:
magic_marker.write("Game Over", align="center", font=FONT)
bullet.hideturtle()
def move_runner():
if runner.distance(checkpoint_1) < 5 or runner.distance(checkpoint_2) < 5:
runner.left(180)
runner.forward(2)
screen.ontimer(move_runner, 50)
screen = Screen()
screen.setup(500, 500)
bullet = Turtle("circle", visible=False) # Description For Bullet
bullet.speed('normal')
bullet.penup()
bullet.setheading(90)
checkpoint_1 = Turtle("circle", visible=False) # Turtle Description for Checkpoint 1
checkpoint_1.color("red")
checkpoint_1.penup()
checkpoint_1.goto(-220, 230)
checkpoint_2 = checkpoint_1.clone() # Turtle Description for Checkpoint 2
checkpoint_2.goto(220, 230)
runner = Turtle("turtle", visible=False)
runner.color("orange")
runner.speed('fastest')
runner.penup()
runner.sety(230)
yertle = Turtle(shape="turtle", visible=False) # Turtle Description for Player 1
yertle.tiltangle(90) # face one way but move another!
yertle.speed('fastest')
yertle.penup()
yertle.color("blue")
yertle.goto(20, -200)
enemy = yertle.clone() # Turtle Description for Player 2
enemy.color("red")
enemy.goto(-20, -200)
checkpoint_1.showturtle()
checkpoint_2.showturtle()
runner.showturtle()
yertle.showturtle()
enemy.showturtle()
magic_marker = Turtle(visible=False) # for writing text
screen.onkey(k1, "a") # Enemy
screen.onkey(k2, "d") # Enemy
screen.onkey(k3, "Left") # Yertle
screen.onkey(k4, "Right") # Yertle
screen.onkey(k5, "w") # Shoot(Enemy)
screen.onkey(k6, "Up") # Shoot(Yertle)
screen.listen()
move_runner()
screen.mainloop()
您的代码有错误(您的评论也是如此)但我的主要建议是查看海龟文档以查看您可以使用的所有功能 - 在编写游戏时,您需要知道乌龟可以做什么。
我把子弹改成了一个共同的子弹 - 当你完成它们时,乌龟不会消失,所以这似乎更好地用于资源。但是,它也有问题。如果继续使用单个子弹模型,则需要将触发事件互锁以防止重叠。如果您使用相同的多个子弹,则需要添加更多ontimer
个事件,例如runner
用于控制其动作。