Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame as pg
- from pygame.math import Vector2 as vec
- import random
- import time
- pg.init()
- WIDTH, HEIGHT, = 1000, 600
- YELLOW = pg.Color('yellow')
- GREEN = pg.Color('green')
- GRAY = pg.Color('gray11')
- RED = pg.Color(255,0,0,0)
- screen = pg.display.set_mode((WIDTH,HEIGHT))
- clock = pg.time.Clock()
- FPS = 60
- PLAYER_FRICTION = .95
- PLAYER_ACC = .2
- class Player(pg.sprite.Sprite):
- def __init__(self):
- pg.sprite.Sprite.__init__(self)
- self.image = pg.Surface((30, 40))
- self.image.fill(YELLOW)
- self.rect = self.image.get_rect(center=(WIDTH/2, HEIGHT))
- self.pos = vec(WIDTH/2, HEIGHT/2)
- self.vel = vec(0,0)
- self.acc = vec(0,0)
- def jump(self):
- self.rect.y += 1
- hits = pg.sprite.spritecollide(self, platforms, False)
- self.rect.y -= 1
- if hits:
- self.vel.y = -13
- def update(self):
- self.acc = vec(0, 0.5)
- keys = pg.key.get_pressed()
- if keys[pg.K_a]:
- self.acc.x = -PLAYER_ACC
- if keys[pg.K_d]:
- self.acc.x = PLAYER_ACC
- # apply friction
- self.vel.x *= PLAYER_FRICTION
- self.vel += self.acc
- self.pos += self.vel
- # wrap around the sides of the screen
- if self.pos.x > WIDTH:
- self.pos.x = 0
- if self.pos.x < 0:
- self.pos.x = WIDTH
- self.rect.midbottom = self.pos
- class Platform(pg.sprite.Sprite):
- def __init__(self, x, y, w, h):
- pg.sprite.Sprite.__init__(self)
- self.image = pg.Surface((w, h))
- self.image.fill(GREEN)
- self.rect = self.image.get_rect(topleft=(x, y))
- class Mob(pg.sprite.Sprite):
- def __init__(self):
- pg.sprite.Sprite.__init__(self)
- self.image = pg.Surface((40,40))
- self.image.fill(RED)
- self.rect = self.image.get_rect()
- self.rect.x = random.randrange(WIDTH - self.rect.width)
- self.rect.y = random.randrange(-100, -40)
- self.speedy = random.randrange(1, 8)
- def update(self):
- self.rect.y += self.speedy
- all_sprites = pg.sprite.Group()
- platforms = pg.sprite.Group()
- MOBS = pg.sprite.Group()
- # spawns the sprites ands adds them to the group
- player = Player()
- all_sprites.add(player)
- # mobs = Mob()
- #all_sprites.add(mobs)
- #MOBS.add(mobs)
- #time.sleep(40)
- # spawns and adds platforms to group
- p1 = Platform(0, HEIGHT - 40, WIDTH, 40)
- p2 = Platform(WIDTH / 2 - 50, HEIGHT - 300, 100, 20)
- p3 = Platform(WIDTH / 2 - 100, HEIGHT - 150, 200, 20)
- all_sprites.add(p1, p2, p3)
- platforms.add(p1, p2, p3)
- running = True
- while running:
- clock.tick(FPS)
- for event in pg.event.get():
- if event.type == pg.QUIT:
- running = False
- if event.type == pg.KEYDOWN:
- if event.key == pg.K_SPACE:
- player.jump()
- all_sprites.update()
- # Check if we hit a wall/platform.
- hits = pg.sprite.spritecollide(player, platforms, False)
- for platform in hits: # Iterate over the collided platforms.
- if player.vel.y > 0: # We're falling.
- player.rect.bottom = platform.rect.top
- player.vel.y = 0
- elif player.vel.y < 0: # We're jumping.
- player.rect.top = platform.rect.bottom
- player.vel.y = 3
- player.pos.y = player.rect.bottom
- #Draw / render
- screen.fill(GRAY)
- all_sprites.draw(screen)
- pg.display.flip()
- pg.quit()
Advertisement
Add Comment
Please, Sign In to add comment