Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 96ab6f1

Browse files
version:1.0.03
""" ------------------------------------------------------------------------------- ** COMPLETED VIDEO #1 ** Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement https://www.youtube.com/watch?v=PjgLeP0G5Yw&t=139s ** COMPLETED VIDEO #2 ** Pygame Side-Scroller Tutorial #2 - Random Object Generation https://www.youtube.com/watch?v=fHlJNjRRXWY TIME STAMP: STOPPED VIDEO @ 16:52 / 19:20 - TIME: 10:52am 3/27/21 Saturday ** READY FOR VIDEO #3 ** Pygame Side-Scroller Tutorial #3 - Collision https://www.youtube.com/watch?v=qTw0lYqTQSU TIME STAMP: STOPPED VIDEO @ 00:00 / 00:00 - TIME: 00:00 / / ------------------------------------------------------------------------------- """
1 parent 9f12f43 commit 96ab6f1

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed

‎running-man-game.py‎

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
import pygame
2+
from pygame.locals import *
3+
import os
4+
import sys
5+
import math
6+
import random
7+
8+
pygame.init()
9+
10+
W, H = 800, 447
11+
win = pygame.display.set_mode((W, H))
12+
pygame.display.set_caption('Side Scroller')
13+
14+
# this is the scrolling background pics...
15+
bg = pygame.image.load(os.path.join('images', 'bg.png')).convert()
16+
bgX = 0
17+
bgX2 = bg.get_width()
18+
19+
clock = pygame.time.Clock() # provides the movement flow...
20+
21+
22+
"""
23+
-----------------------------------------------------------------------------------------------------------------------
24+
** CLASSES BEGIN **
25+
-----------------------------------------------------------------------------------------------------------------------
26+
"""
27+
28+
# PLAYER CLASS
29+
30+
31+
class player(object):
32+
# corresponding images for such actions...
33+
run = [pygame.image.load(os.path.join('images', str(x) + '.png')) for x in range(8, 16)]
34+
jump = [pygame.image.load(os.path.join('images', str(x) + '.png')) for x in range(1, 8)]
35+
slide = [pygame.image.load(os.path.join('images', 'S1.png')), pygame.image.load(os.path.join('images', 'S2.png')),
36+
pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')),
37+
pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')),
38+
pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')),
39+
pygame.image.load(os.path.join('images', 'S3.png')), pygame.image.load(os.path.join('images', 'S4.png')),
40+
pygame.image.load(os.path.join('images', 'S5.png'))]
41+
# jumpList has to do with jumping up and down...
42+
jumpList = [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
43+
4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1,
44+
-1, -1, -1, -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
45+
-3, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4]
46+
47+
def __init__(self, x, y, width, height): # constructor for the player...
48+
self.x = x
49+
self.y = y
50+
self.width = width
51+
self.height = height
52+
self.jumping = False
53+
self.sliding = False
54+
self.slideCount = 0
55+
self.jumpCount = 0
56+
self.runCount = 0
57+
self.slideUp = False
58+
59+
def draw(self, win):
60+
if self.jumping:
61+
self.y -= self.jumpList[self.jumpCount] * 1.2 # has to do with the 'jumping math' (jumping algorithm)
62+
win.blit(self.jump[self.jumpCount // 18], (self.x, self.y))
63+
self.jumpCount += 1
64+
if self.jumpCount > 108:
65+
self.jumpCount = 0
66+
self.jumping = False
67+
self.runCount = 0
68+
elif self.sliding or self.slideUp:
69+
if self.slideCount < 20:
70+
self.y += 1
71+
elif self.slideCount == 80:
72+
self.y -= 19
73+
self.sliding = False
74+
self.slideUp = True
75+
if self.slideCount >= 110:
76+
self.slideCount = 0
77+
self.slideUp = False
78+
self.runCount = 0
79+
win.blit(self.slide[self.slideCount // 10], (self.x, self.y))
80+
self.slideCount += 1
81+
82+
else:
83+
if self.runCount > 42:
84+
self.runCount = 0
85+
win.blit(self.run[self.runCount // 6], (self.x, self.y))
86+
self.runCount += 1
87+
88+
89+
# SAW CLASS
90+
91+
92+
class saw(object):
93+
img = [pygame.image.load(os.path.join('images', 'SAW0.png')),pygame.image.load(os.path.join('images', 'SAW1.png')),pygame.image.load(os.path.join('images', 'SAW2.png')),pygame.image.load(os.path.join('images', 'SAW3.png'))]
94+
95+
def __init__(self, x, y, width, height): # constructor and/or initialization method for the saw object...
96+
self.x = x
97+
self.y = y
98+
self.width = width
99+
self.height = height
100+
self.hitbox = (x,y,width,height) # give the hitbox a rectangle format or shape...
101+
self.count = 0
102+
103+
def draw(self, win): # the math here moves the saw up and down
104+
self.hitbox = (self.x + 5, self.y + 5, self.width - 10, self.height) # THE BELOW MATH EXPLANATION:
105+
if self.count >= 8: # for every 2 frames, we'll draw 1 frame of the saw so that it doesn't spin too fast...
106+
self.count = 0 # used as the index number for the saw images...
107+
# scale down the image of the saw...
108+
win.blit(pygame.transform.scale(self.img[self.count//2], (64, 64)), (self.x, self.y)) # using integer division
109+
self.count += 1
110+
pygame.draw.rect(win, (255,0,0), self.hitbox, 2) # draw a thick red hitbox...
111+
112+
113+
# SPIKE CLASS
114+
115+
116+
class spike(saw): # spike class inherits from saw class...so it will
117+
# have the same properties in the saw's constructor...
118+
119+
img = pygame.image.load(os.path.join('images', 'spike.png'))
120+
121+
def draw(self,win):
122+
self.hitbox = (self.x + 10, self.y, 28, 315)
123+
win.blit(self.img, (self.x, self.y))
124+
pygame.draw.rect(win, (255,0,0),self.hitbox,2)
125+
126+
127+
"""
128+
-----------------------------------------------------------------------------------------------------------------------
129+
** CLASSES END **
130+
-----------------------------------------------------------------------------------------------------------------------
131+
"""
132+
133+
134+
"""
135+
-----------------------------------------------------------------------------------------------------------------------
136+
** BEGIN MAIN LOOP **
137+
-----------------------------------------------------------------------------------------------------------------------
138+
"""
139+
140+
def redrawWindow():
141+
win.blit(bg, (bgX,0)) # these draw the scrolling background
142+
win.blit(bg, (bgX2, 0))
143+
runner.draw(win) # player object calling its draw function to draw himself...
144+
for x in objects:
145+
x.draw(win)
146+
# bigSpike.draw(win) # spike object calling its draw function to draw himself...
147+
# sawBlade.draw(win) # saw object calling its draw function to draw himself...
148+
pygame.display.update()
149+
150+
151+
# create a spike instance or object...
152+
# bigSpike = spike(300,0,48,320)
153+
154+
# create a saw instance or object...
155+
# sawBlade = saw(300,300,64,64)
156+
157+
# create a player instance or object...
158+
runner = player(200, 313, 64, 64) # x, y , and the sprite's dimensions: 64x64
159+
pygame.time.set_timer(USEREVENT+1, 500) # every half second we're going to increase the speed by calling this event...
160+
pygame.time.set_timer(USEREVENT+2, random.randrange(3000, 5000)) # this event is for the obstacle objects timing
161+
speed = 30 # as to how frequently they appear...
162+
run = True
163+
164+
objects = []
165+
166+
while run:
167+
redrawWindow()
168+
# code below gets rid of the obstacle objects once they're off the screen
169+
for objectt in objects:
170+
objectt.x -= 1.4 # decrementing the obstacle object's x
171+
if objectt.x < objectt.width * -1: # if obstacle's x is less than the obstacle's width,
172+
objects.pop(objects.index(objectt)) # this means the obstacle is off the screen so pop it off the list...
173+
# the obstacles will then be instantiated once again in the lower code segments within this while loop...
174+
bgX -= 1.4
175+
bgX2 -= 1.4
176+
# if bgX exceeds the negative x (negative width) value that means its off the screen (no longer visible
177+
if bgX < bg.get_width() * -1:
178+
bgX = bg.get_width() # so, then make bgX = the positive width() again...
179+
if bgX2 < bg.get_width() * -1: # bgX2 is directly behind bgX
180+
bgX2 = bg.get_width()
181+
182+
for event in pygame.event.get():
183+
if event.type == pygame.QUIT:
184+
run = False
185+
pygame.quit()
186+
quit()
187+
if event.type == USEREVENT+1: # what the timer actually does is, it triggers
188+
speed += 1 # the USEREVENT+1 to be true every 500 milli-seconds...(half a second)
189+
# so to check when this event is happening we have to do that within our event loop here...
190+
if event.type == USEREVENT+2:
191+
r = random.randrange(0,2) # the 2 is non-inclusive (randomly instantiate a saw or a spike)
192+
if r == 0: # code below draws the saw and the spike objects by calling their constructors...
193+
objects.append(saw(810,310,64,64)) # then it appends new obstacle into our list...
194+
else: # in this case r == 1
195+
objects.append(spike(810,0,48,320)) # append new obstacle into our list...
196+
197+
# access the computer keyboard keys...
198+
keys = pygame.key.get_pressed()
199+
200+
if keys[pygame.K_SPACE] or keys[pygame.K_UP]:
201+
if not(runner.jumping): # boolean variable: jumping
202+
runner.jumping = True
203+
if keys[pygame.K_DOWN]:
204+
if not (runner.sliding):
205+
runner.sliding = True
206+
207+
clock.tick(speed) # fps frames per second...
208+
209+
"""
210+
-------------------------------------------------------------------------------
211+
** COMPLETED VIDEO #1 **
212+
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
213+
https://www.youtube.com/watch?v=PjgLeP0G5Yw&t=139s
214+
215+
** COMPLETED VIDEO #2 **
216+
Pygame Side-Scroller Tutorial #2 - Random Object Generation
217+
https://www.youtube.com/watch?v=fHlJNjRRXWY
218+
TIME STAMP: STOPPED VIDEO @ 16:52 / 19:20 - TIME: 10:52am 3/27/21 Saturday
219+
220+
** READY FOR VIDEO #3 **
221+
Pygame Side-Scroller Tutorial #3 - Collision
222+
https://www.youtube.com/watch?v=qTw0lYqTQSU
223+
TIME STAMP: STOPPED VIDEO @ 00:00 / 00:00 - TIME: 00:00 / /
224+
-------------------------------------------------------------------------------
225+
"""

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /