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 97a0016

Browse files
orcEnemyVer1.006
orcEnemyVer1.006 - This include the enemy object... I call him orc. Tutorial # 6 is applied .
1 parent e6078b8 commit 97a0016

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

‎orcEnemyVer1.006.py‎

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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+
# 9(sprites)images per variable (character has 9 frames for each direction)
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 #6
16+
#----Class for character start-----------------------------------------------------------------------------------------------------------
17+
class player(object): # class attributes
18+
def __init__(self, x, y, width, height): # this defines our character...this is the class constructor
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): # Draw() functon definition belongs to the player class
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+
# class constructor
57+
def __init__(self, x, y, radius, color, facing): # what facing does is tell us whether the bullet is moving left or right.
58+
self.x = x
59+
self.y = y
60+
self.radius = radius
61+
self.color = color # value would be: rgb(0,0,0)
62+
self.facing = facing
63+
self.velocity = 8 * facing # facing parameter will == 1 or -1 which will determine whether bullet will be facing left or right...
64+
65+
def draw(self, win): # draw a circle
66+
pygame.draw.circle(win, self.color, (self.x,self.y), self.radius) # putting a number 1 value after radius, just makes the circle outline,
67+
# it does'nt fill in the cirle as well.
68+
69+
#----Class for projectile END--------------------------------------------------------------------------------------------------------------
70+
#
71+
# ******************************
72+
# pygame Tutorial # 6 - Enemies
73+
#----Class for enemy START-----------------------------------------------------------------------------------------------------------------
74+
class enemy(object):
75+
# lists - data structures class attributes
76+
# 11(sprites)images per variable (enemy character has 11 frames for each direction) (11*3) = 33 FPS
77+
walkRight = [pygame.image.load('C:/Users/19542/Desktop/game101/Game/R1E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R2E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R3E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R4E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R5E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R6E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R7E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R8E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R9E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R10E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/R11E.png')]
78+
walkLeft = [pygame.image.load('C:/Users/19542/Desktop/game101/Game/L1E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L2E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L3E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L4E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L5E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L6E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L7E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L8E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L9E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L10E.png'), pygame.image.load('C:/Users/19542/Desktop/game101/Game/L11E.png')]
79+
# class constructor
80+
def __init__(self, x, y, width, height, end):
81+
self.x = x
82+
self.y = y
83+
self.width = width
84+
self.height = height
85+
self.end = end # note loc: 86 below: self.path[0] self.path[1]
86+
self.path = [self.x, self.end] # this represents where we're starting and where we're ending...this is a list data structure
87+
self.walkCount = 0
88+
self.velocity = 3
89+
90+
def draw(self,win):
91+
self.move()
92+
if self.walkCount + 1 >= 33:
93+
self.walkCount = 0
94+
95+
if self.velocity > 0: # this means we're moving right
96+
win.blit(self.walkRight[self.walkCount//3], (self.x, self.y)) # integer division
97+
self.walkCount += 1
98+
else:
99+
win.blit(self.walkLeft[self.walkCount//3], (self.x, self.y)) # integer division
100+
self.walkCount += 1
101+
102+
def move(self):
103+
if self.velocity > 0: # if character's direction is to the right on the x axis (positive numbers)
104+
if self.x + self.velocity < self.path[1]: # if its less than the coorditnate that we can't go past, then allow character to move
105+
self.x += self.velocity # to the right on the x axis by velocity value which is set to increments of 3.
106+
else:
107+
self.velocity = self.velocity * -1 # * by neg 1 flips character's direction making it go to the left.
108+
self.walkCount = 0
109+
else: # if our velocity is negative, (so then its not greater than 0) then check that we're not moving past that path to the left...
110+
if self.x - self.velocity > self.path[0]:
111+
self.x += self.velocity # then allow character to move to the left by adding the velocity amount adding a negative number
112+
else:
113+
self.velocity = self.velocity * -1 # * by neg 1 flips character's direction making it go to the left.
114+
self.walkCount = 0
115+
# which is really subtracting...
116+
117+
118+
119+
120+
#----Class for enemy END-------------------------------------------------------------------------------------------------------------------
121+
122+
# class player() object or instance
123+
man = player(300, 410, 64, 64) # def __init__(self, x, y, width, height)
124+
# class enemy() object or instance
125+
orc = enemy(100, 410, 64, 64, 450) # def __init__(self, x, y, width, height, end):
126+
127+
# all objects on the screen are drawn from this function.
128+
def redrawGameWindow():
129+
win.blit(bg, (0, 0)) # This loads: bg = pygame.image.load('C:/Users/19542/Desktop/game101/Game/bg.jpg')
130+
man.draw(win) # draw the player() class' character
131+
orc.draw(win) # draw the enemy() class' character
132+
for bullet in bullets:
133+
bullet.draw(win) # draw the projectile class' bullets, which are objects of type: projectile
134+
135+
pygame.display.update() # this refreshes the display thus showing the current code's result which is making a rectangular figure
136+
137+
#mainloop
138+
bullets = []
139+
run = True
140+
while run:
141+
# optimizing the code to use the object's class attributes...ie: man.velocity, (self.velocity), etc...
142+
clock.tick(27) # 27 frames per second (27 FPS)
143+
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
144+
if event.type == pygame.QUIT: # if user clicks on red button
145+
run = False # then run == False
146+
147+
for bullet in bullets:
148+
if bullet.x < 500 and bullet.x > 0: # checking that bullet is within the dimensions of our screen, then allow bullet to be shot
149+
bullet.x += bullet.velocity
150+
else:
151+
bullets.pop(bullets.index(bullet)) # this code removes the bullet from the bullets list at that specific bullet index position
152+
# plus you're going to make more bullet objects anyway and fill the bullets list with them.
153+
keys = pygame.key.get_pressed() # this checks for keys that are continually held down after the initial pressing of them.
154+
155+
if keys[pygame.K_SPACE]:
156+
if man.left: # if man.left == True then...
157+
facing = -1 # projectile shoots to the left in the direction the man is facing although facing pertains to the projectile.
158+
else:
159+
facing = 1 # projectile or bullet faces to the right...(the facing variable pertains to the projectile not the man figure).
160+
if len(bullets) < 5: # 'synching' class projectile object's parameters: x,y,radius,color-rgb,facing to the player character
161+
bullets.append(projectile(round(man.x + man.width//2), round(man.y + man.height//2), 6, (0,0,0), facing))
162+
# using integer division // this makes the bullet come from the middle of the man
163+
# notice how the projectile class' constructor is being called to populate the list with projectile objects
164+
165+
if keys[pygame.K_LEFT] and man.x > man.velocity:
166+
man.x -= man.velocity # to move left you subtract from the x coordinate
167+
man.left = True
168+
man.right = False
169+
man.standing = False
170+
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 )
171+
man.x += man.velocity # to move right you add to the x coordinate
172+
man.right = True
173+
man.left = False
174+
man.standing = False
175+
else:
176+
man.standing = True
177+
man.walkCount = 0
178+
if not (man.isJump): # if isJump not False then its True (negative logic because isJump is assigned False up top)
179+
if keys[pygame.K_UP]:
180+
man.isJump = True
181+
man.right = False
182+
man.left = False
183+
man.walkCount = 0 # walkCount is = 0 because the character is not walking afterall, he is jumping at this point.
184+
else:
185+
if man.jumpCount >= -10:
186+
neg = 1 # when rec character is going up neg variable == positive 1 thus multiplying by 1, nothing occurs...
187+
if man.jumpCount < 0:
188+
neg = -1 # eventually jumpCount will be less than 1 ie; a negative number so neg will == negative 1 effecting the calculation
189+
# and making the character come back down.
190+
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.
191+
# y -= (jumpCount ** 2) * 0.5 * neg this calculation also works exactly the same as the above one...
192+
man.jumpCount -= 1 # this will cause the character to speed up on the jump by 90 pixels, then 80 and 70 and so on...
193+
#plus if you leave it out it won't come back down...It makes it come back down, slowly and smoothly.
194+
else:
195+
man.isJump = False
196+
man.jumpCount = 10
197+
redrawGameWindow()
198+
199+
pygame.quit() # quit game...

0 commit comments

Comments
(0)

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