键盘按键不起作用。 [蟒蛇]

时间:2015-12-04 15:56:49

标签: python turtle-graphics

我创造了这个迷你游戏,但我遇到了一个问题。没有出现任何错误。它只是不工作。

问题

当我按下字母" P"游戏应该开始(它还没有完成。它应该只是清除屏幕并放一个绿点),或者当我按下" H"应该弹出高分。所以我觉得你明白了。

我认为的问题是代码(由于评论而插入):

#a command for the back menu (not finished)
def back():
  drawButton = turtle.Turtle()
  drawButton.hideturtle()
  drawButton.penup()
  drawButton.speed(0)
  drawButton.goto(100, 70)
  drawButton.pendown()
  drawButton.pensize(4)
  drawButton.fillcolor('lightgreen')
  drawButton.begin_fill()
  drawButton.left(90)
  drawButton.forward(35)
  drawButton.left(90)
  drawButton.forward(170)
  drawButton.left(90)
  drawButton.forward(35)
  drawButton.left(90)
  drawButton.forward(170)
  drawButton.end_fill()
  drawButton.penup()
  drawButton.goto(-63, 80)
  drawButton.color('black')
  drawButton.write("Press P to begin", font=(None, 15, "bold"))

  highscores = turtle.Turtle()
  highscores.hideturtle()
  highscores.penup()
  highscores.speed(0)
  highscores.goto(93, 35)
  highscores.pendown()
  highscores.pensize(4)
  highscores.fillcolor('yellow')
  highscores.begin_fill()
  highscores.left(90)
  highscores.forward(23)
  highscores.left(90)
  highscores.forward(155)
  highscores.left(90)
  highscores.forward(23)
  highscores.left(90)
  highscores.forward(155)
  highscores.end_fill()
  highscores.penup()
  highscores.goto(-58, 41)
  highscores.color('black')
  highscores.write("Press H for highscores", font=(None, 10, "bold"))

wn.onkey(back, "B")

#creates the page for the instructions
def startInstructions():
  overlay = turtle.Turtle()
  overlay.speed(0)
  overlay.hideturtle()
  overlay.penup()
  overlay.pendown()
  overlay.color('black')
  overlay.dot(6000)
  wn.onkey(None)
  wn.onkey(back, "B")

wn.onkey(startInstructions, "I")

#this is where the game'll go
def startGame():
  overlay = turtle.Turtle()
  overlay.speed(0)
  overlay.hideturtle()
  overlay.penup()
  overlay.pendown()
  overlay.color('black')
  overlay.dot(6000)
  startingTurtle.penup()
  startingTurtle.hideturtle()
  startingTurtle.color('green')
  startingTurtle.goto(100, 40)
  startingTurtle.dot(25)
  wn.onkey(None)

wn.onkey(startGame, "P")

#creates the page for the highscores
def lookHighscores():
  overlay = turtle.Turtle()
  overlay.speed(0)
  overlay.hideturtle()
  overlay.penup()
  overlay.pendown()
  overlay.color('black')
  overlay.dot(6000)
  wn.onkey(None)
  wn.onkey(back, "B")

wn.onkey(lookHighscores, "H")

这是我按下字母时所得到的追溯"我"。 (当我按其他字母时,跟踪不显示):

Traceback (most recent call last):
  File "C:\Users\techs\Desktop\Alien Shooter.py", line 299, in <module>
    entrance2.speed(0)
  File "C:\Users\techs\AppData\Local\Programs\Python\Python35-32\lib\turtle.py", line 2174, in speed
    self.pen(speed=speed)
  File "C:\Users\techs\AppData\Local\Programs\Python\Python35-32\lib\turtle.py", line 2459, in pen
    self._update()
  File "C:\Users\techs\AppData\Local\Programs\Python\Python35-32\lib\turtle.py", line 2660, in _update
    self._update_data()
  File "C:\Users\techs\AppData\Local\Programs\Python\Python35-32\lib\turtle.py", line 2646, in _update_data
    self.screen._incrementudc()
  File "C:\Users\techs\AppData\Local\Programs\Python\Python35-32\lib\turtle.py", line 1292, in _incrementudc
    raise Terminator
turtle.Terminator

整个代码:

import turtle
import time

wn = turtle.Screen()
wn.bgcolor('black')
startingTurtle = turtle.Turtle()
backHigh = turtle.Turtle()

