Advertisement
Guest User

B&B

a guest
Mar 27th, 2015
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. #1 - import library
  2. import pygame
  3. from pygame.locals import *
  4. import math
  5. import random
  6.  
  7. #2 - initialize the game
  8. pygame.init()
  9. width, height = 640, 480
  10. screen=pygame.display.set_mode((width, height))
  11. keys = [False, False, False, False]
  12. playerpos = [100,100]
  13. acc = [0,0]
  14. arrows = []
  15. badtimer = 100
  16. badtimer1 = 0
  17. badguys = [[640,100]]
  18. healthvalue = 194
  19.  
  20. #3 - load images
  21. player = pygame.image.load("resources/images/dude.png")
  22. grass = pygame.image.load("resources/images/grass.png")
  23. castle = pygame.image.load("resources/images/castle.png")
  24. arrow = pygame.image.load("resources/images/bullet.png")
  25. badguyimg1 = pygame.image.load("resources/images/badguy.png")
  26. badguyimg = badguyimg1
  27. healthbar = pygame.image.load("resources/images/healthbar.png")
  28. health = pygame.image.load("resources/images/health.png")
  29.  
  30. #4 - keep looping through
  31. while 1:
  32. badtimer -=1
  33. #5- clear the screen before drawing it again
  34. screen.fill(0)
  35. #6 - draw the screen elements
  36. for x in range(width/grass.get_width()+1):
  37. for y in range(height/grass.get_height()+1):
  38. screen.blit(grass,(x*100,y*100))
  39. screen.blit(castle,(0,30))
  40. screen.blit(castle,(0,135))
  41. screen.blit(castle,(0,240))
  42. screen.blit(castle,(0,345))
  43. #6.1 - set player position and rotation
  44. position = pygame.mouse.get_pos()
  45. angle = math.atan2(position[1]-(playerpos[1]+32), position[0] - (playerpos[0]+26))
  46. playerrot = pygame.transform.rotate(player, 360-angle*57.29)
  47. playerpos1 = (playerpos[0] - playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2)
  48. screen.blit(playerrot, playerpos1)
  49. #6.2 - draw arrows
  50. for bullet in arrows:
  51. index = 0
  52. velx = math.cos(bullet[0])*10
  53. vely = math.sin(bullet[0])*10
  54. bullet[1]+=velx
  55. bullet[2]+=vely
  56. if bullet[1] <-64 or bullet[1]>640 or bullet[2]<-64 or bullet[2]>480:
  57. arrows.pop(index)
  58. index+=1
  59. for projectile in arrows:
  60. arrow1 = pygame.transform.rotate(arrow, 360 - projectile[0]*57.29)
  61. screen.blit(arrow1, (projectile[1], projectile[2]))
  62. #6.3 - draw badgers
  63. if badtimer == 0:
  64. badguys.append([640, random.randint(50, 430)])
  65. badtimer = 100 - (badtimer1*2)
  66. if badtimer1 >= 35:
  67. badtimer1 = 35
  68. else:
  69. badtimer1 += 5
  70. index = 0
  71. for badguy in badguys:
  72. if badguy[0] < -64:
  73. badguys.pop(index)
  74. badguy[0] -= 4
  75. #6.3.1 - attack castle
  76. badrect = pygame.Rect(badguyimg.get_rect())
  77. badrect.top = badguy[1]
  78. badrect.left = badguy[0]
  79. if badrect.left < 64:
  80. healthvalue -= random.randint(5,20)
  81. badguys.pop(index)
  82. #6.3.2 - check for collisions
  83. index1=0
  84. for bullet in arrows:
  85. bullrect = pygame.Rect(arrow.get_rect())
  86. bullrect.left = bullet[1]
  87. bullrect.top = bullet[2]
  88. if badrect.colliderect(bullrect):
  89. acc[0]+=1
  90. badguys.pop(index)
  91. arrows.pop(index1)
  92. index1+=1
  93. index += 1
  94. for badguy in badguys:
  95. screen.blit(badguyimg, badguy)
  96. #6.4 - draw clock
  97. font= pygame.font.Font(None, 24)
  98. survivedtext = font.render(str((90000-pygame.time.get_ticks())/60000)+":" + str((90000-pygame.time.get_ticks())/1000%60).zfill(2), True, (0,0,0))
  99. textRect = survivedtext.get_rect()
  100. textRect.topright = [635,5]
  101. screen.blit(survivedtext, textRect)
  102. #6.5 - draw health bar
  103. screen.blit(healthbar, (5,5))
  104. for health1 in range(healthvalue):
  105. screen.blit(health, (health1+8, 8))
  106. #7 - update the screen
  107. pygame.display.flip()
  108. #8 - loop through the events
  109. for event in pygame.event.get():
  110. if event.type == pygame.KEYDOWN:
  111. if event.key == K_w:
  112. keys[0] = True
  113. elif event.key == K_a:
  114. keys[1] = True
  115. elif event.key == K_s:
  116. keys[2] = True
  117. elif event.key == K_d:
  118. keys[3] = True
  119. if event.type == pygame.KEYUP:
  120. if event.key == K_w:
  121. keys[0] = False
  122. elif event.key == K_a:
  123. keys[1] = False
  124. elif event.key == K_s:
  125. keys[2] = False
  126. elif event.key == K_d:
  127. keys[3] = False
  128. if event.type == pygame.MOUSEBUTTONDOWN:
  129. position = pygame.mouse.get_pos()
  130. acc[1]+=1
  131. arrows.append([math.atan2(position[1] - (playerpos1[1] + 32), position[0] - (playerpos1[0]+26)), playerpos1[0]+32, playerpos1[1]+32])
  132. #9 - move player
  133. if keys[0]:
  134. playerpos[1]-=5
  135. elif keys[2]:
  136. playerpos[1]+=5
  137. if keys[1]:
  138. playerpos[0]-=5
  139. elif keys[3]:
  140. playerpos[0]+=5
  141. # check if the event is the X button
  142. if event.type==pygame.QUIT:
  143. #if it is quit the game
  144. pygame.quit()
  145. exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement