Advertisement
Guest User

new_game_part1

a guest
Feb 21st, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. import pygame
  2. import sys
  3.  
  4.  
  5. class MySprite:
  6. def __init__(self, image, location):
  7. self.the_image = pygame.image.load(image)
  8. self.the_rect = self.the_image.get_rect()
  9. self.the_rect.left = location[0]
  10. self.the_rect.top = location[1]
  11.  
  12. def move_left(self):
  13. check = self.the_rect.left - 5
  14. if check > 0:
  15. self.the_rect.left = check
  16.  
  17. def move_right(self, edge):
  18. check = self.the_rect.right + 5
  19. if check < edge:
  20. self.the_rect.right = check
  21.  
  22. def move_up(self):
  23. check = self.the_rect.top - 5
  24. if check > 0:
  25. self.the_rect.top = check
  26.  
  27. def move_down(self, edge):
  28. check = self.the_rect.bottom + 5
  29. if check < edge:
  30. self.the_rect.bottom = check
  31.  
  32. def blit(self, the_surface):
  33. the_surface.blit(self.the_image, self.the_rect)
  34.  
  35.  
  36. class MainWindow:
  37. def __init__(self):
  38. pygame.init()
  39. pygame.key.set_repeat(10, 10)
  40. self.width = 700
  41. self.height = 700
  42. size = (self.width, self.height)
  43. self.DISPLAYSURF = pygame.display.set_mode(size)
  44. pygame.display.set_caption("FireTech")
  45.  
  46. self.ball = MySprite("player.png", (300, 300))
  47.  
  48. def main_game_loop(self):
  49. while True: # main game loop
  50. for event in pygame.event.get():
  51. if event.type == pygame.QUIT:
  52. pygame.quit()
  53. sys.exit()
  54. if event.type == pygame.KEYDOWN:
  55. if event.key == pygame.K_LEFT:
  56. self.ball.move_left()
  57. # if it's the right arrow
  58. elif event.key == pygame.K_RIGHT:
  59. self.ball.move_right(self.width)
  60. # if it's the up arrow
  61. elif event.key == pygame.K_UP:
  62. self.ball.move_up()
  63. # if it's the down arrow
  64. elif event.key == pygame.K_DOWN:
  65. self.ball.move_down(self.height)
  66. self.DISPLAYSURF.fill([0, 0, 0])
  67. self.ball.blit(self.DISPLAYSURF)
  68. pygame.display.update()
  69. pygame.display.flip()
  70.  
  71.  
  72. my_game = MainWindow()
  73. my_game.main_game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement