Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import pygame
  2.  
  3. class Sprite:
  4. def __init__(self,x,y,filename):
  5. self.x=x
  6. self.y=y
  7. self.bitmap=pygame.image.load(filename)
  8. self.bitmap.set_colorkey((0,0,0))
  9. def picture(self,screen):
  10. screen.blit(self.bitmap,(self.x,self.y))
  11.  
  12. def move_zet(zet):
  13. if zet.right==True:
  14. zet.x+=zet.step
  15. if zet.x>460:
  16. zet.right=False
  17. else:
  18. zet.x-=zet.step
  19. if zet.x<0:
  20. zet.right=True
  21.  
  22. def move_gun_arrow(gun,arrow,event):
  23. if event.type==pygame.KEYDOWN:
  24. if event.key==pygame.K_LEFT:
  25. if gun.x>0:
  26. gun.x-=1
  27. if event.key==pygame.K_RIGHT:
  28. if gun.x<460:
  29. gun.x+=1
  30. if event.key==pygame.K_DOWN:
  31. if gun.y<410:
  32. gun.y+=1
  33. if event.key==pygame.K_UP:
  34. if gun.y>70:
  35. gun.y-=1
  36. if event.key==pygame.K_SPACE:
  37. if arrow.push == False:
  38. arrow.x=gun.x + 10
  39. arrow.y=gun.y - 5
  40. arrow.push = True
  41. if event.type==pygame.MOUSEMOTION:
  42. m=pygame.mouse.get_pos()
  43. pygame.mouse.set_visible(False)
  44. if 0<m[0]<460:
  45. gun.x=m[0]
  46. if 70<m[1]<400:
  47. gun.y=m[1]
  48. if event.type==pygame.MOUSEBUTTONDOWN:
  49. if event.button == 1:
  50. if arrow.push == False:
  51. arrow.x=gun.x + 10
  52. arrow.y=gun.y - 5
  53. arrow.push = True
  54.  
  55. def fly(arrow):
  56. if arrow.y < 0:
  57. arrow.push = False
  58. arrow.x = -40
  59. arrow.y = 350
  60. elif arrow.push == False:
  61. arrow.x= -40
  62. arrow.y= 350
  63. else:
  64. arrow.y -= 3
  65.  
  66. def log(arrow, zet):
  67. if abs(arrow.x - zet.x) <= 20 and abs(arrow.y - zet.y )<= 20:
  68. print(zet.po)
  69. arrow.push = False
  70. zet.po += 10
  71. zet.step *= 1.05
  72.  
  73. elif arrow.y < 0:
  74. zet.po -= 1
  75.  
  76.  
  77.  
  78. def main():
  79. pygame.init()
  80. window=pygame.display.set_mode(( 500, 500)) # окно
  81. pygame.display.set_caption('Пушка')
  82. screen=pygame.Surface((500,450))# игровой экран
  83. status_bar=pygame.Surface((500,50))
  84. #==== создание шрифтов
  85. pygame.font.init()
  86. bar_font=pygame.font.SysFont('Arial',24,True,False)
  87. zet=Sprite(250,20,'hero_2.png')
  88. zet.right=True
  89. zet.po = 0
  90. zet.step = 1
  91. gun=Sprite(250,190,'hero.png')
  92. arrow=Sprite(-40,350,'hero_3.png')
  93. arrow.push = False
  94. pygame.key.set_repeat(1,1)
  95. done=True
  96. while done:
  97. for event in pygame.event.get():
  98. if (event.type==pygame.QUIT):
  99. done = False
  100. move_gun_arrow(gun,arrow,event)
  101. screen.fill((73,101,78))
  102. status_bar.fill((192,207,178))
  103. fly(arrow)
  104. move_zet(zet)
  105. zet.picture(screen)
  106. arrow.picture(screen)
  107. gun.picture(screen)
  108. log(arrow,zet)
  109. status_bar.blit(bar_font.render('СЧЕТ: '+str(zet.po),1,(37,53,40)),(20,10))
  110. status_bar.blit(bar_font.render('СКОРОСТЬ: '+str(zet.step),1,(37,53,40)),(320,10))
  111. window.blit(screen,(0,50))
  112. window.blit(status_bar,(0,0))
  113. pygame.display.flip()
  114. pygame.time.delay(5)
  115.  
  116.  
  117. if __name__=='__main__':
  118. main()
  119. pygame.font.quit()
  120. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement