-
Notifications
You must be signed in to change notification settings - Fork 0
/
16-stickmangame before challenges.py
144 lines (129 loc) · 4.49 KB
/
16-stickmangame before challenges.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
from tkinter import *
import random
import time
class Game:
def __init__(self):
self.tk = Tk()
self.tk.title("Mr. Stick Man Races for the Exit")
self.tk.resizable(0,0)
self.tk.wm_attributes("-topmost", 1)
self.canvas = Canvas(self.tk, width=500, height=500, highlightthickness=0)
self.canvas.pack()
self.tk.update()
self.canvas_height = 500
self.canvas_width = 500
self.bg = PhotoImage(file="background.gif")
self.alternate = True
w = self.bg.width()
h = self.bg.height()
for x in range(0, 5):
for y in range (0, 5):
if self.alternate == True:
self.canvas.create_image(x * w, y * h, image=self.bg, anchor='nw')
self.alternate = False
else:
# self.canvas.create_image(x * w, y * h, image=self.bg, anchor='nw')
self.alternate = True
self.sprites = []
self.running = True
def mainloop(self):
while 1:
if self.running == True:
for sprite in self.sprites:
sprite.move()
self.tk.update_idletasks()
self.tk.update()
self.tk.update()
time.sleep(0.01)
class Coords:
def __init__(self, x1=0, y1=0, x2=0, y2=0):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def within_x(co1, co2):
if (co1.x1 > co2.x1 and co1.x1 < co2.x2) \
or (co1.x1 > co2.x1 and co1.x1 < co2.x2) \
or (co2.x1 > co1.x1 and co2.x1 < co1.x2) \
or (co2.x2 > co1.x1 and co2.x2 < co1.x2):
return True
else:
return False
def within_y(co1, co2):
if (co1.y1 > co2.y1 and co1.y1 < co2.y2) \
or (co1.y1 > co2.y1 and co1.y1 < co2.y2) \
or (co2.y1 > co1.y1 and co2.y1 < co1.y2) \
or (co2.y2 > co1.y1 and co2.y2 < co1.y2):
return True
else:
return False
def collided_left(co1, co2):
if within_y(co1, c02):
if co1.x1 <= co2.y2 and co1.y1 >= co2.y1:
return True
return False
def collided_right(co1, co2):
if within_y(co1, c02):
if co1.x2 >= co2.x2 and co1.x2 >= co2.x2:
return True
return False
def collided_top(co1, co2):
if within_x(co1, c02):
if co1.y1 >= co2.y2 and co1.y1 >= co2.y1:
return True
return False
def collided_bottom(y, co1, co2):
if within_x(co1, c02):
y_calc = co1.y2 + y
if y_calc >= co2.y1 and y_calc <= co2.y2:
return True
return False
class Sprite:
def __init__(self, game):
self.game = game
self.endgame = False
self.coordinates = None
def move(self):
pass
def coords(self):
return self.coordinates
class PlatformSprite(Sprite):
def __init__(self, game, photo_image, x, y, width, height):
Sprite.__init__(self, game)
self.photo_image = photo_image
self.image = game.canvas.create_image(x, y, \
image=self.photo_image, anchor='nw')
self.coordinates = Coords(x, y, x + width, y + height)
g = Game()
# Define platform positions for game
platform1 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
0, 480, 100, 10)
platform2 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
150, 440, 100, 10)
platform3 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
300, 400, 100, 10)
platform4 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
300, 160, 100, 10)
platform5 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
175, 350, 66, 10)
platform6 = PlatformSprite(g, PhotoImage(file="platform2.gif"), \
50, 300, 66, 10)
platform7 = PlatformSprite(g, PhotoImage(file="platform2.gif"), \
170, 120, 66, 10)
platform8 = PlatformSprite(g, PhotoImage(file="platform2.gif"), \
45, 60, 66, 10)
platform9 = PlatformSprite(g, PhotoImage(file="platform2.gif"), \
170, 250, 32, 10)
platform10 = PlatformSprite(g, PhotoImage(file="platform3.gif"), \
230, 200, 32, 10)
g.sprites.append(platform1)
g.sprites.append(platform2)
g.sprites.append(platform3)
g.sprites.append(platform4)
g.sprites.append(platform5)
g.sprites.append(platform6)
g.sprites.append(platform7)
g.sprites.append(platform8)
g.sprites.append(platform9)
g.sprites.append(platform10)
g.mainloop()