Guest User

Untitled

a guest
Oct 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. import random
  2. import pygame
  3. import sys
  4. import time
  5. #Setting up pygame
  6. pygame.init()
  7. shooting = False
  8.  
  9. goingright = 1
  10. keys = [False,False,False,False]
  11. clock = pygame.time.Clock()
  12. screen = pygame.display.set_mode([500,500])
  13. font = pygame.font.Font(None,50)
  14. #Creating class for player
  15. class Player:
  16. def __init__(self,x,y,width,height):
  17. self.x = x
  18. self.y = y
  19. self.width = width
  20. self.height = height
  21. def draw(self):
  22. pygame.draw.rect(screen,[0,255,0],[int(self.x),int(self.y),int(self.width),int(self.height)],0)
  23. def move(self):
  24. if keys[1] == True:
  25. self.x -= 1
  26. elif keys[3] == True:
  27. self.x += 1
  28.  
  29. if self.x < 0:
  30. print(self.x)
  31. self.x = 0
  32. if self.x > 500 - self.width:
  33. print(self.x)
  34. self.x = 500 - self.width
  35. def shoot(self):
  36. return
  37. class Bullet():
  38. def __init__(self,x,y):
  39. self.x = x
  40. self.y = y
  41. def update(self,y_amount = 5):
  42. self.y -= y_amount
  43. return
  44. def draw(self):
  45. self.rect = pygame.draw.rect(screen,[0,255,0],[int(self.x),int(self.y),10,50],0)
  46.  
  47.  
  48. class Alien():
  49. def __init__(self,x,y,goingright,dead):
  50. self.x = x
  51. self.y = y
  52.  
  53. self.goingright = goingright
  54. self.dead = dead
  55. def draw(self):
  56. self.rect = pygame.draw.rect(screen,[0,255,0],[int(self.x),int(self.y),25,25],0)
  57.  
  58. def move(self):
  59.  
  60. if self.goingright == True:
  61. self.x += 0.1
  62. if self.x > 500 - 25:
  63. self.y+=25
  64. self.x = 475
  65. self.goingright = False
  66. elif self.goingright == False:
  67. self.x -= 0.1
  68. if self.x < 0:
  69. self.y+=25
  70. self.x = 0
  71. self.goingright = True
  72.  
  73.  
  74.  
  75.  
  76. bullets = []
  77. aliens = []
  78.  
  79.  
  80. #Creating a player
  81.  
  82. player = Player(200,450,40,20)
  83. for i in range(100,800,75):
  84. aliens.append(Alien(i,100,True,False))
  85.  
  86.  
  87. #Main Loop
  88. while True:
  89.  
  90. #Background
  91. screen.fill([0,0,0])
  92. #Letting Player move
  93. player.move()
  94. #Drawing Player
  95. player.draw()
  96. #Updating screen
  97.  
  98. #Checking for events
  99. for event in pygame.event.get():
  100.  
  101. #Checking for quit
  102. if event.type == pygame.QUIT:
  103. sys.exit()
  104. if event.type == pygame.KEYDOWN:
  105.  
  106. #Checking for keys
  107. if event.key == pygame.K_w:
  108. keys[0] = True
  109. elif event.key == pygame.K_a:
  110. keys[1]=True
  111. elif event.key == pygame.K_s:
  112. keys[2]=True
  113. elif event.key == pygame.K_d:
  114. keys[3]=True
  115. elif event.key == pygame.K_SPACE:
  116. bullets.append(Bullet(player.x, player.y))
  117.  
  118.  
  119.  
  120. if event.type == pygame.KEYUP:
  121. if event.key == pygame.K_w:
  122. keys[0]=False
  123. elif event.key == pygame.K_a:
  124. keys[1]=False
  125. elif event.key == pygame.K_s:
  126. keys[2]=False
  127. elif event.key == pygame.K_d:
  128. keys[3]=False
  129. elif event.key == pygame.K_SPACE:
  130. shooting = True
  131. shooting = False
  132. c = 0
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. for bullet in bullets:
  148. bullet.draw()
  149. bullet.update()
  150. for alien in aliens:
  151. alien.draw()
  152. if alien.dead == False:
  153. alien.move()
  154. for alien in aliens:
  155.  
  156. for bullet in bullets:
  157.  
  158. if alien.x < bullet.x + 10 and alien.x + 25 > bullet.x and alien.y < bullet.y + 50 and 25 + alien.y > bullet.y:
  159. pygame.display.flip()
Add Comment
Please, Sign In to add comment