-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrock-paper-scissors.py
180 lines (121 loc) · 3.65 KB
/
rock-paper-scissors.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'''
RULES ::
for rock = press 'r'
for scissor = press 's'
for paper = press 'p'
to end the current game = press 'e'
'''
import turtle
import random
import time
import os
#turtle screen setup
def screen_setup():
scrn = turtle.Screen()
scrn.bgcolor("#A4E4F2")
scrn.title("Rock-Paper-Scissor")
scrn.setup(height=600,width = 700)
#creating portion for player and computer
def split_screen() :
t = turtle.Turtle()
t.speed(0)
t.penup()
t.setposition(0,-300)
t.pendown()
t.hideturtle()
t.color("#01041F")
t.pensize(3)
t.left(90)
t.forward(600)
def player_enviroment() :
you.shape(paper_image)
you.penup()
you.speed(0)
you.goto(-175,0)
def computer_enviroment() :
comp.shape(paper_image)
comp.speed(0)
comp.penup()
comp.goto(175,0)
def computer_move(c) :
idx = random.randint(1,1000) % 3
global score_you
global score_comp
if idx!=c :
#rock paper
if c==0 and idx==1 : score_comp+=1
elif c==1 and idx==1 : score_you+=1
#rock scissor
elif c==0 and idx==2 : score_you+=1
elif c==2 and idx==0 : score_comp+=1
# paper scissor
elif c==1 and idx==2 : score_comp+=1
elif c==2 and idx==1 : score_you+=1
comp.shape(images[idx])
peny.clear()
peny.write(f"You: {score_you}",align="center", font = ("Comic Sans MS",22))
penc.clear()
penc.write(f"Computer: {score_comp}",align="center", font = ("Comic Sans MS",22))
def rock() :
you.shape(rock_image)
computer_move(0)
def paper() :
you.shape(paper_image)
computer_move(1)
def scissor():
you.shape(scissor_image)
computer_move(2)
def take_input() :
kb = turtle.Screen()
kb.listen()
kb.onkey(rock,"r")
kb.onkey(paper,"p")
kb.onkey(scissor,"s")
kb.onkey(end,"e")
def end() :
turtle.clearscreen()
turtle.bgcolor("#F1B28E")
sp = turtle.Turtle()
sp.hideturtle()
sp.color("#0B17BF")
if(score_comp>score_you) : sp.write("computer won!!",align="center", font = ("Comic Sans MS",30))
elif (score_comp<score_you) : sp.write("You won!!",align="center", font = ("Comic Sans MS",30))
else : sp.write("Draw",align="center", font = ("Comic Sans MS",30))
time.sleep(2)
turtle.bye()
#variables
you = turtle.Turtle()
comp = turtle.Turtle()
peny = turtle.Turtle()
penc = turtle.Turtle()
#worksteps
screen_setup()
split_screen()
# image define
rock_image= 'rock.gif'
paper_image = 'paper.gif'
scissor_image = 'scissors.gif'
images = [rock_image,paper_image,scissor_image]
turtle.register_shape(rock_image)
turtle.register_shape(paper_image)
turtle.register_shape(scissor_image)
player_enviroment()
computer_enviroment()
take_input()
score_you = 0
score_comp = 0
#initial score
peny.speed(0)
peny.penup()
peny.hideturtle()
peny.goto(-160,180)
peny.color("#390395")
peny.write("You : 0", align="center", font = ("Comic Sans MS",22))
penc.speed(0)
penc.penup()
penc.hideturtle()
penc.goto(160,180)
penc.color("#390395")
penc.write("Computer : 0", align="center", font = ("Comic Sans MS",20))
# if you can't play the game remove the following line
input("Press any key to exit ...")