Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!user/bin/env python
- import sys, pygame
- from pygame.locals import *
- #--- Our Variables
- black = (0,0,0)
- size = (320, 240)
- speed = [0,5]
- #--- Init pygame
- pygame.init
- #--- Display
- pygame.display.set_caption("Rectangle Man: Story of a Square")
- screen = pygame.display.set_mode(size)
- class Player(object):
- def __init__(self):
- self.rect = pygame.Rect(0, 0, 15, 16)
- def move(self, dx, dy):
- if dx != 0:
- self.move_single_axis(dx, 0)
- if dy != 0:
- self.move_single_axis(0, dy)
- def move_single_axis(self, dx, dy):
- self.rect.x += dx
- self.rect.y += dy
- tx = int(player.rect.x / 16)
- ty = int(player.rect.y / 16)
- rects = [[tx, ty],[tx + 1, ty],[tx, ty + 1],[tx + 1, ty + 1]]
- for h in rects:
- if walls[int(h[1])][int(h[0])] == 1:
- wam = pygame.Rect(h[0] * 16, h[1] * 16, 16, 16)
- if self.rect.colliderect(wam):
- if dx > 0:
- self.rect.right = wam.left
- if dx < 0:
- self.rect.left = wam.right
- if dy > 0:
- self.rect.bottom = wam.top
- global jump
- jump = 0
- speed[1] = 0
- if dy < 0:
- self.rect.top = wam.bottom
- speed[1] = 0
- class Wall(object):
- def __init__(self, pos, tile):
- tNum = tile
- #--- Variables
- clock = pygame.time.Clock()
- #ball = pygame.image.load("ball.jpg").convert()
- #buff = pygame.image.load("buff.jpg").convert()
- rects = [pygame.Rect(0,0, 1, size[1]), pygame.Rect(0, size[1] - 20, size[0], size[1]), pygame.Rect(size[0] - 1, 0, size[0], size[1])]
- jumping = False
- jump = 0
- jumpNum = 2
- inc = 1
- jHeight = 10
- level = ["""WWWWWWWWWWWWWWWWWWWW.WS W.W WWWWWW W.W WWWW W W.W W WWWW W.W WWW WWWW W.W W W W W.W W W WWW WW.W WWW WWW W W W.W W W W W W.WWW W WWWWW W W.W W WW W.W W WWWW WWW W.W W W W W.WWWWWWWWWWWWWWWWWWWW."""
- ]
- walls = []
- walls.append([])
- player = Player()
- def getEvents():
- for event in pygame.event.get():
- if event.type == KEYDOWN:
- if event.key == K_UP:
- global jumping, jump
- if jumping == False:
- jumping = True
- jump += 1
- speed[1] = -jHeight
- global inc
- inc = 1
- if event.key == K_DOWN:
- player.rect = pygame.Rect(player.rect.x, player.rect.y + 8,16,8)
- pygame.draw.rect(screen, (0,0,0), ballbuff)
- if event.key == K_LEFT:
- speed[0] = -4
- if event.key == K_RIGHT:
- speed[0] = 4
- if event.type == KEYUP:
- if event.key == K_UP:
- inc = 2
- if event.key == K_DOWN and speed[1] > 0:
- player.rect = pygame.Rect(player.rect.x, player.rect.y - 8,16,16)
- if event.key == K_LEFT and speed[0] < 0:
- speed[0] = 0
- if event.key == K_RIGHT and speed[0] > 0:
- speed[0] = 0
- if event.type == pygame.QUIT:
- running = False
- sys.exit()
- def loadMap(num):
- y = 0
- for row in level[num]:
- if row == ".":
- walls.append([])
- y += 1
- if row == "W":
- walls[y].append(1)
- if row == " ":
- walls[y].append(0)
- if row == "S":
- walls[y].append(2)
- player.rect.x = (len(walls) - 1) * 16
- player.rect.y = y * 16
- loadMap(0)
- running = True
- while running:
- ballbuff = pygame.Rect(player.rect.x, player.rect.y, 16, player.rect.bottom - player.rect.top)
- getEvents()
- player.move(speed[0], speed[1])
- if speed[1] < jHeight:
- if speed[1] < 0:
- speed[1] += inc
- else:
- speed[1] += 1
- if jumping:
- if (speed[1] >= 0) and (jump <= jumpNum - 1):
- jumping = False
- for i in range(0, len(rects)):
- pygame.draw.rect(screen, (0, 255, 0), rects[i])
- for i in range(0, len(walls)):
- for e in range(0, len(walls[i])):
- if walls[i][e] == 1:
- pygame.draw.rect(screen, (0, 0, 255), [e * 16, i * 16, 16, 16])
- if walls[i][e] == 0:
- pygame.draw.rect(screen, (0, 0, 0), [e * 16, i * 16, 16, 16])
- tx = player.rect.x / 16
- ty = player.rect.y / 16
- pygame.draw.rect(screen, (0,255,0), [tx * 16, ty * 16, 16, 16])
- pygame.draw.rect(screen, (0,255,0), [(tx + 1) * 16, ty * 16, 16, 16])
- pygame.draw.rect(screen, (0,255,0), [tx * 16, (ty + 1) * 16, 16, 16])
- pygame.draw.rect(screen, (0,255,0), [(tx + 1) * 16, (ty + 1) * 16, 16, 16])
- pygame.draw.rect(screen, (0,0,0), ballbuff) #used to be screen.blit...so change back for images
- pygame.draw.rect(screen, (255, 200, 0), player)
- clock.tick(30)
- pygame.display.flip()
Add Comment
Please, Sign In to add comment