repl.it
@Pyth0n/

space game

Python (with Turtle)

No description

fork
loading
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import turtle
import math
import random
from time import sleep

window = turtle.Screen()
window.bgcolor("dark blue")
window.tracer(3)

border = turtle.Turtle()
border.speed(50)
border.penup()
border.setposition(-300,-300)
border.pendown()
border.pensize(3)
for side in range (4):
    border.forward(600)
    border.left(90)
border.hideturtle()    

mypen = turtle.Turtle()
mypen.hideturtle()
mypen.penup()
mypen.setundobuffer(1000)

mypen2 = turtle.Turtle()
mypen2.hideturtle()
mypen2.penup()
mypen2.setundobuffer(1000)

img = "rocketship.png"
player = turtle.Turtle()
player.color("yellow")
player.shape(img)
player.penup()                
player.speed(0)                   
player2 = turtle.Turtle()
player2.color("white")
player2.shape("star")
player2.penup()               
player2.speed(0)


speed = 1
speed2 = 1

score = 0
score2 = 0

maxGoals = 6
goals = []

for count in range(maxGoals):
    goals.append(turtle.Turtle())
    goals[count].color("red")
    goals[count].shape("circle")
    goals[count].speed(0)
    goals[count].penup()
    goals[count].right(random.randint(0,360))
    goals[count].setposition(random.randint(-300,300), random.randint(-300, 300))

def turnLeft():
    player.left(30)
def turnRight():
    player.right(30)
def speedUp():
    global speed
    speed = speed + 1
    speedMax()
def slowDown():
    global speed
    speed = speed - 1
    speedMin()

def Left():
    player2.left(30)
def Right():
    player2.right(30)
def speedUp2():
    global speed2
    speed2 = speed2 + 1
    speedMax2()
def slowDown2():
    global speed2
    speed2 = speed2 - 1
    speedMin2()


def isCollison(turtle1,turtle2):
    d = math.sqrt(math.pow(turtle1.xcor() - turtle2.xcor(),2) + math.pow(turtle1.ycor() - turtle2.ycor(),2))
    if d < 20:
        return True
    else:
        return False
def checkBoundary(turtle):
    if turtle.xcor() > 290 or turtle.xcor() < -290: 
        turtle.right(180)
    if turtle.ycor() > 290 or turtle.ycor() < -290:
        turtle.right(180)

def ScorePlayer1():
 global border
 global goals
 global score
 global scoreTally
 if score == 10:
   print('Player 1, You Win!!!')
   for turtle in (window.turtles()):
     turtle.reset()
     turtle.ht()
     turtle.penup()
     Victory1 = "Player 1 Wins!!!!!"
     mypen.write(Victory1, False, align = "center", font = ("Times New Roman", 24, "normal"))
     
   import turtle
   window2 = turtle.Screen()
   window2.bgcolor("dark blue")
   exit()

def ScorePlayer2():
 global border
 global goals
 global score
 global scoreTally
 if score2 == 10:
   print('Player 2, You Win!!!')
   for turtle in (window.turtles()):
     turtle.reset()
     turtle.ht()
     turtle.penup()
     Victory1 = "Player 2 Wins!!!!!"
     mypen2.write(Victory1, False, align = "center", font = ("Times New Roman", 24, "normal"))
     
   import turtle
   window2 = turtle.Screen()
   window2.bgcolor("dark blue")
   exit()

def speedMax():
  global speed
  if speed > 10:
    speed = 10

def speedMax2():
  global speed2
  if speed2 > 10:
    speed2 = 10

def speedMin():
  global speed
  if speed < 0:
    speed = 0

def speedMin2():
  global speed2
  if speed2 < 0:
    speed2 = 0


window.onkey(turnLeft, "a")
window.onkey(turnRight, "d")
window.onkey(speedUp, "w")
window.onkey(slowDown, "s")

window.onkey(Left, "Left")
window.onkey(Right, "Right")
window.onkey(speedUp2, "Up")
window.onkey(slowDown2, "Down")
window.listen()

while True:
 player.forward(speed)
 player2.forward(speed2)
 
 checkBoundary(player)
 checkBoundary(player2)

 for count in range(maxGoals):        
    checkBoundary(goals[count])
    goals[count].forward(3)
    
    if(isCollison(player, goals[count])):
        goals[count].setposition(random.randint(-300,300), random.randint(-300, 300))
        goals[count].right(random.randint(0,360))
        score = score + 1
        print score
        mypen.undo()
        mypen.setposition(-290, 310)
        scoreTally = "Score: %s" %score
        mypen.write(scoreTally, False, align = "left", font = ("Arial", 14, "normal"))
        ScorePlayer1()

    if(isCollison(player2, goals[count])):
        goals[count].setposition(random.randint(-300,300), random.randint(-300, 300))
        goals[count].right(random.randint(0,360))
        score2 = score2 + 1
        print score2
        mypen2.undo()
        mypen2.setposition(220, 310)
        scoreTally = "Score: %s" %score2
        mypen2.write(scoreTally, False, align = "left", font = ("Arial", 14, "normal"))
        ScorePlayer2()
result
console
?