Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from classes import *
- import sys
- WHITE = (0,0,0)
- BLACK = (255,255,255)
- WIDTH = 600
- HEIGHT = 400
- sprites = pygame.sprite.Group()
- objects = pygame.sprite.Group()
- def Update(dimension,direction):
- if dimension == "x" and direction == "neg":
- for i in objects:
- i.rect.x -= 2
- if dimension == "y" and direction == "pos":
- for i in objects:
- i.rect.y -= 2
- if dimension == "x" and direction == "pos":
- for i in objects:
- i.rect.x += 2
- if dimension == "y" and direction == "neg":
- for i in objects:
- i.rect.y += 2
- def CheckCollision(objects,Player1):
- if pygame.sprite.spritecollideany(Player1,objects,False) == True:
- return(True)
- else:
- return(False)
- def main():
- update = False
- Xpos = 0
- Ypos = 0
- Player1 = Player(WHITE,10,10)
- Player1.rect.x = WIDTH/2
- Player1.rect.y = HEIGHT/2
- sprites.add(Player1)
- Hitbox1 = Hitbox("wall",50,50)
- Hitbox1.rect.x = 100
- Hitbox1.rect.y = 100
- sprites.add(Hitbox1)
- objects.add(Hitbox1)
- Hitbox2 = Hitbox("wall",100,50)
- Hitbox2.rect.x = 200
- Hitbox2.rect.y = 100
- sprites.add(Hitbox2)
- objects.add(Hitbox2)
- pygame.init()
- pygame.display.set_caption("Hitboxes")
- screen = pygame.display.set_mode((WIDTH,HEIGHT))
- clock = pygame.time.Clock()
- while True:
- #key presses
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- Running = False
- pygame.quit()
- sys.exit()
- #move Left
- keys = pygame.key.get_pressed()
- if keys[pygame.K_a] and CheckCollision(objects,Player1) == False:
- Xpos = Player1.moveLeft(2,Xpos)
- Update("x","pos")
- if CheckCollision(Player1, objects) == True:
- Xpos = Player1.moveRight(2,Xpos)
- Update("x","neg")
- #move Right
- if keys[pygame.K_d] and CheckCollision(objects,Player1) == False:
- Xpos = Player1.moveRight(2,Xpos)
- Update("x","neg")
- if CheckCollision(Player1, objects) == True:
- Xpos = Player1.moveLeft(2,Xpos)
- Update("x","pos")
- #move Up
- if keys[pygame.K_w] and CheckCollision(objects,Player1) == False:
- Ypos = Player1.moveUp(2,Ypos)
- Update("y","neg")
- if CheckCollision(Player1, objects) == True:
- Ypos = Player1.moveDown(2,Ypos)
- Update("y","pos")
- #move Down
- if keys[pygame.K_s] and CheckCollision(objects,Player1) == False:
- Ypos = Player1.moveDown(2,Ypos)
- Update("y","pos")
- if CheckCollision(Player1, objects) == True:
- Ypos = Player1.moveUp(2,Ypos)
- Update("y","neg")
- sprites.draw(screen)
- pygame.display.flip()
- clock.tick(60)
- screen.fill(BLACK)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement