-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_the_point.py
106 lines (85 loc) · 3.45 KB
/
guess_the_point.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from random import randint
import turtle
from time import sleep
def game():
"""The game of searching point.
Rules:
1. At first the program shows coordinates of the rectangle to the user (they are randomly selected),
then the user has to guess the point from inside the rectangle by enter the coordinates of the point.
2. The coordinates of the points can be randomized (and guessing) from the range -200:200.
"""
random_rectangle = Rectangle()
print("Welcome to the game! Your task is to choose the point that falls in the rectangle.")
print(f"The coordinates of our rectangle are: {random_rectangle.show_both_points()}\n")
coordinate_x = Coordinate.validation('first')
coordinate_y = Coordinate.validation('second')
random_rectangle.draw_rectangle_and_point(coordinate_x, coordinate_y)
user_game = Game((coordinate_x, coordinate_y))
print("\n")
print(user_game.check_the_point(random_rectangle.show_both_points()))
class Coordinate:
@staticmethod
def validation(number_of_coordinate_in_string):
while True:
coordinate = input(f"Type {number_of_coordinate_in_string} coordinate from the scope -200:200: ")
try:
coordinate = int(coordinate)
if -200 < coordinate < 200:
return coordinate
else:
print("You entered the wrong value!")
continue
except ValueError:
print("You entered the wrong value!")
continue
class Rectangle:
# class for choose two random points of the rectangle
def __init__(self):
self.point1 = randint(-200, 200), randint(-200, 200)
self.point2 = self.setting_point2()
def setting_point2(self):
point2_x = randint(-200, 200)
point2_y = randint(-200, 200)
while point2_x == self.point1[0]:
point2_x = randint(-200, 200)
while point2_y == self.point1[1]:
point2_y = randint(-200, 200)
point2 = point2_x, point2_y
return point2
def show_both_points(self):
return self.point1, self.point2
def draw_rectangle_and_point(self, user_point_x, user_point_y):
screen = turtle.Screen()
screen.setup(500, 500)
screen.title('RECTANGLE')
t = turtle.Turtle()
t.hideturtle()
t.up()
t.pensize(3)
point1 = self.point1[0], self.point1[1]
point2 = self.point2[0], self.point2[1]
t.goto(point1[0], point1[1])
t.down()
t.goto(point2[0], point1[1])
t.goto(point2[0], point2[1])
t.goto(point1[0], point2[1])
t.goto(point1[0], point1[1])
t.up()
t.goto(user_point_x, user_point_y)
t.dot(7, "red")
sleep(5)
class Game:
# check if the user's point falls in the selected rectangle, return True or False
def __init__(self, user_point): # user_point -> tuple
self.user_point = user_point
def check_the_point(self, points):
point1 = points[0]
point2 = points[1]
scope_x = min(point1[0], point2[0]), max(point1[0], point2[0])
scope_y = min(point1[1], point2[1]), max(point1[1], point2[1])
if scope_x[0] < self.user_point[0] < scope_x[1] and scope_y[0] < self.user_point[1] < scope_y[1]:
return "Yes, you're right!"
else:
return "Unfortunately, you gave the wrong coordinates :("
if __name__ == '__main__':
game()