-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdoodlejump.py
174 lines (162 loc) · 6.67 KB
/
doodlejump.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
import pygame
from pygame.locals import *
import sys
import random
class DoodleJump:
def __init__(self):
self.screen = pygame.display.set_mode((800, 600))
self.green = pygame.image.load("assets/green.png").convert_alpha()
pygame.font.init()
self.score = 0
self.font = pygame.font.SysFont("Arial", 25)
self.blue = pygame.image.load("assets/blue.png").convert_alpha()
self.red = pygame.image.load("assets/red.png").convert_alpha()
self.red_1 = pygame.image.load("assets/red_1.png").convert_alpha()
self.playerRight = pygame.image.load("assets/right.png").convert_alpha()
self.playerRight_1 = pygame.image.load("assets/right_1.png").convert_alpha()
self.playerLeft = pygame.image.load("assets/left.png").convert_alpha()
self.playerLeft_1 = pygame.image.load("assets/left_1.png").convert_alpha()
self.spring = pygame.image.load("assets/spring.png").convert_alpha()
self.spring_1 = pygame.image.load("assets/spring_1.png").convert_alpha()
self.direction = 0
self.playerx = 400
self.playery = 400
self.platforms = [[400, 500, 0, 0]]
self.springs = []
self.cameray = 0
self.jump = 0
self.gravity = 0
self.xmovement = 0
def updatePlayer(self):
if not self.jump:
self.playery += self.gravity
self.gravity += 1
elif self.jump:
self.playery -= self.jump
self.jump -= 1
key = pygame.key.get_pressed()
if key[K_RIGHT]:
if self.xmovement < 10:
self.xmovement += 1
self.direction = 0
elif key[K_LEFT]:
if self.xmovement > -10:
self.xmovement -= 1
self.direction = 1
else:
if self.xmovement > 0:
self.xmovement -= 1
elif self.xmovement < 0:
self.xmovement += 1
if self.playerx > 850:
self.playerx = -50
elif self.playerx < -50:
self.playerx = 850
self.playerx += self.xmovement
if self.playery - self.cameray <= 200:
self.cameray -= 10
if not self.direction:
if self.jump:
self.screen.blit(self.playerRight_1, (self.playerx, self.playery - self.cameray))
else:
self.screen.blit(self.playerRight, (self.playerx, self.playery - self.cameray))
else:
if self.jump:
self.screen.blit(self.playerLeft_1, (self.playerx, self.playery - self.cameray))
else:
self.screen.blit(self.playerLeft, (self.playerx, self.playery - self.cameray))
def updatePlatforms(self):
for p in self.platforms:
rect = pygame.Rect(p[0], p[1], self.green.get_width() - 10, self.green.get_height())
player = pygame.Rect(self.playerx, self.playery, self.playerRight.get_width() - 10, self.playerRight.get_height())
if rect.colliderect(player) and self.gravity and self.playery < (p[1] - self.cameray):
if p[2] != 2:
self.jump = 15
self.gravity = 0
else:
p[-1] = 1
if p[2] == 1:
if p[-1] == 1:
p[0] += 5
if p[0] > 550:
p[-1] = 0
else:
p[0] -= 5
if p[0] <= 0:
p[-1] = 1
def drawPlatforms(self):
for p in self.platforms:
check = self.platforms[1][1] - self.cameray
if check > 600:
platform = random.randint(0, 1000)
if platform < 800:
platform = 0
elif platform < 900:
platform = 1
else:
platform = 2
self.platforms.append([random.randint(0, 700), self.platforms[-1][1] - 50, platform, 0])
coords = self.platforms[-1]
check = random.randint(0, 1000)
if check > 900 and platform == 0:
self.springs.append([coords[0], coords[1] - 25, 0])
self.platforms.pop(0)
self.score += 100
if p[2] == 0:
self.screen.blit(self.green, (p[0], p[1] - self.cameray))
elif p[2] == 1:
self.screen.blit(self.blue, (p[0], p[1] - self.cameray))
elif p[2] == 2:
if not p[3]:
self.screen.blit(self.red, (p[0], p[1] - self.cameray))
else:
self.screen.blit(self.red_1, (p[0], p[1] - self.cameray))
for spring in self.springs:
if spring[-1]:
self.screen.blit(self.spring_1, (spring[0], spring[1] - self.cameray))
else:
self.screen.blit(self.spring, (spring[0], spring[1] - self.cameray))
if pygame.Rect(spring[0], spring[1], self.spring.get_width(), self.spring.get_height()).colliderect(pygame.Rect(self.playerx, self.playery, self.playerRight.get_width(), self.playerRight.get_height())):
self.jump = 50
self.cameray -= 50
def generatePlatforms(self):
on = 600
while on > -100:
x = random.randint(0,700)
platform = random.randint(0, 1000)
if platform < 800:
platform = 0
elif platform < 900:
platform = 1
else:
platform = 2
self.platforms.append([x, on, platform, 0])
on -= 50
def drawGrid(self):
for x in range(80):
pygame.draw.line(self.screen, (222,222,222), (x * 12, 0), (x * 12, 600))
pygame.draw.line(self.screen, (222,222,222), (0, x * 12), (800, x * 12))
def run(self):
clock = pygame.time.Clock()
self.generatePlatforms()
while True:
self.screen.fill((255,255,255))
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if self.playery - self.cameray > 700:
self.cameray = 0
self.score = 0
self.springs = []
self.platforms = [[400, 500, 0, 0]]
self.generatePlatforms()
self.playerx = 400
self.playery = 400
self.drawGrid()
self.drawPlatforms()
self.updatePlayer()
self.updatePlatforms()
self.screen.blit(self.font.render(str(self.score), -1, (0, 0, 0)), (25, 25))
pygame.display.flip()
DoodleJump().run()