Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys
- clock = pygame.time.Clock()
- from pygame.locals import *
- pygame.init()
- pygame.display.set_caption('Physics Explanation')
- SCREEN = [550,550]
- screen = pygame.display.set_mode(SCREEN,0,32)
- player = pygame.Rect(100,100,40,80)
- tiles = [pygame.Rect(200,350,50,50),pygame.Rect(260,320,50,50)]
- def collision_test(rect,tiles):
- collisions = []
- for tile in tiles:
- if rect.colliderect(tile):
- collisions.append(tile)
- return collisions
- def move(rect,movement,tiles):
- rect.x += movement[0]
- collisions = collision_test(rect,tiles)
- for tile in collisions:
- if movement[0] > 0:
- rect.right = tile.left
- if movement[0] < 0:
- rect.left = tile.right
- rect.y += movement[1]
- collisions = collision_test(rect,tiles)
- for tile in collisions:
- if movement[1] > 0:
- rect.bottom = tile.top
- if movement[1] < 0:
- rect.top = tile.bottom
- return rect
- right = False
- left = False
- up = False
- down = False
- RED = (255,0,0)
- bg = pygame.image.load('bg.jpg')
- while True:
- screen.blit(bg, [0, 0])
- movement = [0,0]
- if right == True:
- movement[0] += 5
- print(player.x)
- if left == True:
- movement[0] -= 5
- print(player.x)
- if up == True:
- movement[1] -= 5
- print(player.y)
- if down == True:
- movement[1] += 5
- print(player.y)
- player = move(player,movement,tiles)
- pygame.draw.rect(screen,(255,255,255),player)
- for tile in tiles:
- pygame.draw.rect(screen,(255,0,0),tile)
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- if event.type == KEYDOWN:
- if event.key == K_RIGHT:
- right = True
- if event.key == K_LEFT:
- left = True
- if event.key == K_DOWN:
- down = True
- if event.key == K_UP:
- up = True
- if event.type == KEYUP:
- if event.key == K_RIGHT:
- right = False
- if event.key == K_LEFT:
- left = False
- if event.key == K_DOWN:
- down = False
- if event.key == K_UP:
- up = False
- pygame.display.update()
- clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement