Guest User

Game Code

a guest
Feb 21st, 2019
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. import pygame
  2.  
  3. pygame.init()
  4.  
  5. clock = pygame.time.Clock()
  6. fps = 160
  7.  
  8. screen = pygame.display.set_mode((500,500))
  9. pygame.display.set_caption("Game 2")
  10. screen.fill((255,255,255))
  11.  
  12. class Player(pygame.sprite.Sprite):
  13. def __init__(self,x,y,width,height):
  14. pygame.sprite.Sprite.__init__(self)
  15. self.x = x
  16. self.y = y
  17. self.width = width
  18. self.height = height
  19. self.right = False
  20. self.left = False
  21. self.up = False
  22. self.down = False
  23. self.surf = pygame.Surface((50,50))
  24. self.rect = self.surf.get_rect()
  25.  
  26. def draw(self):
  27. pygame.draw.rect(screen, (0,0,0), (self.x, self.y, self.width, self.height))
  28.  
  29. def collision_test(self, block1):
  30. if pygame.sprite.collide_rect(self, block1):
  31. print("a collision is detected")
  32.  
  33. def move_player():
  34. if player.right == True:
  35. player.x += 2
  36. elif player.left == True:
  37. player.x -= 2
  38. elif player.up == True:
  39. player.y -= 2
  40. elif player.down == True:
  41. player.y += 2
  42.  
  43. class Block1(pygame.sprite.Sprite):
  44. def __init__(self,x,y,width,height):
  45. pygame.sprite.Sprite.__init__(self)
  46. self.x = x
  47. self.y = y
  48. self.width = width
  49. self.height = height
  50. self.surf = pygame.Surface((self.width,self.height))
  51. self.rect = self.surf.get_rect()
  52.  
  53. def draw(self):
  54. pygame.draw.rect(screen, (150,150,150), (self.x, self.y, self.width, self.height))
  55.  
  56. player = Player(50,50,50,50)
  57. block1 = Block1(200, 200, 100, 100)
  58.  
  59. def redrawGameWindow():
  60. screen.fill((255, 255, 255))
  61. player.draw()
  62. block1.draw()
  63. pygame.display.update()
  64.  
  65. run = True
  66. while run:
  67.  
  68. clock.tick(fps)
  69.  
  70. for event in pygame.event.get():
  71. if event.type == pygame.QUIT:
  72. pygame.quit()
  73.  
  74. keys = pygame.key.get_pressed()
  75.  
  76. if keys[pygame.K_RIGHT]:
  77. player.right = True
  78. player.left = False
  79. player.up = False
  80. player.down = False
  81.  
  82. elif keys[pygame.K_LEFT]:
  83. player.left = True
  84. player.right = False
  85. player.up = False
  86. player.down = False
  87.  
  88. elif keys[pygame.K_UP]:
  89. player.up = True
  90. player.right = False
  91. player.left = False
  92. player.down = False
  93.  
  94. elif keys[pygame.K_DOWN]:
  95. player.down = True
  96. player.right = False
  97. player.left = False
  98. player.up = False
  99.  
  100. else:
  101. player.right = False
  102. player.left = False
  103. player.up = False
  104. player.down = False
  105.  
  106. move_player()
  107.  
  108. player.collision_test()
  109.  
  110. redrawGameWindow()
Advertisement
Add Comment
Please, Sign In to add comment