-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
121 lines (106 loc) · 3.97 KB
/
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
import pygame
def checkCollision(x, y, treasurex, treasurey, window):
collision_state = False
if y >= treasurey and y <= treasurey + 40:
if x >= treasurex and x <= treasurex + 35:
y = 650
collision_state = True
elif x + 35 >= treasurex and x + 35 <= treasurex + 35:
y = 650
collision_state = True
elif y + 40 >= treasurey and y + 40 <= treasurey + 40:
if x >= treasurex and x <= treasurex + 35:
y = 650
collision_state = True
elif x + 35 >= treasurex and x + 35 <= treasurex + 35:
y = 650
collision_state = True
return collision_state, y
pygame.init()
window = pygame.display.set_mode((900, 700))
font = pygame.font.SysFont("comicsans", 80)
level = 1
finished = False
x = 450 - 35/2
y = 650
playerimg = pygame.image.load("player.png")
playerimg = pygame.transform.scale(playerimg, (35, 40))
playerimg = playerimg.convert_alpha()
bgimg = pygame.image.load("background.png")
bgimg = pygame.transform.scale(bgimg, (900, 700))
window.blit(bgimg, (0, 0))
treasureimg = pygame.image.load("treasure.png")
treasureimg = pygame.transform.scale(treasureimg, (35, 40))
treasureimg = treasureimg.convert_alpha()
enemyimg = pygame.image.load("enemy.png")
enemyimg = pygame.transform.scale(enemyimg, (35,40))
enemyimg = enemyimg.convert_alpha()
treasurex = 450 - 35/2
treasurey = 50
enemyx = 100
enemyy = 570
window.blit(treasureimg, (treasurex, treasurey))
enemynames = {0:"Max", 1:"Jill", 2:"Greg", 3:"Diane"}
frame = pygame.time.Clock()
collision_treasure = False
enemyright = True
collision_enemy = False
name = ""
enemies = [(enemyx, enemyy, enemyright)]#(enemyx, enemyy, enemyright)
while finished == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finished = True
keypress = pygame.key.get_pressed()
enemyIndex = 0
for enemyx, enemyy, enemyright in enemies:
if enemyx >= 800-35:
enemyright = False
elif enemyx <= 100:
enemyright = True
if enemyright == True:
enemyx += 5*level
else:
enemyx -= 5*level
enemies[enemyIndex] = (enemyx, enemyy, enemyright)
enemyIndex += 1
if keypress[pygame.K_SPACE] == 1:
y -= 5
#dorect = pygame.Rect(x, y, 30, 30)
rectcolor = (0, 0, 255)
#white = (255, 255, 255)
window.blit(bgimg, (0, 0))
window.blit(treasureimg, (treasurex, treasurey))
window.blit(playerimg, (x, y))
enemyIndex = 0
for enemyx, enemyy, enemyright in enemies:
window.blit(enemyimg, (enemyx, enemyy))
collision_enemy,y = checkCollision(x, y, enemyx, enemyy, window)
if collision_enemy == True:
name = enemynames[enemyIndex]
textDead = font.render("You died!",True,(255,0,0))
textKilled = font.render("You were killed by "+name,True,(255,0,0))
window.blit(textDead, (450 - textDead.get_width()/2,250 - textDead.get_height()/2))
window.blit(textKilled, (450 - textKilled.get_width()/2,300 - textKilled.get_height()/2))
pygame.display.flip()
frame.tick(1)
frame.tick(50)
enemyIndex += 1
collision_treasure,y = checkCollision(x, y, treasurex, treasurey, window)
#collision_enemy,y = checkCollision(x, y, enemyx, enemyy, window)
if collision_treasure == True:
level += 1
enemies.append((enemyx-50*level, enemyy-50*level, False))
winText = font.render("You've reached Level"+str(level),True, (0,0,0))
window.blit(winText, (450 - winText.get_width()/2,300))
pygame.display.flip()
frame.tick(1)
#elif collision_enemy == True:
# winText = font.render("You died! :( " ,True, (0,0,0))
# window.blit(winText, (450 - winText.get_width()/2,200))
# collision_enemy = False
# pygame.display.flip()
# frame.tick(1)
#pygame.draw.rect(window, rectcolor, dorect)
pygame.display.flip()
frame.tick(30)