score=0
lives=3
ENTRANCE_TIME=5

time.sleep(1)

#displays the entrance screen
while ENTRANCE_TIME >= 0:
  entrance4 = turtle.Turtle()
  entrance4.clear()
  entrance = turtle.Turtle()
  entrance.hideturtle()
  entrance.penup()
  entrance.speed(0)
  entrance.color('#F54747')
  entrance.goto(-200, 400)
  entrance.write("Alien Shooter", font=(None, 50, "bold"))
  entrance44 = turtle.Turtle()
  entrance44.clear()
  entrance11 = turtle.Turtle()
  entrance11.hideturtle()
  entrance11.penup()
  entrance11.speed(0)
  entrance11.color('#F54747')
  entrance11.goto(-100, 350)
  entrance11.write("by", font=(None, 30))
  entrance11.goto(-50, 350)
  entrance11.write("Wanderer", font=(None, 30, "bold"))
  time.sleep(0.1)
  entrance11.clear()
  entrance22 = turtle.Turtle()
  entrance22.hideturtle()
  entrance22.penup()
  entrance22.speed(0)
  entrance22.color('#46F2AB')
  entrance22.goto(-100, 350)
  entrance22.write("by", font=(None, 30))
  entrance22.goto(-50, 350)
  entrance22.write("Wanderer", font=(None, 30, "bold"))
  entrance.clear()
  entrance2 = turtle.Turtle()
  entrance2.hideturtle()
  entrance2.penup()
  entrance2.speed(0)
  entrance2.color('#46F2AB')
  entrance2.goto(-200, 400)
  entrance2.write("Alien Shooter", font=(None, 50, "bold"))
  time.sleep(0.1)
  entrance2.clear()
  entrance22.clear()
  entrance33 = turtle.Turtle()
  entrance33.hideturtle()
  entrance33.penup()
  entrance33.speed(0)
  entrance33.color('#F2E446')
  entrance33.goto(-100, 350)
  entrance33.write("by", font=(None, 30))
  entrance33.goto(-50, 350)
  entrance33.write("Wanderer", font=(None, 30, "bold"))
  entrance3 = turtle.Turtle()
  entrance3.hideturtle()
  entrance3.penup()
  entrance3.speed(0)
  entrance3.color('#F2E446')
  entrance3.goto(-200, 400)
  entrance3.write("Alien Shooter", font=(None, 50, "bold"))
  time.sleep(0.1)
  entrance33.clear()
  entrance44 = turtle.Turtle()
  entrance44.hideturtle()
  entrance44.penup()
  entrance44.speed(0)
  entrance44.color('#F2466C')
  entrance44.goto(-100, 350)
  entrance44.write("by", font=(None, 30))
  entrance44.goto(-50, 350)
  entrance44.write("Wanderer", font=(None, 30, "bold"))
  entrance3.clear()
  entrance4 = turtle.Turtle()
  entrance4.hideturtle()
  entrance4.penup()
  entrance4.speed(0)
  entrance4.goto(-200, 400)
  entrance4.color('#F2466C')
  entrance.write("Alien Shooter", font=(None, 50, "bold"))
  entrance4.clear()
  ENTRANCE_TIME=ENTRANCE_TIME-1

#creates a black overlay for the whole screen
overlay = turtle.Turtle()
overlay.speed(0)
overlay.hideturtle()
overlay.penup()
overlay.pendown()
overlay.color('black')
overlay.dot(6000)

#displays the current version of the game in the bottom right corner
version = turtle.Turtle()
version.hideturtle()
version.penup()
version.speed(0)
version.goto(540, -460)
version.color('white')
version.write("Version 1.0.0", font=(None, 10, "bold"))

#creates the shadow for the flipped sideways triangle
drawButton2 = turtle.Turtle()
drawButton2.hideturtle()
drawButton2.penup()
drawButton2.speed(0)
drawButton2.goto(-165, 256)
drawButton2.pendown()
drawButton2.pensize(4)
drawButton2.fillcolor('#2C6E00')
drawButton2.color('#2C6E00')
drawButton2.begin_fill()
drawButton2.left(90)
drawButton2.fd(56)
drawButton2.right(125)
drawButton2.fd(55)
drawButton2.right(113)
drawButton2.fd(54)
drawButton2.end_fill()

#creates the flipped sideways triangle and writes the text next to it
drawButton = turtle.Turtle()
drawButton.hideturtle()
drawButton.penup()
drawButton.speed(0)
drawButton.goto(-165, 260)
drawButton.pendown()
drawButton.pensize(4)
drawButton.fillcolor('#43A800')
drawButton.color('#43A800')
drawButton.begin_fill()
drawButton.left(90)
drawButton.fd(56)
drawButton.right(125)
drawButton.fd(55)
drawButton.right(113)
drawButton.fd(54)
drawButton.end_fill()
drawButton.penup()
drawButton.goto(-95, 264)
drawButton.color('#43A800')
drawButton.write("Press P to begin", font=(None, 20, "bold"))

#creates the shadow for the crown
highscores2 = turtle.Turtle()
highscores2.hideturtle()
highscores2.penup()
highscores2.speed(0)
highscores2.goto(-165, 176)
highscores2.pendown()
highscores2.pensize(4)
highscores2.fillcolor('#D1B204')
highscores2.color('#D1B204')
highscores2.begin_fill()
highscores2.fd(56)
highscores2.left(90)
highscores2.fd(35)
highscores2.left(150)
highscores2.fd(40)
highscores2.right(135)
highscores2.fd(36)
highscores2.left(150)
highscores2.fd(36)
highscores2.right(140)
highscores2.fd(39)
highscores2.left(155)
highscores2.fd(35)
highscores2.end_fill()

#creates the crown and writes the text next to it
highscores = turtle.Turtle()
highscores.hideturtle()
highscores.penup()
highscores.speed(0)
highscores.goto(-165, 180)
highscores.pendown()
highscores.pensize(4)
highscores.fillcolor('#FFD900')
highscores.color('#FFD900')
highscores.begin_fill()
highscores.fd(56)
highscores.left(90)
highscores.fd(35)
highscores.left(150)
highscores.fd(40)
highscores.right(135)
highscores.fd(36)
highscores.left(150)
highscores.fd(36)
highscores.right(140)
highscores.fd(39)
highscores.left(155)
highscores.fd(35)
highscores.end_fill()
highscores.penup()
highscores.goto(-95, 184)
highscores.color('#FFD900')
highscores.write("Press H for highscores", font=(None, 20, "bold"))

#creates the shadow for the question mark
instructions2 = turtle.Turtle()
instructions2.hideturtle()
instructions2.penup()
instructions2.speed(0)
instructions2.goto(-165, 96)
instructions2.pendown()
instructions2.color('#B82727')
instructions2.write("?", font=(None, 40, "bold"))
instructions2.pensize(4)

#creates the question mark and writes the text next to it
instructions = turtle.Turtle()
instructions.hideturtle()
instructions.penup()
instructions.speed(0)
instructions.goto(-165, 100)
instructions.pendown()
instructions.color('#FC6060')
instructions.write("?", font=(None, 40, "bold"))
instructions.penup()
instructions.goto(-95, 104)
instructions.color('#FC6060')
instructions.write("Press I for instructions", font=(None, 20, "bold"))

#creates the blue rectangle
contacts2 = turtle.Turtle()
contacts2.hideturtle()
contacts2.penup()
contacts2.speed(0)
contacts2.goto(-280, 0)
contacts2.pensize(4)
contacts2.pendown()
contacts2.color('#00A3A6')
contacts2.fillcolor('#00A3A6')
contacts2.begin_fill()
contacts2.pendown()
contacts2.fd(600)
contacts2.left(90)
contacts2.fd(50)
contacts2.left(90)
contacts2.fd(600)
contacts2.left(90)
contacts2.fd(50)
contacts2.end_fill()
contacts2.penup()

#creates the shadow for the rectangle and writes my gmail in it for support
contacts = turtle.Turtle()
contacts.hideturtle()
contacts.penup()
contacts.speed(0)
contacts.goto(-280, 0)
contacts.pensize(4)
contacts.pendown()
contacts.color('#00D0D4')
contacts.fillcolor('#00D0D4')
contacts.begin_fill()
contacts.left(90)
contacts.fd(50)
contacts.right(90)
contacts.fd(5)
contacts.right(90)
contacts.fd(50)
contacts.right(90)
contacts.fd(5)
contacts.end_fill()
contacts.penup()
contacts.color('#FFFFFF')
contacts.goto(-270, 7)
contacts.write("Email: kybartas.matas9901@gmail.com", font=(None, 22, "bold"))

#creates the flashing title
while True:
  entrance4 = turtle.Turtle()
  entrance4.clear()
  entrance = turtle.Turtle()
  entrance.hideturtle()
  entrance.penup()
  entrance.speed(0)
  entrance.color('#F54747')
  entrance.goto(-200, 400)
  entrance.write("Alien Shooter", font=(None, 50, "bold"))
  time.sleep(0.1)
  entrance11.clear()
  entrance.clear()
  entrance2 = turtle.Turtle() #line 299
  entrance2.hideturtle()
  entrance2.penup()
  entrance2.speed(0)
  entrance2.color('#46F2AB')
  entrance2.goto(-200, 400)
  entrance2.write("Alien Shooter", font=(None, 50, "bold"))
  time.sleep(0.1)
  entrance2.clear()
  entrance3 = turtle.Turtle()
  entrance3.hideturtle()
  entrance3.penup()
  entrance3.speed(0)
  entrance3.color('#F2E446')
  entrance3.goto(-200, 400)
  entrance3.write("Alien Shooter", font=(None, 50, "bold"))
  time.sleep(0.1)
  entrance3.clear()
  entrance4 = turtle.Turtle()
  entrance4.hideturtle()
  entrance4.penup()
  entrance4.speed(0)
  entrance4.goto(-200, 400)
  entrance4.color('#F2466C')
  entrance.write("Alien Shooter", font=(None, 50, "bold"))
  entrance4.clear()

#a command for the back menu (not finished)
def back():
  drawButton = turtle.Turtle()
  drawButton.hideturtle()
  drawButton.penup()
  drawButton.speed(0)
  drawButton.goto(100, 70)
  drawButton.pendown()
  drawButton.pensize(4)
  drawButton.fillcolor('lightgreen')
  drawButton.begin_fill()
  drawButton.left(90)
  drawButton.forward(35)
  drawButton.left(90)
  drawButton.forward(170)
  drawButton.left(90)
  drawButton.forward(35)
  drawButton.left(90)
  drawButton.forward(170)
  drawButton.end_fill()
  drawButton.penup()
  drawButton.goto(-63, 80)
  drawButton.color('black')
  drawButton.write("Press P to begin", font=(None, 15, "bold"))

  highscores = turtle.Turtle()
  highscores.hideturtle()
  highscores.penup()
  highscores.speed(0)
  highscores.goto(93, 35)
  highscores.pendown()
  highscores.pensize(4)
  highscores.fillcolor('yellow')
  highscores.begin_fill()
  highscores.left(90)
  highscores.forward(23)
  highscores.left(90)
  highscores.forward(155)
  highscores.left(90)
  highscores.forward(23)
  highscores.left(90)
  highscores.forward(155)
  highscores.end_fill()
  highscores.penup()
  highscores.goto(-58, 41)
  highscores.color('black')
  highscores.write("Press H for highscores", font=(None, 10, "bold"))

wn.onkey(back, "B")

#creates the page for the instructions
def startInstructions():
  overlay = turtle.Turtle()
  overlay.speed(0)
  overlay.hideturtle()
  overlay.penup()
  overlay.pendown()
  overlay.color('black')
  overlay.dot(6000)
  wn.onkey(None)
  wn.onkey(back, "B")

wn.onkey(startInstructions, "I")

#this is where the game'll go
def startGame():
  overlay = turtle.Turtle()
  overlay.speed(0)
  overlay.hideturtle()
  overlay.penup()
  overlay.pendown()
  overlay.color('black')
  overlay.dot(6000)
  startingTurtle.penup()
  startingTurtle.hideturtle()
  startingTurtle.color('green')
  startingTurtle.goto(100, 40)
  startingTurtle.dot(25)
  wn.onkey(None)

wn.onkey(startGame, "P")

#creates the page for the highscores
def lookHighscores():
  overlay = turtle.Turtle()
  overlay.speed(0)
  overlay.hideturtle()
  overlay.penup()
  overlay.pendown()
  overlay.color('black')
  overlay.dot(6000)
  wn.onkey(None)
  wn.onkey(back, "B")

wn.onkey(lookHighscores, "H")

wn.listen()
wn.mainloop()

注意:游戏尚未完成。

提前致谢!

1 个答案:

答案 0 :(得分:0)

刚删除了你的循环while True:

wn.mainloop()更新为turtle.mainloop()

它有效。