I am running a Linux mint for the first time . I tried coding a python problem but for two days I am continiously facing problems due to Linux interface please
This is my code:-
import turtleimport timeboxsize=200caught=Falsescore=0#function that are called in keypressdef up():mouse.forward(10)checkbound()def left():move.left(45)def right():move.right(45)def back():mouse.backward(10)checkbound()def quitTurtles():window.bye()#stop the mouse from leaving the square set sizedef checkbound():global boxsizeif mouse.xcor()>boxsize:mouse.goto(boxsize, mouse.ycor())if mouse.xcor()<-boxsize:mouse.goto(-boxsize, mouse.ycor())if mouse.ycor()>boxsize:mouse.goto(mouse.xcor(),boxsize)if mouse.ycor()<-boxsize:mouse.goto(mouse.xcor(),-boxsize)#set up screenwindow=turtle.Screen()mouse=turtle.Turtle()cat=turtle.Turtle()mouse.penup()mouse.penup()mouse.goto(100,100)#add key listenerswindow.onkeypress(up ,'UP')window.onkeypress(right ,'left')window.onkeypress(left ,'Right')window.onkeypress(back ,'DOWN')window.onkeypress(quitTurtles, "Escape")difficulty=window.numinput("difficulty","Enter a difficulty from 1 to 5",minval=1,maxval=5)window.listen()#main loop#note how it changes with difficultywhile not caught:cat.setheading(cat.towards(mouse))cat.forward(8+diffficulty)score=score+1if cat.distance(mouse)<5:caught=truetime.sleep(0.2-(0.1*difficulty))window.textinput("GAME OVER","WELL DONE YOU SCORED:"+str(score*difficulty))window.bye()
This code has several problems, many of which will keep it from running correctly:
Substituted move
for mouse
:
def up():mouse.forward(10)checkbound()def left():move.left(45)
Unnecessary global
declaration as boxsize
is not assigned:
def checkbound():global boxsize
In code copy-and-paste, didn't change mouse
to cat
:
mouse=turtle.Turtle()
cat=turtle.Turtle()
mouse.penup()
mouse.penup()
The difficulty
variable not spelled consistently:
cat.forward(8+diffficulty)time.sleep(0.2-(0.1*difficulty))
Incorrect case for boolean:
caught=true
As noted in comments, total inconsistency in key naming case:
window.onkeypress(right ,'left')
window.onkeypress(left ,'Right')
window.onkeypress(back ,'DOWN')
Bigger picture issues are use of sleep()
in an event-driven environment and lack of drawn boundaries so player knows the limits. Rather than address these issues one by one in SO questions, let's rework this code to work within the turtle event environment and be playable as a game:
from turtle import Screen, TurtleBOX_SIZE = 600# functions that are called in keypress
def up():mouse.forward(15)checkbound()def left():mouse.left(45)def right():mouse.right(45)def back():mouse.backward(15)checkbound()def checkbound():''' stop the mouse from leaving the square set size '''if mouse.xcor() > BOX_SIZE/2:mouse.goto(BOX_SIZE/2, mouse.ycor())elif mouse.xcor() < -BOX_SIZE/2:mouse.goto(-BOX_SIZE/2, mouse.ycor())if mouse.ycor() > BOX_SIZE/2:mouse.goto(mouse.xcor(), BOX_SIZE/2)elif mouse.ycor() < -BOX_SIZE/2:mouse.goto(mouse.xcor(), -BOX_SIZE/2)def move():global scorecat.setheading(cat.towards(mouse))cat.forward(2 * difficulty)score += 1if cat.distance(mouse) < 5:screen.textinput("GAME OVER", "WELL DONE YOU SCORED: {}".format(score * difficulty))screen.bye()else:screen.ontimer(move, 200 - 100 * difficulty)score = 0# set up screen
screen = Screen()marker = Turtle()
marker.hideturtle()
marker.penup()
marker.goto(-BOX_SIZE/2, -BOX_SIZE/2)
marker.pendown()
for _ in range(4):marker.forward(BOX_SIZE)marker.left(90)difficulty = int(screen.numinput("difficulty", "Enter a difficulty from 1 to 5", minval=1, maxval=5))cat = Turtle()
cat.shapesize(2)
cat.penup()mouse = Turtle()
mouse.penup()
mouse.goto(200, 200)# add key listeners
screen.onkeypress(up, 'Up')
screen.onkeypress(right, 'Left')
screen.onkeypress(left, 'Right')
screen.onkeypress(back, 'Down')
screen.onkeypress(screen.bye, 'Escape')
screen.listen()screen.ontimer(move, 1000) # give player a chance to move hand from keyboard to mousescreen.mainloop()