import pygame import random pygame.init() pygame.font.init() win = pygame.display.set_mode((500, 500)) pygame.display.set_caption("test") bigFont = pygame.font.SysFont('Impact', 50) smallFont = pygame.font.SysFont('Impact', 35) posGround = 405 posPlayer = 40 posy = posGround -64 jumpCount = 21 walkCount = 0 bgCount = 0 groundCount =0 jump = False class Obstacle: def __init__(self, position, width): self.position = position self.width = width obs1 = Obstacle(600, random.randrange(10, 70, 10)) obs2 = Obstacle(1000, random.randrange(10, 70, 10)) obs3 = Obstacle(1400, random.randrange(10, 70, 10)) obs4 = Obstacle(2000, random.randrange(10, 70, 10)) obs5 = Obstacle(2200, random.randrange(10, 70, 10)) obs6 = Obstacle(2600, random.randrange(10, 70, 10)) def jumpHeight(count): return (-0.02*count*count+8)*20 walkSprite = [pygame.image.load('walk1.png'), pygame.image.load('walk2.png'), pygame.image.load('walk3.png'), pygame.image.load('walk4.png'), pygame.image.load('walk5.png'), pygame.image.load('walk6.png')] obstacleSprite =[pygame.image.load('obs10.png'), pygame.image.load('obs20.png'), pygame.image.load('obs30.png'), pygame.image.load('obs40.png'), pygame.image.load('obs50.png'), pygame.image.load('obs60.png'), pygame.image.load('obs70.png')] bg = pygame.image.load('bg.jpg') ground = pygame.image.load('ground.png') program_Running = True game_Running = True while program_Running: while game_Running: print('yay') pygame.time.delay(int(1000/60)) obstacles = [obs1, obs2, obs3, obs4, obs5, obs6] for event in pygame.event.get(): if event.type == pygame.QUIT: game_Running = False program_Running = False key = pygame.key.get_pressed() if key[pygame.K_SPACE] and jumpCount == 21: jump = True jumpCount = -20 if jump and jumpCount <= 20: posy = posGround - int(jumpHeight(jumpCount)) - 64 jumpCount += 1 for i in range(0, 6): if abs(obstacles[i].position - posPlayer) posGround -64 -50: game_Running = False win.fill((0, 0, 0)) win.blit(bg, (0+int(bgCount/3), 0)) win.blit(ground, (groundCount, posGround)) win.blit(walkSprite[(walkCount % 18)//3], (posPlayer, posy)) for i in range(0, 6): if obstacles[i].position < 500: if obstacles[i].width == 10: win.blit(obstacleSprite[0], (obstacles[i].position, posGround-50)) elif obstacles[i].width == 20: win.blit(obstacleSprite[1], (obstacles[i].position, posGround-50)) elif obstacles[i].width == 30: win.blit(obstacleSprite[2], (obstacles[i].position, posGround-50)) elif obstacles[i].width == 40: win.blit(obstacleSprite[3], (obstacles[i].position, posGround-50)) elif obstacles[i].width == 50: win.blit(obstacleSprite[4], (obstacles[i].position, posGround-50)) elif obstacles[i].width == 60: win.blit(obstacleSprite[5], (obstacles[i].position, posGround-50)) elif obstacles[i].width == 70: win.blit(obstacleSprite[6], (obstacles[i].position, posGround-50)) obstacles[i].position -= 5 if obstacles[i].position < -1 * obstacles[i].width: obstacles[i].width = random.randrange(10, 70, 10) obstacles[i].position = 2400 print(i, ": position: ", obstacles[i].position) pygame.display.update() walkCount += 1 groundCount -= 5 if groundCount <= -641: groundCount = 0 lostText = bigFont.render('YOU LOST', False, (255, 0, 0)) lostText2 = smallFont.render("Press Spacebar to replay", False, (255, 0, 0)) win.fill((0, 0, 0)) win.blit(lostText, (150, 150)) win.blit(lostText2, (75, 250)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: program_Running = False key = pygame.key.get_pressed() if key[pygame.K_SPACE]: game_Running = True # reseting every variable: posy = posGround - 64 jumpCount = 21 walkCount = 0 bgCount = 0 groundCount = 0 jump = False obs1 = Obstacle(600, random.randrange(10, 70, 10)) obs2 = Obstacle(1000, random.randrange(10, 70, 10)) obs3 = Obstacle(1400, random.randrange(10, 70, 10)) obs4 = Obstacle(2000, random.randrange(10, 70, 10)) obs5 = Obstacle(2200, random.randrange(10, 70, 10)) obs6 = Obstacle(2600, random.randrange(10, 70, 10))