Advertisement
j311yf1sh

Untitled

May 8th, 2011
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.99 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. from sys import exit
  4. from math import *
  5. from random import randint
  6.  
  7. #Background
  8. bg = './images/bg.png'
  9.  
  10. #Graphics player
  11. still = './images/player/still.gif'
  12. walk = './images/player/walk.gif'
  13. kick = './images/player/kick.gif'
  14. punch = './images/player/punch.gif'
  15. duck = './images/player/duck.gif'
  16. special1 = './images/player/special.png'
  17. jumpy = './images/player/jump.gif'
  18.  
  19. #graphics npc
  20. enemy_still = './images/npc/estill.gif'
  21.  
  22. #create screen
  23. screen = pygame.display.set_mode((400, 254), 0, 32)
  24. pygame.display.set_caption('MK Game')
  25.  
  26. #initate pygame
  27. pygame.init()
  28.  
  29. #game graphic load
  30. background = pygame.image.load(bg).convert()
  31.  
  32. #character graphic load
  33. stillpro = pygame.image.load(still).convert()
  34. walkpro = pygame.image.load(walk).convert()
  35. kickpro = pygame.image.load(kick).convert()
  36. punch1 = pygame.image.load(punch).convert()
  37. duckpro = pygame.image.load(duck).convert()
  38. special = pygame.image.load(special1).convert()
  39. jump = pygame.image.load(jumpy).convert()
  40.  
  41. #character flip
  42. stillflip = pygame.transform.flip(stillpro, 1, 0)
  43. specialflip = pygame.transform.flip(special, 1, 0)
  44. walkflip = pygame.transform.flip(walkpro, 1, 0)
  45. kickflip = pygame.transform.flip(kickpro, 1, 0)
  46. punchflip = pygame.transform.flip(punch1, 1, 0)
  47. duckflip = pygame.transform.flip(duckpro, 1, 0)
  48. jumpflip = pygame.transform.flip(jump, 1, 0)
  49.  
  50. #npc graphic load
  51. estill = pygame.image.load(enemy_still).convert()
  52.  
  53. #npc flip
  54. eflipstill = pygame.transform.flip(estill, 1, 0)
  55.  
  56. #sprite will change depending on wich key is pressed
  57. sprite = stillpro
  58. ducky = stillpro
  59. duckflipy = stillflip
  60.  
  61. esprite = estill
  62.  
  63. spriteflip = stillflip
  64.  
  65. #player location variables
  66. x, y = 0, 80
  67. move_x, move_y = 0, 0
  68. airtime = 0
  69.  
  70. #player variables
  71. phealth = 100
  72. ppower = 0
  73. speed = 5
  74.  
  75. #enemy location variables
  76. ex, ey = 140, 80
  77.  
  78. #enemy variables
  79. ehealth = 100
  80. epower = 0
  81. espeed = 5
  82.  
  83. #time keeping
  84. clock = pygame.time.Clock()
  85.  
  86.  
  87. while True:
  88.  
  89. #Error checks
  90.  
  91. #player hit test
  92. xplus = x + 88
  93. rangey = range(x, xplus)
  94. xneg = x - 40
  95. negrangey = range(xneg, x)
  96.  
  97. #enemy hit test
  98. explus = ex + 88
  99. rangey = range(ex, explus)
  100. exneg = ex - 40
  101. enegrangey = range(exneg, ex)
  102.  
  103. #time handling
  104. time_passed = clock.tick(60)
  105. time_passed_seconds = time_passed / 1000.
  106. ptime = time_passed_seconds #passed time short handed
  107.  
  108. #npc facing
  109. if ex > x:
  110. esprite = eflipstill
  111. elif ex < x:
  112. esprite = estill
  113.  
  114. #npc test brain
  115. move = randint(1,2)
  116. if ex > x and move == 1:
  117. ex = ex - espeed
  118. elif ex < x and move == 1:
  119. ex = ex + espeed
  120.  
  121.  
  122.  
  123. #Keycommands start
  124. for event in pygame.event.get():
  125. if event.type == QUIT:
  126. exit()
  127. # Get keydown commands
  128. if event.type == KEYDOWN:
  129. if event.key == K_LEFT:
  130. move_x = -speed - 1
  131. if sprite != duckpro:
  132. if sprite != kickpro:
  133. if sprite != punch1:
  134. if sprite != special:
  135. if sprite != jump:
  136. sprite = walkpro
  137. spriteflip = walkflip
  138. if event.key == K_RIGHT:
  139. move_x = speed + 1
  140. if sprite != duckpro:
  141. if sprite != kickpro:
  142. if sprite != punch1:
  143. if sprite != special:
  144. if sprite != jump:
  145. sprite = walkpro
  146. spriteflip = walkflip
  147. if event.key == K_a:
  148. if x < ex:
  149. if ex in rangey:
  150. ehealth -=10
  151. print 'enemy health', ehealth,'player power', ppower
  152. ppower += 20
  153. elif x > ex:
  154. if ex in negrangey:
  155. ehealth -= 10
  156. print 'enemy health', ehealth,'player power', ppower
  157. ppower += 20
  158. sprite = kickpro
  159. spriteflip = kickflip
  160. if event.key == K_d:
  161. if x < ex:
  162. if ex in rangey:
  163. ehealth -=10
  164. print 'enemy health', ehealth,'player power', ppower
  165. ppower += 20
  166. elif x > ex:
  167. if ex in negrangey:
  168. ehealth -= 10
  169. print 'enemy health', ehealth,'player power', ppower
  170. ppower += 20
  171. sprite = punch1
  172. spriteflip = punchflip
  173. if event.key == K_DOWN:
  174. ducky = duckpro
  175. duckyflip = duckflip
  176. spriteflip = duckflip
  177. sprite = duckpro
  178. y = 115
  179. if event.key == K_s and ppower >= 50:
  180. if x < ex:
  181. if ex in rangey:
  182. ehealth -=10
  183. print 'enemy health', ehealth,'player power', ppower
  184. elif x > ex:
  185. if ex in negrangey:
  186. ehealth -= 10
  187. print 'enemy health', ehealth,'player power', ppower
  188. sprite = special
  189. spriteflip = specialflip
  190. ppower -= 50
  191. if event.key == K_UP and y > 30 and airtime <= 0:
  192. sprite = jump
  193. spriteflip = jumpflip
  194. while (y > 30):
  195. y = y - 1
  196. airtime = airtime + 1
  197. if y == 30 and airtime >= 5:
  198. y = y + 50
  199. airtime = 0
  200. break
  201.  
  202. #Get key up commands
  203. elif event.type == KEYUP:
  204. if event.key == K_LEFT:
  205. move_x = 0
  206. if sprite != duckpro:
  207. if sprite != kickpro:
  208. if sprite != punch1:
  209. if sprite != special:
  210. if sprite != jump:
  211. sprite = stillpro
  212. spriteflip = stillflip
  213. if event.key == K_RIGHT:
  214. move_x = 0
  215. if sprite != duckpro:
  216. if sprite != kickpro:
  217. if sprite != punch1:
  218. if sprite != special:
  219. if sprite != jump:
  220. sprite = stillpro
  221. spriteflip = stillflip
  222. if event.key == K_a:
  223. sprite = stillpro
  224. spriteflip = stillflip
  225. if event.key == K_d:
  226. sprite = stillpro
  227. spriteflip = stillflip
  228. if event.key == K_DOWN:
  229. sprite = stillpro
  230. spriteflip = stillflip
  231. ducky = stillpro
  232. y = 80
  233. if event.key == K_s:
  234. sprite = stillpro
  235. spriteflip = stillflip
  236. if event.key == K_UP or airtime >= 1:
  237. while(y < 80):
  238. y = y + 1
  239. airtime = 0
  240. sprite = stillpro
  241. spriteflip = stillflip
  242.  
  243. #spare error fixing if's I.E Graphics being in wrong place after multiple key press
  244. print airtime
  245. if x < 0:
  246. x = 0
  247.  
  248. if x > 350:
  249. x = 350
  250.  
  251. if y < 0:
  252. y = 80
  253.  
  254. if y > 80 and sprite != duckpro:
  255. y = 80
  256.  
  257. if y == 80 and sprite == duckpro:
  258. y = 115
  259.  
  260. if x > ex:
  261. if ducky == duckpro:
  262. if sprite != kickpro:
  263. if sprite != walkpro:
  264. if sprite != punch1:
  265. if sprite != special:
  266. if sprite != jump :
  267. sprite = duckpro
  268. y = 115
  269. if duckflipy == duckflip:
  270. if sprite != kickpro:
  271. if sprite != walkpro:
  272. if sprite != punch1:
  273. if sprite != special:
  274. if sprite != jump :
  275. spriteflip = duckflip
  276. y = 115
  277.  
  278.  
  279.  
  280. #print x, y, ptime
  281.  
  282. x+= move_x
  283. y+= move_y
  284.  
  285. screen.fill((0,0,0))
  286. screen.blit(background, (0,0))
  287.  
  288. #print player sprites to screen
  289. if x < ex:
  290. if sprite == stillpro:
  291. screen.blit(stillpro, (x, y))
  292. if sprite == walkpro:
  293. screen.blit(walkpro, (x, y))
  294. if sprite == special:
  295. screen.blit(special, (x, y))
  296. if sprite == kickpro:
  297. screen.blit(kickpro, (x, y))
  298. if sprite == punch1:
  299. screen.blit(punch1, (x, y))
  300. if sprite == duckpro:
  301. screen.blit(duckpro, (x, y))
  302. if sprite == jump:
  303. screen.blit(jump, (x, y))
  304. if x > ex:
  305. if spriteflip == stillflip:
  306. screen.blit(stillflip, (x, y))
  307. if spriteflip == walkflip:
  308. screen.blit(walkflip, (x, y))
  309. if spriteflip == specialflip:
  310. screen.blit(specialflip, (x, y))
  311. if spriteflip == kickflip:
  312. screen.blit(kickflip, (x, y))
  313. if spriteflip == punchflip:
  314. screen.blit(punchflip, (x, y))
  315. if spriteflip == jumpflip:
  316. screen.blit(jumpflip, (x, y))
  317. if spriteflip == duckflip:
  318. screen.blit(duckflip, (x, y))
  319.  
  320. #enemy sprites to screen
  321. screen.blit(esprite,(ex, ey))
  322.  
  323. #check if health is good
  324. if ehealth <= 0:
  325. print 'Game Over! player wins!'
  326. exit()
  327. elif phealth <= 0:
  328. print 'Game Over! Enemy wins!'
  329. exit()
  330.  
  331. pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement