Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #this is a platformer
- import pygame as pg
- from settings import *
- vec = pg.math.Vector2
- #initializes pygame
- pg.init()
- pg.mixer.init()
- screen = pg.display.set_mode((WIDTH,HEIGHT))
- pg.display.set_caption("My Game")
- clock = pg.time.Clock()
- #makes the sprites
- class Player(pg.sprite.Sprite):
- #sprite for the PLAYER
- def __init__(self):
- pg.sprite.Sprite.__init__(self)
- self.image = pg.Surface((30,40))
- self.image.fill(YELLOW)
- self.rect = self.image.get_rect()
- self.rect.center = (WIDTH / 2, HEIGHT - 30)
- self.pos = vec(WIDTH / 2, HEIGHT / 2)
- self.vel = vec(0,0)
- self.acc = vec(0,0)
- def jump(self):
- self.rect.x += 1
- hits = pg.sprite.spritecollide(self, platforms, False)
- self.rect.x -= 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.acc.x += self.vel.x * PLAYER_FRICTION
- # equations of motion
- self.vel += self.acc
- self.pos += self.vel + 0.5 * self.acc
- # 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
- # makes the platform sprite
- 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()
- self.rect.x = x
- self.rect.y = y
- class Mob(pg.sprite.Sprite):
- def __init__(self):
- pg.sprite.Sprite.__init__(self)
- self.image = pg.Surface((50,50))
- self.image.fill(RED)
- self.rect = self.image.get_rect()
- self.rect.x = WIDTH / 2
- self.rect.y = 100
- def update(self):
- self.rect.y += 5
- # makes the group to add sprites in group
- all_sprites = pg.sprite.Group()
- platforms = pg.sprite.Group()
- mobs = pg.sprite.Group()
- #adds the sprites to group and spawns the sprites
- player = Player()
- all_sprites.add(player)
- mob = Mob()
- all_sprites.add(mob)
- mobs.add(mob)
- # spawns and adds platforms to group
- p1 = Platform(0, HEIGHT - 40, WIDTH, 40)
- all_sprites.add(p1)
- platforms.add(p1)
- p2 = Platform(WIDTH / 2 - 50, HEIGHT - 300, 100, 20)
- all_sprites.add(p2)
- platforms.add(p2)
- p3 = Platform(WIDTH / 2 - 100, HEIGHT - 150, 200, 20)
- all_sprites.add(p3)
- platforms.add(p3)
- #Game loop
- running = True
- while running:
- #keep loop running at the right speed
- clock.tick(FPS)
- #proccess input(events)
- for event in pg.event.get():
- #check for closing the window
- if event.type == pg.QUIT:
- running = False
- if event.type == pg.KEYDOWN:
- if event.key == pg.K_SPACE:
- player.jump()
- #Update
- all_sprites.update()
- # check if the player hits the platform - only if falling
- if player.vel.y > 0:
- hits = pg.sprite.spritecollide(player, platforms, False)
- if hits:
- if player.rect.top == hits[0].rect.bottom:
- player.vel.y = 10
- else:
- player.pos.y = hits[0].rect.top + 1
- player.vel.y = 0
- #Draw / render
- screen.fill(BLACK)
- all_sprites.draw(screen)
- #after drawing everything_flip the display
- pg.display.flip()
- pg.quit()
Add Comment
Please, Sign In to add comment