SHOW:
|
|
- or go back to the newest paste.
1 | # set up for games | |
2 | ||
3 | import turtle | |
4 | import math | |
5 | import random | |
6 | ||
7 | #screen setup | |
8 | wn = turtle.Screen() | |
9 | wn.bgcolor("lightblue") | |
10 | ||
11 | ||
12 | ||
13 | # draw a border | |
14 | mypen= turtle.Turtle() | |
15 | mypen.penup() | |
16 | mypen.setposition(-300,-300) | |
17 | mypen.pendown() | |
18 | mypen.pensize(3) | |
19 | for side in range(4): | |
20 | mypen.forward(600) | |
21 | mypen.left(90) | |
22 | mypen.hideturtle() | |
23 | #create a turtle | |
24 | player= turtle.Turtle() | |
25 | player.color("blue") | |
26 | player.shape("triangle") | |
27 | player.penup() | |
28 | player.speed(0) | |
29 | ||
30 | # make more enemies | |
31 | ||
32 | maxEnemies = 6 | |
33 | enemies = [] | |
34 | ||
35 | ||
36 | ||
37 | ||
38 | #create enemies | |
39 | ||
40 | for count in range(maxEnemies) : | |
41 | enemies.append(turtle.Turtle()) | |
42 | enemies[count].color("red") | |
43 | enemies[count].shape("circle") | |
44 | enemies[count].penup() | |
45 | enemies[count].speed(0) | |
46 | enemies[count].setposition(random.randint(-300, 300),random.randint(-300, 300)) | |
47 | ||
48 | #set speed variable | |
49 | speed = 1 | |
50 | ||
51 | #define funct ions | |
52 | ||
53 | def turnleft(): | |
54 | player.left(30) | |
55 | ||
56 | def turnright(): | |
57 | player.right(30) | |
58 | ||
59 | def increasespeed(): | |
60 | global speed | |
61 | speed+= 1 | |
62 | ||
63 | def isCollision(t1,t2): | |
64 | d = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2)) | |
65 | if d < 20: | |
66 | return True | |
67 | else: | |
68 | return False | |
69 | ||
70 | ||
71 | ||
72 | #set keyboard bindings | |
73 | turtle.listen() | |
74 | turtle.onkey(turnleft,"Left") | |
75 | turtle.onkey(turnright,"Right") | |
76 | turtle.onkey(increasespeed,"Up") | |
77 | ||
78 | ||
79 | ||
80 | ||
81 | while True: | |
82 | player.forward(speed) | |
83 | ||
84 | ||
85 | ||
86 | # SET BOUNDARIES | |
87 | if player.xcor() > 300 or player.xcor() < -300: | |
88 | player.right(180) | |
89 | ||
90 | if player.ycor() > 300 or player.ycor() < -300: | |
91 | player.right(180) | |
92 | ||
93 | ||
94 | ||
95 | ||
96 | # move enemy | |
97 | for count in range(maxEnemies): | |
98 | enemies[count].forward(3) | |
99 | # set enemy boundary | |
100 | if enemies[count].xcor() > 290 or enemies[count].xcor() < -290: | |
101 | enemies[count].right(180) | |
102 | ||
103 | if enemies[count].ycor() > 290 or enemies[count].ycor() < -290: | |
104 | enemies[count].right(180) | |
105 | #collision checking | |
106 | if isCollision(player, enemies[count]): | |
107 | enemies[count].setposition(random.randint(-300, 300),random.randint(-300, 300)) | |
108 | enemies[count].right(random.randint(0,360)) | |
109 | ||
110 | ||
111 | ||
112 | delay = input("Press enter to finish.") | |
113 | ||
114 | ||
115 | ||
116 | File "/Users/johngrumbrechtiv/Desktop/python_work/turtle_game.py", line 82, in <module> | |
117 | player.forward(speed) | |
118 | File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/turtle.py", line 1637, in forward | |
119 | self._go(distance) | |
120 | File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/turtle.py", line 1605, in _go | |
121 | self._goto(ende) | |
122 | File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/turtle.py", line 3158, in _goto | |
123 | screen._pointlist(self.currentLineItem), | |
124 | File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/turtle.py", line 755, in _pointlist | |
125 | cl = self.cv.coords(item) | |
126 | File "<string>", line 1, in coords | |
127 | File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2466, in coords | |
128 | self.tk.call((self._w, 'coords') + args))] | |
129 | _tkinter.TclError: invalid command name ".!canvas" | |
130 | [Finished in 13.4s with exit code 1] | |
131 | [cmd: ['/usr/local/bin/python3', '-u', '/Users/johngrumbrechtiv/Desktop/python_work/turtle_game.py']] | |
132 | [dir: /Users/johngrumbrechtiv/Desktop/python_work] | |
133 | [path: /Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin] |