Advertisement
Guest User

game_part2

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