Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import random
- import pygame
- acceleration = .3
- gravity = .4
- speedlimit = 3
- friction = .4
- # Class for the player
- class Player(object):
- def __init__(self):
- self.rect = pygame.Rect(32, 32, 16, 16)
- self.falling = True
- self.doublejump = False
- self.xspeed = 0
- self.yspeed = 0
- self.moving = False
- def move(self, dx, dy):
- # Move each axis separately. Collision checked both times.
- if dx != 0:
- self.move_single_axis(dx, 0)
- if dy != 0:
- self.move_single_axis(0, dy)
- def jump(self, height):
- if not self.falling:
- self.yspeed = -height
- self.falling = True
- elif not self.doublejump and self.yspeed < 3:
- self.yspeed = -height
- self.doublejump = True
- def move_single_axis(self, dx, dy):
- # Move the rect
- self.rect.x += dx
- self.rect.y += dy
- # If you collide with a wall, move out based on velocity
- for wall in walls:
- if self.rect.colliderect(wall.rect):
- if dx > 0: # Moving right; Hit the left side of the wall
- self.rect.right = wall.rect.left
- if self.moving:
- self.xspeed = 0
- if dx < 0: # Moving left; Hit the right side of the wall
- self.rect.left = wall.rect.right
- if self.moving:
- self.xspeed = 0
- if dy > 0: # Moving down; Hit the top side of the wall
- self.rect.bottom = wall.rect.top
- if self.moving:
- self.yspeed = 0
- self.xspeed + (self.xspeed<0)*friction - (self.xspeed>0)*friction
- self.falling = False
- self.doublejump = False
- if dy < 0: # Moving up; Hit the bottom side of the wall
- self.rect.top = wall.rect.bottom
- if self.moving:
- self.yspeed = 0
- # Nice class to hold a wall rect
- class Wall(object):
- def __init__(self, pos):
- walls.append(self)
- self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
- # Initialise pygame
- os.environ["SDL_VIDEO_CENTERED"] = "1"
- pygame.init()
- # Set up the display
- pygame.display.set_caption("TwipplyCraft!!1!!eleven!")
- screen = pygame.display.set_mode((320, 240))
- clock = pygame.time.Clock() # SMEE, STOP THE TICKING!!
- walls = [] # List to hold the walls
- player = Player() # Create the player
- # Holds the level layout in a list of strings.
- level = [
- "####################",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "####################",
- ]
- # Parse the level string above. W = wall
- x = y = 0
- for row in level:
- for col in row:
- if col == "#":
- Wall((x, y))
- x += 16
- y += 16
- x = 0
- running = True
- while running:
- clock.tick(60)
- for e in pygame.event.get():
- if e.type == pygame.QUIT:
- running = False
- if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
- running = False
- # Move the player if an arrow key is pressed
- key = pygame.key.get_pressed()
- if key[pygame.K_LEFT] or key[pygame.K_a]:
- if player.yspeed > speedlimit:
- player.xspeed -= acceleration
- if key[pygame.K_RIGHT] or key[pygame.K_d]:
- if player.xspeed < speedlimit:
- player.xspeed += acceleration
- # Jump with space
- if key[pygame.K_SPACE]:
- player.jump(6)
- # Falling
- if player.falling:
- player.yspeed+=gravity
- #Update Position
- player.move(player.xspeed, 0)
- player.move(0, player.yspeed)
- if not (player.xspeed == 0 or player.yspeed == 0): player.moving = True
- else: player.moving = False
- # Draw the scene
- screen.fill((0, 0, 0))
- if len(walls) > 0:
- for wall in walls:
- pygame.draw.rect(screen, (255, 255, 255), wall.rect)
- if player.rect: pygame.draw.rect(screen, (255, 200, 0), player.rect)
- pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement