eFFeeMMe
By: a guest | Feb 9th, 2010 | Syntax:
Python | Size: 2.98 KB | Hits: 22 | Expires: Never
'''
Created on Feb 9, 2010
@author: effeemme
A little platformer. The challenge was to make it in 5 minutes. Took me 10 :( then I cleaned it up a bit.
'''
import pygame
pygame.init()
class Block:
def __init__(self, x, y):
self.x = x
self.y = y
class Character(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.canjump = False
self._xSpeed = 0.0
self._ySpeed = 0.0
def step(self):
self.x += self.xSpeed
self.y += self.ySpeed
self.ySpeed += 0.5
def jump(self):
if self.canjump:
self.ySpeed = -16
self.canjump = False
def draw(self, display):
pygame.draw.rect(display, (255, 255, 255), (self.x - 8, self.y - 16, 16, 16))
pygame.draw.rect(display, (255, 0, 0), (self.x - 8, self. y - 1, 16, 2))
def get_xSpeed(self):
return self._xSpeed
def set_xSpeed(self, value):
self._xSpeed = value
if self._xSpeed < -4:
self._xSpeed = -4
if self._xSpeed > 4:
self._xSpeed = 4
def get_ySpeed(self):
return self._ySpeed
def set_ySpeed(self, value):
self._ySpeed = value
if self._ySpeed < -4:
self._ySpeed = -4
if self._ySpeed > 4:
self._ySpeed = 4
xSpeed = property(get_xSpeed, set_xSpeed)
ySpeed = property(get_ySpeed, set_ySpeed)
if __name__ == "__main__":
clock = pygame.time.Clock()
display = pygame.display.set_mode((640, 480))
c = Character(50, 50)
blocks = [Block(x, 80) for x in range(20, 200, 16)]
pressedKeys = set()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
import sys
sys.exit()
elif event.type == pygame.KEYDOWN:
pressedKeys.add(event.key)
elif event.type == pygame.KEYUP:
pressedKeys.remove(event.key)
notAccelerating = True
if pygame.K_UP in pressedKeys:
c.jump()
if pygame.K_LEFT in pressedKeys:
c.xSpeed -= 2
notAccelerating = False
if pygame.K_RIGHT in pressedKeys:
c.xSpeed += 2
notAccelerating = False
if notAccelerating:
if c.xSpeed != 0:
c.xSpeed -= abs(c.xSpeed) / c.xSpeed
c.step()
for block in blocks:
if abs(c.x - block.x) <= 8:
if c.y - 8 <= block.y <= c.y:
c.y = block.y
c.canjump = True
display.fill((0,0,0))
c.draw(display)
for block in blocks:
pygame.draw.line(display, (255,255,255), (block.x-8, block.y), (block.x+8, block.y), 4)
pygame.display.flip()
clock.tick(30)