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 ceff113

Browse files
Create walkingManGame.py
walkingManGame.py Tutorial # 3 version --- procedural programming The version before the OOP optimization version. Tutorial # 4 is the OOP version.
1 parent 7e1c7b8 commit ceff113

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

‎walkingManGame.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import pygame
2+
pygame.init()
3+
# NOTE: the coordinate in pygame is in the top-left of an object on the screen.
4+
win = pygame.display.set_mode((500, 480)) # dimensions of the window object
5+
pygame.display.set_caption("Guavadream Media LLC")
6+
7+
walkRight = [pygame.image.load('C:/Users/19542/Desktop/game101/Game/R1.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R2.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R3.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R4.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R5.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R6.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R7.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R8.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R9.png')]
8+
walkLeft = [pygame.image.load('C:/Users/19542/Desktop/game101/Game/L1.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L2.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L3.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L4.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L5.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L6.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L7.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L8.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L9.png')]
9+
bg = pygame.image.load('C:/Users/19542/Desktop/game101/Game/bg.jpg')
10+
char = pygame.image.load('C:/Users/19542/Desktop/game101/Game/standing.png')
11+
12+
clock = pygame.time.Clock()
13+
14+
x = 50
15+
y = 400
16+
width = 64
17+
height = 64
18+
velocity = 5
19+
isJump = False
20+
jumpCount = 10
21+
left = False
22+
right = False
23+
walkCount = 0
24+
25+
def redrawGameWindow():
26+
global walkCount
27+
win.blit(bg, (0, 0)) # This loads: bg = pygame.image.load('C:/Users/19542/Desktop/game101/Game/bg.jpg')
28+
29+
if walkCount + 1 >= 27: # NOTICE: the FPS here is 27 FPS
30+
walkCount = 0
31+
if left:
32+
win.blit(walkLeft[walkCount//3], (x,y)) # integer divide by 3 integer division excludes the remainder, all the decimals, rounds off the number
33+
walkCount += 1
34+
elif right:
35+
win.blit(walkRight[walkCount//3], (x,y)) # integer divide by 3 integer division excludes the remainder, all the decimals, rounds off the number
36+
walkCount += 1 # [STOPPED TUT VID @ 12:23 / 15:42....Pygame Tutorial #3 - Character Animation & Sprites]
37+
else:
38+
win.blit(char, (x,y))
39+
40+
pygame.display.update() # this refreshes the display thus showing the current code's result which is making a rectangular figure
41+
42+
#mainloop
43+
run = True
44+
while run:
45+
46+
clock.tick(27) # 27 frames per second (27 FPS)
47+
for event in pygame.event.get(): # this event object can be used to detect keys clicked on too but pygame.key.get_pressed() is better
48+
if event.type == pygame.QUIT: # if user clicks on red button
49+
run = False # then run == False
50+
keys = pygame.key.get_pressed() # this checks for keys that are continually held down after the initial pressing of them.
51+
if keys[pygame.K_LEFT] and x > velocity:
52+
x -= velocity # to move left you subtract from the x coordinate
53+
left = True
54+
right = False
55+
elif keys[pygame.K_RIGHT] and x < 500 - width - velocity: # making sure x is less than the window object's width (win on LOC: 9 line of code )
56+
x += velocity # to move right you add to the x coordinate
57+
right = True
58+
left = False
59+
else:
60+
right = False
61+
left = False
62+
walkCount = 0
63+
64+
if not (isJump):
65+
if keys[pygame.K_SPACE]:
66+
isJump = True
67+
right = False
68+
left = False
69+
walkCount = 0
70+
else:
71+
if jumpCount >= -10:
72+
neg = 1 # when rec character is going up neg variable == positive 1 thus multiplying by 1, nothing occurs...
73+
if jumpCount < 0:
74+
neg = -1 # eventually jumpCount will be less than 1 ie; a negative number so neg will == negative 1 effecting the calculation
75+
# and making the character come back down.
76+
y -= (jumpCount ** 2) / 2 * neg # squared by 2 so this will cause the character to move 100 pixels upward and downward on the y axis.
77+
# y -= (jumpCount ** 2) * 0.5 * neg this calculation also works exactly the same as the above one...
78+
jumpCount -= 1 # this will cause the character to speed up on the jump by 90 pixels, then 80 and 70 and so on...
79+
#plus if you leave it out it won't come back down...It makes it come back down, slowly and smoothly.
80+
else:
81+
isJump = False
82+
jumpCount = 10
83+
redrawGameWindow()
84+
85+
pygame.quit() # and quit game...

0 commit comments

Comments
(0)

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