-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageExercise.py
More file actions
130 lines (109 loc) · 3.69 KB
/
imageExercise.py
File metadata and controls
130 lines (109 loc) · 3.69 KB
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
import sys
import pygame
def getkey():
for event in pygame.event.get():
if event.type == pygame.QUIT:
# pygame.image.save(window, "game-over.bmp")
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
sys.exit()
if event.key in controls:
key = controls[event.key]
return key
def ferry(who, step):
done = False
for actor in who:
actor["rect"] = actor["rect"].move((step, 0))
if not arena.contains(actor["rect"]):
actor["rect"] = actor["rect"].move((-step, 0))
actor["surf"] = pygame.transform.flip(actor["surf"], True, False)
done = True
return done
def failure():
myfont = pygame.font.Font('freesansbold.ttf', 48)
msg = myfont.render("Failure", True, (255, 0, 0))
msg_box = msg.get_rect()
msg_box.center = arena.center
window.blit(msg, msg_box)
pygame.display.flip()
pygame.time.wait(1000)
def success():
myfont = pygame.font.Font('freesansbold.ttf', 48)
msg = myfont.render("Success", True, (255, 255, 255))
msg_box = msg.get_rect()
msg_box.center = arena.center
window.blit(msg, msg_box)
pygame.display.flip()
pygame.time.wait(1000)
pygame.init()
window = pygame.display.set_mode((640, 480))
arena = window.get_rect()
wolf = {"file": "wolf.png"}
goat = {"file": "goat.png"}
cabb = {"file": "cabb.png"}
farm = {"file": "farm.png"}
actors = [wolf, goat, cabb, farm]
for i, actor in enumerate(actors):
actor["surf"] = pygame.image.load(actor["file"])
actor["rect"] = actor["surf"].get_rect()
actor["rect"].midleft = (0, (i+1)*arena.height/5)
gamegraph = {
"wgcf-": {"f": "wgc-f", "w": "gc-wf", "g": "wc-gf", "c": "wg-cf"},
"wc-gf": {"f": "wcf-g", "g": "wgcf-"},
"wcf-g": {"f": "wc-gf", "c": "w-gcf", "w": "c-wgf"},
"w-gcf": {"g": "wgf-c", "c": "wcf-g"},
"c-wgf": {"g": "gcf-w", "w": "wcf-g"},
"wgf-c": {"w": "g-wcf", "g": "w-gcf"},
"gcf-w": {"c": "g-wcf", "g": "c-wgf"},
"g-wcf": {"f": "gf-wc", "c": "gcf-w", "w": "wgf-c"},
"gf-wc": {"g": "-wgcf", "f": "g-wcf"},
"wgc-f": "failure",
"cf-wg": "failure",
"gc-wf": "failure",
"wf-gc": "failure",
"wg-cf": "failure",
"f-wgc": "failure",
"-wgcf": "success"
}
gamestate = "wgcf-"
controls = {pygame.K_f: "f",
pygame.K_g: "g",
pygame.K_w: "w",
pygame.K_c: "c"}
passengers = {"f": [farm],
"g": [farm, goat],
"w": [farm, wolf],
"c": [farm, cabb]}
ferry_step = -5
action = "listen"
fpsClock = pygame.time.Clock()
while True:
if action == "listen":
key = getkey()
if key in gamegraph[gamestate]:
gamestate = gamegraph[gamestate][key]
ferry_who = passengers[key]
ferry_step = -ferry_step
action = "ferry"
if action == "ferry":
done = ferry(ferry_who, ferry_step)
if done:
if gamegraph[gamestate] == "failure":
action = "failure"
elif gamegraph[gamestate] == "success":
print("does it work LOLOL")
action = "success"
else:
action = "listen"
if action == "failure":
failure()
sys.exit()
if action == "success":
success()
sys.exit()
window.fill(pygame.Color("green"))
for actor in actors:
window.blit(actor["surf"], actor["rect"])
pygame.display.flip()
fpsClock.tick(120)