Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import pygame
  2. pygame.init()
  3.  
  4. #Window display for pygame with caption
  5. win = pygame.display.set_mode((800,600))
  6. pygame.display.set_caption("Derpes Herp")
  7.  
  8. #Names for things
  9. walkRight = [pygame.image.load("walk1R.png"), pygame.image.load("walk2R.png"), pygame.image.load("walk3R.png")]
  10. walkLeft = [pygame.image.load("walk1L.png"), pygame.image.load("walk2L.png"), pygame.image.load("walkL3.png")]
  11. bg = pygame.image.load("bg.png")
  12. char = pygame.image.load("orgbob.png")
  13. #Name for fps controller
  14. clock = pygame.time.Clock()
  15.  
  16. ## Had index errors without this counter
  17.  
  18.  
  19. class player(object):
  20. def __init__(self,x,y,width,height):
  21. self.x = x
  22. self.y = y
  23. self.width = width
  24. self.height = height
  25. self.vel = 5
  26. self.isJump = False
  27. self.left = False
  28. self.right = False
  29. self.walkCount = 0
  30. self.jumpCount = 10
  31. self.standing = True
  32. self.attackCount = 0
  33. self.attack = False
  34.  
  35. ##draw is just the name given for this function so I know its "drawing"
  36. ##Also if walkCount is 9 or reset I think might be backwards
  37. def draw(self, win):
  38. if self.walkCount + 1 >= 9 :
  39. self.walkCount = 0
  40.  
  41. ## Controls walk frames using walkCounter as an index
  42. if not(self.standing):
  43. if self.left:
  44. win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
  45. self.walkCount += 1
  46. elif self.right:
  47. win.blit(walkRight[self.walkCount//3], (self.x,self.y))
  48. self.walkCount +=1
  49. else:
  50. if self.right:
  51. win.blit(walkRight[0], (self.x, self.y))
  52. else:
  53. win.blit(walkRight[0], (self.x, self.y))
  54.  
  55. ##a simple punch without the whole animation atm(self, x ,y ,width, height):
  56. class attacks(object):
  57. def __init__(self,x,y,width,height):
  58. attacks.attack = [pygame.image.load("atk1.png"), pygame.image.load("atk2.png"), pygame.image.load("atk3.png")]
  59.  
  60. def draw(self, win):
  61. attacks.attack()
  62. if self.attackCount +1 >= 9:
  63. self. attackCount = 0
  64. if not (attacks.attack):
  65. if attacks.attack:
  66. win.blit(attack[0],[self.attackCount//3], self.x, self.y)
  67. self.attackCount += 1
  68.  
  69. ## attempt of punch on screen
  70. class enemy(object):
  71. walkRight = [pygame.image.load("enwalkR.png")]
  72. walkLeft = [pygame.image.load("enwalkL.png")]
  73.  
  74. def __init__(self, x, y, width, height, end):
  75. self.x = x
  76. self.y = y
  77. self.width = width
  78. self.height = height
  79. self.path = [x, end]
  80. self.walkCount = 0
  81. self.vel = 3
  82.  
  83. def draw(self, win):
  84. self.move()
  85. if self.walkCount + 1 >= 3:
  86. self.walkCount = 0
  87.  
  88. if self.vel > 0:
  89. win.blit(self.walkRight[self.walkCount//3], (self.x,self.y))
  90. self.walkCount += 1
  91. else:
  92. win.blit(self.walkLeft[self.walkCount//3], (self.x,self.y))
  93. self.walkCount += 1
  94.  
  95. def move(self):
  96. if self.vel > 0:
  97. if self.x < self.path[1] + self.vel:
  98. self.x += self.vel
  99. else:
  100. self.vel = self.vel * -1
  101. self.x += self.vel
  102. self.walkCount = 0
  103. else:
  104. if self.x > self.path[0] - self.vel:
  105. self.x += self.vel
  106. else:
  107. self.vel = self.vel * -1
  108. self.x += self.vel
  109. self.walkCount = 0
  110.  
  111. def redrawGameWindow():
  112. global walkCount
  113. global attack
  114.  
  115. win.blit(bg, (0, 0))
  116.  
  117. bob.draw(win)
  118. jerk.draw(win)
  119.  
  120.  
  121. pygame.display.update()
  122.  
  123.  
  124. #While true
  125. #This calls the classes created
  126. bob = player(100, 200, 128, 128)
  127. jerk = enemy(500, 200, 164, 100, 600)
  128.  
  129. run = True
  130. while run:
  131. clock.tick(30)
  132.  
  133. for event in pygame.event.get():
  134. if event.type == pygame.QUIT:
  135. run = False
  136.  
  137.  
  138. keys = pygame.key.get_pressed()
  139. #attempting to make this into a bool that changes the value
  140. if keys[pygame.K_SPACE]:
  141.  
  142. bob.attack = True
  143. bob.left = False
  144. bob.right = False
  145. if keys[pygame.K_LEFT] and bob.x > bob.vel:
  146. bob.x -= bob.vel
  147. bob.left = True
  148. bob.right = False
  149. bob.standing = False
  150. elif keys[pygame.K_RIGHT] and bob.x < 800 - bob.width - bob.vel:
  151. bob.x += bob.vel
  152. bob.right = True
  153. bob.left = False
  154. bob.standing = False
  155. else:
  156. bob.standing = True
  157. bob.walkCount = 0
  158.  
  159. if not(bob.isJump):
  160. if keys[pygame.K_UP]:
  161. bob.isJump = True
  162. bob.right = False
  163. bob.left = False
  164. bob.walkCount = 0
  165. else:
  166. if bob.jumpCount >= -10:
  167. neg = 1
  168. if bob.jumpCount < 0:
  169. neg = -1
  170. bob.y -= (bob.jumpCount ** 2) * 0.5 * neg
  171. bob.jumpCount -= 1
  172. else:
  173. bob.isJump = False
  174. bob.jumpCount = 10
  175.  
  176. redrawGameWindow()
  177.  
  178. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement