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 e6078b8

Browse files
shooting-Game
OOP CODE OPTIMIZATION Applied: tutorials #4, #5
1 parent db3fa85 commit e6078b8

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

‎shooting-Game.py‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
# OOP CODE OPTIMIZATION
15+
# Applied: tutorials #4, #5
16+
#----Class for character start---------------------------------------------------------------------------------------
17+
class player(object): # class attributes
18+
def __init__(self, x, y, width, height): # this defines our character...
19+
self.x = x
20+
self.y = y
21+
self.width = width
22+
self.height = height
23+
self.velocity = 5
24+
self.isJump = False
25+
self.jumpCount = 10
26+
self.left = False
27+
self.right = False
28+
self.walkCount = 0
29+
self.standing = True
30+
31+
def draw(self, win):
32+
if self.walkCount + 1 >= 27: # NOTICE: the FPS here is 27 FPS
33+
self.walkCount = 0
34+
35+
if not (self.standing): # if not standing, then the character is walking...
36+
if self.left:
37+
win.blit(walkLeft[self.walkCount//3], (self.x,self.y)) # integer divide by 3 integer division excludes the remainder, all the decimals, rounds off the number
38+
self.walkCount += 1
39+
elif self.right:
40+
win.blit(walkRight[self.walkCount//3], (self.x,self.y)) # integer divide by 3 integer division excludes the remainder, all the decimals, rounds off the number
41+
self.walkCount += 1
42+
else:
43+
if self.right:
44+
win.blit(walkRight[0], (self.x, self.y)) # shows the first animation of character looking to the right
45+
else:
46+
win.blit(walkLeft[0], (self.x, self.y)) # shows the first animation of character looking to the left
47+
48+
#win.blit(char, (self.x,self.y)) # this plays the animation of the character standing and facing the screen.
49+
50+
#----Class for character end-------------------------------------------------------------------------------------------
51+
#
52+
# ******************************
53+
#
54+
#----Class for projectile START-----------------------------------------------------------------------------------------
55+
class projectile(object): # facing parameter will == 1 or -1
56+
def __init__(self, x, y, radius, color, facing): # what facing does is tell us whether the bullet is moving left or right.
57+
self.x = x
58+
self.y = y
59+
self.radius = radius
60+
self.color = color
61+
self.facing = facing
62+
self.velocity = 8 * facing # facing parameter will == 1 or -1 which will determine whether bullet will be facing left or right...
63+
64+
def draw(self, win): # draw a circle
65+
pygame.draw.circle(win, self.color, (self.x,self.y), self.radius) # putting a number 1 value after radius, just makes the circle outline,
66+
# it does'nt fill in the cirle as well.
67+
68+
69+
#----Class for projectile END-------------------------------------------------------------------------------------------
70+
71+
# class player() object or instance
72+
man = player(300, 410, 64, 64)
73+
74+
# all objects on the screen are drawn from this function.
75+
def redrawGameWindow():
76+
win.blit(bg, (0, 0)) # This loads: bg = pygame.image.load('C:/Users/19542/Desktop/game101/Game/bg.jpg')
77+
man.draw(win) # draw the player() class' character
78+
for bullet in bullets:
79+
bullet.draw(win) # draw the projectile class' bullets, which are objects of type: projectile
80+
81+
pygame.display.update() # this refreshes the display thus showing the current code's result which is making a rectangular figure
82+
83+
#mainloop
84+
bullets = []
85+
run = True
86+
while run:
87+
# optimizing the code to use the object's class attributes...ie: man.velocity, (self.velocity), etc...
88+
clock.tick(27) # 27 frames per second (27 FPS)
89+
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
90+
if event.type == pygame.QUIT: # if user clicks on red button
91+
run = False # then run == False
92+
93+
for bullet in bullets:
94+
if bullet.x < 500 and bullet.x > 0: # checking that bullet is within the dimensions of our screen, then allow bullet to be shot
95+
bullet.x += bullet.velocity
96+
else:
97+
bullets.pop(bullets.index(bullet)) # this code removes the bullet from the bullets list at that specific bullet index position
98+
# plus you're going to make more bullet objects anyway and fill the bullets list with them.
99+
keys = pygame.key.get_pressed() # this checks for keys that are continually held down after the initial pressing of them.
100+
101+
if keys[pygame.K_SPACE]:
102+
if man.left: # if man.left == True then...
103+
facing = -1
104+
else:
105+
facing = 1
106+
if len(bullets) < 5: # 'synching' class projectile object's parameters: x,y,radius,color,facing to the player character
107+
bullets.append(projectile(round(man.x + man.width//2), round(man.y + man.height//2), 6, (0,0,0), facing))
108+
# using integer division // this makes the bullet come from the middle of the man
109+
# notice how the projectile class' constructor is being called to populate the list with projectile objects
110+
111+
if keys[pygame.K_LEFT] and man.x > man.velocity:
112+
man.x -= man.velocity # to move left you subtract from the x coordinate
113+
man.left = True
114+
man.right = False
115+
man.standing = False
116+
elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.velocity: # making sure x is less than the window object's width (win on LOC: 9 line of code )
117+
man.x += man.velocity # to move right you add to the x coordinate
118+
man.right = True
119+
man.left = False
120+
man.standing = False
121+
else:
122+
man.standing = True
123+
man.walkCount = 0
124+
if not (man.isJump): # if isJump not False then its True (negative logic because isJump is assigned False up top)
125+
if keys[pygame.K_UP]:
126+
man.isJump = True
127+
man.right = False
128+
man.left = False
129+
man.walkCount = 0 # walkCount is = 0 because the character is not walking afterall, he is jumping at this point.
130+
else:
131+
if man.jumpCount >= -10:
132+
neg = 1 # when rec character is going up neg variable == positive 1 thus multiplying by 1, nothing occurs...
133+
if man.jumpCount < 0:
134+
neg = -1 # eventually jumpCount will be less than 1 ie; a negative number so neg will == negative 1 effecting the calculation
135+
# and making the character come back down.
136+
man.y -= (man.jumpCount ** 2) / 2 * neg # squared by 2 so this will cause the character to move 100 pixels upward and downward on the y axis.
137+
# y -= (jumpCount ** 2) * 0.5 * neg this calculation also works exactly the same as the above one...
138+
man.jumpCount -= 1 # this will cause the character to speed up on the jump by 90 pixels, then 80 and 70 and so on...
139+
#plus if you leave it out it won't come back down...It makes it come back down, slowly and smoothly.
140+
else:
141+
man.isJump = False
142+
man.jumpCount = 10
143+
redrawGameWindow()
144+
145+
pygame.quit() # quit game...

0 commit comments

Comments
(0)

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