Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!user/bin/env python
- import sys, pygame, os
- from pygame.locals import *
- #--- Our Variables
- black = (0,0,0)
- size = (320, 17*16)
- speed = [0,5]
- #--- Init pygame
- os.environ["SDL_VIDEO_CENTERED"] = "1"
- 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, ty = int(player.rect.x / 16), 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[0])][int(h[1])].tNum == 1:
- wam = walls[int(h[0])][int(h[1])].rect
- 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):
- self.rect = pygame.Rect(pos[0], pos[1], pos[2], pos[3])
- self.tNum = pos[4]
- #--- Variables
- clock = pygame.time.Clock()
- #ball = pygame.image.load("ball.jpg").convert()
- jumping = False
- jump, jumpNum, inc, jHeight = 0, 2, 1, 10
- walls = []
- player = Player()
- camera = pygame.Rect(0, 0, screen.get_width(), screen.get_height())
- def translate(rect):
- global camera
- return pygame.Rect(rect.x - camera.x, rect.y - camera.y, rect.w, rect.h)
- def getEvents():
- global camera
- 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 parseLevel(filename):
- fileObj = open("level.txt")
- st = fileObj.read()
- fileObj.close()
- x = st.find(".", 0)
- y = st.find("(", x)
- h = 0
- m = []
- for i in range(0, int(st[0:x])):
- walls.append([])
- for e in range(0, int(st[x + 1:y])):
- h = st.find("(", h) + 1
- h2 = st.find(")", h)
- for la in range(0, 5):
- go = st.find(",", h, h2)
- m.append(int(st[h:go]))
- h = go + 1
- if m[4] == 2:
- m[4] = 0
- player.rect = pygame.Rect(m[0], m[1], m[2], m[3])
- walls[i].append(Wall((m[0], m[1], m[2], m[3], m[4])))
- m = []
- parseLevel("level.txt")
- 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])
- camera.x = player.rect.x - (camera.width / 2)
- if camera.x < 0:
- camera.x = 0
- if camera.x + camera.width > len(walls) * 16:
- camera.x = (len(walls) * 16) - camera.width
- camera.y = player.rect.y - (camera.height / 2)
- if camera.y < 0:
- camera.y = 0
- if camera.y + camera.height > len(walls[0]) * 16:
- camera.y = (len(walls[0]) * 16) - camera.height
- 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(walls)):
- for e in range(0, len(walls[i])):
- rect = translate(walls[i][e].rect)
- col = {0 : (0,0,0), 1: (0,0,255), 2 : (0,0,0)}[walls[i][e].tNum]
- pygame.draw.rect(screen, col, rect)
- #pygame.draw.rect(screen, (0, 0, 255), [e * 16, i * 16, 16, 16])
- #if walls[i][e].tNum == 0:
- # pygame.draw.rect(screen, (0, 0, 0), [e * 16, i * 16, 16, 16])
- rect = translate(ballbuff)
- pygame.draw.rect(screen, (0,0,0), rect) #used to be screen.blit...so change back for images
- rect = translate(player.rect)
- pygame.draw.rect(screen, (255, 200, 0), rect)
- clock.tick(30)
- pygame.display.flip()
Add Comment
Please, Sign In to add comment