[game] bug escape

Loneliness, Depression & Relationship Forum

Help Support Loneliness, Depression & Relationship Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.

mickey

Well-known member
Joined
Jan 4, 2015
Messages
432
Reaction score
2
As part of an exercise I wrote a simple bug race program in which three differently-colored bugs wander randomly around the screen until all three have "escaped" the screen by going past its edge. You can play by yourself or with other people by guessing which bug will escape first, or last, or in which order, or how many moves it will take them, etc.

To run the game you need to have Python 3.43 installed, which you can get from python.org. Then just copy and paste the following script into a textfile, save the file with a .py extension, and double-click it in your file manager.

Here's the script:

Code:
import turtle
import random

def isonscreen(t):
    x, y = t.xcor(), t.ycor()
    return abs(x) < xw and abs(y) < yw

def randomwalk(t):
    t.left(random.randrange(-180, 180))
    t.forward(random.randrange(25,76))

window = turtle.Screen()
window.bgcolor("yellow")
xw, yw = window.window_width()/2, window.window_height()/2

turt1, turt2, turt3 = turtle.Turtle(), turtle.Turtle(), turtle.Turtle()
for (x,y) in [(turt1, "blue"), (turt2, "red"), (turt3, "green")]:
    x.color(y)
    x.shape("turtle")
    x. pensize(3)

z, a, b = True, True, True
while z or a or b:
    if isonscreen(turt1):
        randomwalk(turt1)
    else:
        z = False
    if isonscreen(turt2):
        randomwalk(turt2)
    else: 
        a = False
    if isonscreen(turt3):
        randomwalk(turt3)
    else:
        b = False

window.exitonclick()
 

Latest posts

Back
Top