import pygame import sys from pygame.locals import * import random import time pygame.init() vec = pygame.math.Vector2 HEIGHT = 450 WIDTH = 400 ACC = 0.5 FRIC = -0.12 FPS = 60 FramePerSec = pygame.time.Clock() displaysurface = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Game") class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() self.surf = pygame.Surface((30, 30)) self.surf.fill((128,255,40)) self.rect = self.surf.get_rect() self.pos = vec((10, 385)) self.vel = vec(0, 0) self.acc = vec(0, 0) self.jumping = False self.score = 0 def move(self): self.acc = vec(0,0.5) pressed_keys = pygame.key.get_pressed() if pressed_keys[K_LEFT]: self.acc.x = -ACC if pressed_keys[K_RIGHT]: self.acc.x = ACC self.acc.x += self.vel.x * FRIC self.vel += self.acc self.pos += self.vel + 0.5 * self.acc if self.pos.x > WIDTH: self.pos.x = 0 if self.pos.x < 0: self.pos.x = WIDTH self.rect.midbottom = self.pos #jumping def jump (self): hits = pygame.sprite.spritecollide(self, platforms, False) if hits: self.vel.y = - 15#Applying up force def cancel_jump(self): if self.jumping: if self.vel.y <-3: self.vel.y = -3 def update(self): hits = pygame.sprite.spritecollide(P1 , platforms, False ) if P1.vel.y > 0: if hits: if self.pos.y= HEIGHT: plat.kill() if P1.rect.top > HEIGHT: for entity in all_sprites: entity.kill() time.sleep(1) displaysurface.fill((255,0,0)) pygame.display plat_gen() displaysurface.fill((0,0,0)) f = pygame.font.SysFont("Verdana",20) g = f.render(str(P1.score), True, (123,255,0)) displaysurface.blit(g, (WIDTH/2, 10)) for entity in all_sprites: displaysurface.blit(entity.surf, entity.rect) entity.move() pygame.display.update() FramePerSec.tick(FPS)