Advertisement
Guest User

main

a guest
Jul 12th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import pygame
  2. from classes import *
  3. import sys
  4.  
  5. WHITE = (0,0,0)
  6. BLACK = (255,255,255)
  7.  
  8. WIDTH = 600
  9. HEIGHT = 400
  10.  
  11. sprites = pygame.sprite.Group()
  12. objects = pygame.sprite.Group()
  13.  
  14. def Update(dimension,direction):
  15. if dimension == "x" and direction == "neg":
  16. for i in objects:
  17. i.rect.x -= 2
  18. if dimension == "y" and direction == "pos":
  19. for i in objects:
  20. i.rect.y -= 2
  21. if dimension == "x" and direction == "pos":
  22. for i in objects:
  23. i.rect.x += 2
  24. if dimension == "y" and direction == "neg":
  25. for i in objects:
  26. i.rect.y += 2
  27.  
  28. def CheckCollision(objects,Player1):
  29. if pygame.sprite.spritecollideany(Player1,objects,False) == True:
  30. return(True)
  31. else:
  32. return(False)
  33.  
  34. def main():
  35.  
  36. update = False
  37.  
  38. Xpos = 0
  39. Ypos = 0
  40.  
  41. Player1 = Player(WHITE,10,10)
  42. Player1.rect.x = WIDTH/2
  43. Player1.rect.y = HEIGHT/2
  44. sprites.add(Player1)
  45.  
  46. Hitbox1 = Hitbox("wall",50,50)
  47. Hitbox1.rect.x = 100
  48. Hitbox1.rect.y = 100
  49. sprites.add(Hitbox1)
  50. objects.add(Hitbox1)
  51.  
  52. Hitbox2 = Hitbox("wall",100,50)
  53. Hitbox2.rect.x = 200
  54. Hitbox2.rect.y = 100
  55. sprites.add(Hitbox2)
  56. objects.add(Hitbox2)
  57.  
  58. pygame.init()
  59.  
  60. pygame.display.set_caption("Hitboxes")
  61. screen = pygame.display.set_mode((WIDTH,HEIGHT))
  62.  
  63. clock = pygame.time.Clock()
  64. while True:
  65. #key presses
  66. for event in pygame.event.get():
  67. if event.type == pygame.QUIT:
  68. Running = False
  69. pygame.quit()
  70. sys.exit()
  71.  
  72. #move Left
  73. keys = pygame.key.get_pressed()
  74. if keys[pygame.K_a] and CheckCollision(objects,Player1) == False:
  75. Xpos = Player1.moveLeft(2,Xpos)
  76. Update("x","pos")
  77. if CheckCollision(Player1, objects) == True:
  78. Xpos = Player1.moveRight(2,Xpos)
  79. Update("x","neg")
  80. #move Right
  81. if keys[pygame.K_d] and CheckCollision(objects,Player1) == False:
  82. Xpos = Player1.moveRight(2,Xpos)
  83. Update("x","neg")
  84. if CheckCollision(Player1, objects) == True:
  85. Xpos = Player1.moveLeft(2,Xpos)
  86. Update("x","pos")
  87. #move Up
  88. if keys[pygame.K_w] and CheckCollision(objects,Player1) == False:
  89. Ypos = Player1.moveUp(2,Ypos)
  90. Update("y","neg")
  91. if CheckCollision(Player1, objects) == True:
  92. Ypos = Player1.moveDown(2,Ypos)
  93. Update("y","pos")
  94. #move Down
  95. if keys[pygame.K_s] and CheckCollision(objects,Player1) == False:
  96. Ypos = Player1.moveDown(2,Ypos)
  97. Update("y","pos")
  98. if CheckCollision(Player1, objects) == True:
  99. Ypos = Player1.moveUp(2,Ypos)
  100. Update("y","neg")
  101.  
  102. sprites.draw(screen)
  103.  
  104. pygame.display.flip()
  105. clock.tick(60)
  106. screen.fill(BLACK)
  107.  
  108. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement