-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry_game_v1.py
87 lines (61 loc) · 2.23 KB
/
geometry_game_v1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from random import randint
import turtle
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def falls_in_rectangle(self, rectangle):
if rectangle.point1.x < self.x < rectangle.point2.x \
and rectangle.point1.y < self.y < rectangle.point2.y:
return True
else:
return False
class Rectangle:
def __init__(self, point1, point2):
self.point1 = point1
self.point2 = point2
def area(self):
return (self.point2.x - self.point1.x) * \
(self.point2.y - self.point1.y)
class GuiRectangle(Rectangle):
def draw(self, canvas):
canvas.penup()
canvas.goto(self.point1.x, self.point1.y)
canvas.pendown()
canvas.forward(self.point2.x - self.point1.x)
canvas.left(90)
canvas.forward(self.point2.y - self.point1.y)
canvas.left(90)
canvas.forward(self.point2.x - self.point1.x)
canvas.left(90)
canvas.forward(self.point2.y - self.point1.y)
class GuiPoint(Point):
def draw(self, canvas, size=5, color='red'):
canvas.penup()
canvas.goto(self.x, self.y)
canvas.pendown()
canvas.dot(size , color)
# gui_rectangle = GuiRectangle(Point(randint(0, 400), randint(0, 400)),
# Point(randint(10, 400), randint(10, 400)))
# myturtle = turtle.Turtle()
# gui_rectangle.draw(canvas=myturtle)
# print(gui_rectangle.area())
# Create rectangle object
rectangle = GuiRectangle(Point(randint(0, 400), randint(0, 400)),
Point(randint(10, 400), randint(10,400)))
# Print rectangle coordinates
print("Rectangle Coordinates: ",
rectangle.point1.x, ",",
rectangle.point1.y, "and",
rectangle.point2.x, ",",
rectangle.point2.y)
# Get point and area from user
user_point = GuiPoint(float(input("Guess x: ")), float(input("Guess y: ")))
user_area = float(input("Guess rectangle area: "))
# Print out the game result
print("Your point was inside rectangle: ", user_point.falls_in_rectangle(rectangle))
print("Your area was off by: ", rectangle.area() - user_area)
myturtle = turtle.Turtle()
rectangle.draw(canvas=myturtle)
user_point.draw(canvas=myturtle)
turtle.done()