Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- from objects import *
- from functions import *
- pygame.mixer.pre_init()
- pygame.init()
- pygame.font.init()
- timer = pygame.font.SysFont('arial', 20)
- #Obstacle Variables
- health_x = 100
- lives = 3
- #Variables
- level = 0
- health_x = 100
- #Clock
- clock = pygame.time.Clock()
- timerDisplaySeconds = 0
- timerDisplayMinutes = 0
- timerDisplayMilliseconds = 0
- #Play background music
- pygame.mixer.music.load("Rick.mp3")
- pygame.mixer.music.set_volume(0.5)
- pygame.mixer.music.play(-1)
- #Player
- #Player
- image = pygame.image.load("duckleft.png")
- player = image.get_rect()
- player.x = 570
- player.y = 555
- #Doors
- imaged1 = pygame.image.load("Teleporter1.png")
- door1 = imaged1.get_rect()
- door1.x = 570
- door1.y = 30
- imaged2 = pygame.image.load("Teleporter2.png")
- door2 = imaged2.get_rect()
- door2.x = 2
- door2.y = 59
- #goal
- imageg = pygame.image.load("goal.png")
- goal = imageg.get_rect()
- goal.x = 500
- goal.y = 0
- #Jump Variables
- speed_x = 0
- speed_y = 0
- grav = 0.5
- accel = -7
- jumping = False
- playerFalling = False
- duckjumpupdate = ()
- #Screen Background
- WIDTH = 600
- HEIGHT = 600
- screen_size=(WIDTH,HEIGHT)
- screen=pygame.display.set_mode(screen_size)
- background = (0,0,0)
- #Health Bar
- health = pygame.Rect(25, 20, health_x, 15)
- health_bar = pygame.Rect(20,15, 170, 25)
- game_run = True
- while game_run:
- if level == 0:
- game_menu()
- #Background
- screen.fill(background)
- speed_y += grav
- events = pygame.event.get()
- for event in events:
- if pygame.event == pygame.QUIT:
- exit()
- elif event.type == pygame.KEYDOWN:
- if not jumping and not playerFalling:
- if event.key == pygame.K_UP:
- # jumping acceleration
- speed_y = accel
- jumping = True
- #Get a list of all the buttons that were pressed
- keys_pressed = pygame.key.get_pressed()
- #If cursor keys pressed
- if not jumping:
- speed_x = 0
- if keys_pressed[pygame.K_LEFT] == 1:
- duckjumpupdate = ("Left")
- speed_x = -1
- if keys_pressed[pygame.K_RIGHT] == 1:
- duckjumpupdate = ("Right")
- speed_x = 1
- player = player.move(speed_x, speed_y)
- #Set Restrictors
- if jumping or not jumping:
- if player.colliderect(restrictions[0]):
- player.x = 0
- if player.colliderect(restrictions[1]):
- player.x = 565
- if level == 1:
- # Draw platforms
- for plat2 in platforms1_a:
- pygame.draw.rect(screen, (250, 250, 250), plat2)
- if player.colliderect(plat2) and speed_y <= 0:
- speed_x = 0
- jumping = False
- speed_y += grav
- player.top = plat2.bottom
- playerFalling = True
- for plat1 in platforms1_b:
- pygame.draw.rect(screen, (250, 0, 250), plat1)
- if player.colliderect(plat1) and speed_y >= 0:
- speed_x = 0
- speed_y = 0
- player.bottom = plat1.top
- jumping = False
- playerFalling = False
- if player.colliderect(floor1):
- speed_x = 0
- speed_y = 0
- player.bottom = floor1.top
- jumping = False
- playerFalling = False
- #Draw door to next level
- screen.blit(imaged1, door1)
- #Draw obstacles
- for obs in obstacles1:
- pygame.draw.rect(screen, (210,220,20), obs)
- #Draw floor
- pygame.draw.rect(screen, (250,0,250), floor1)
- #move obstacles
- obstacles1[0] = obstacles1[0].move(-1,0)
- obstacles1[1] = obstacles1[1].move(1,0)
- obstacles1[2] = obstacles1[2].move(1.5,0)
- obstacles1[3] = obstacles1[3].move(0,2.5)
- obstacles1[4] = obstacles1[4].move(0,2.5)
- obstacles1[5] = obstacles1[5].move(0,2.5)
- #If obstacles collide
- if obstacles1[0].colliderect(restrictions[0]):
- obstacles1[0] = pygame.Rect(600, 370, 10,10)
- if obstacles1[1].colliderect(restrictions[1]):
- obstacles1[1] = pygame.Rect(1,450,10,10)
- if obstacles1[2].colliderect(restrictions[1]):
- obstacles1[2] = pygame.Rect(1, 290, 10,10)
- if obstacles1[3].colliderect(plat1):
- obstacles1[3] = pygame.Rect(450, 0, 10,10)
- if obstacles1[4].colliderect(plat1):
- obstacles1[4] = pygame.Rect(350, 0, 10,10)
- if obstacles1[5].colliderect(plat1):
- obstacles1[5] = pygame.Rect(250, 0, 10,10)
- #move platforms
- platforms1_a[4] = platforms1_a[4].move(1,0)
- platforms1_b[4] = platforms1_b[4].move(1,0)
- if player.bottom == platforms1_b[4].top:
- player = player.move(1,0)
- platforms1_a[5] = platforms1_a[5].move(-1,0)
- platforms1_b[5] = platforms1_b[5].move(-1,0)
- if player.bottom == platforms1_b[5].top:
- player = player.move(-1,0)
- if platforms1_a[4].left > 600:
- platforms1_a[4] = pygame.Rect(-100, 320, 100, 2)
- platforms1_b[4] = pygame.Rect(-100, 313, 100, 7)
- if platforms1_a[5].right < 0:
- platforms1_a[5] = pygame.Rect(600, 240, 100, 2)
- platforms1_b[5] = pygame.Rect(600, 233, 100, 7)
- #Lose health when damaged
- for obs in obstacles1:
- if (player.colliderect(obs)) and health_x > 0:
- health_x = health_x - 1
- health = pygame.Rect(25, 20, health_x, 15)
- if health_x == 0 and lives > 1:
- lives = lives - 1
- health_x = 100
- if health_x == 0 and lives == 1:
- game_run = False
- if player.colliderect(door1):
- level, player = level_counter(level,player)
- if level == 2:
- #platforms
- for plat2a in platforms2_a:
- pygame.draw.rect(screen, (250, 250, 250), plat2a)
- if player.colliderect(plat2a) and (speed_y <= 0):
- speed_x = 0
- jumping = False
- speed_y += grav
- player.top = plat2a.bottom
- playerFalling = True
- for plat2b in platforms2_b:
- pygame.draw.rect(screen, (65, 105, 225), plat2b)
- if player.colliderect(plat2b) and (speed_y >= 0):
- speed_x = 0
- speed_y = 0
- player.bottom = plat2b.top
- jumping = False
- playerFalling = False
- #Floor
- pygame.draw.rect(screen, (65,105,225), floor2)
- #Draw door to next level
- screen.blit(imaged2, door2)
- #if collide floor
- if player.colliderect(floor2):
- speed_x = 0
- speed_y = 0
- player.bottom = floor2.top
- jumping = False
- playerFalling = False
- platforms2_a[1] = platforms2_a[1].move(-2,0)
- platforms2_b[1] = platforms2_b[1].move(-2,0)
- platforms2_a[2] = platforms2_a[2].move(2,0)
- platforms2_b[2] = platforms2_b[2].move(2,0)
- platforms2_a[7] = platforms2_a[7].move(0,2.3)
- platforms2_b[7] = platforms2_b[7].move(0,2.3)
- if platforms2_a[1].right < 0:
- platforms2_a[1] = pygame.Rect(600, 410, 40, 2)
- platforms2_b[1] = pygame.Rect(600, 403, 40, 7)
- if platforms2_a[2].left > 600:
- platforms2_a[2] = pygame.Rect(-80, 336, 40, 2)
- platforms2_b[2] = pygame.Rect(-80, 329, 40, 7)
- if platforms2_a[7].bottom > 187:
- platforms2_a[7] = pygame.Rect(120, 0,108, 2)
- platforms2_b[7] = pygame.Rect(120,-7,108, 7)
- if player.bottom == platforms2_b[1].top:
- speed_x = -2
- if player.bottom == platforms2_b[2].top:
- speed_x = 2
- if player.bottom == platforms2_b[7].top:
- player = player.move(0,2.3)
- for obs2 in obstacles2:
- pygame.draw.rect(screen, (210,160, 20), obs2)
- #move obstacles
- obstacles2[0] = obstacles2[0].move(2.5,0)
- obstacles2[1] = obstacles2[1].move(2.5,0)
- obstacles2[2] = obstacles2[2].move(0,3)
- obstacles2[3] = obstacles2[3].move(0,3)
- obstacles2[4] = obstacles2[4].move(0,2.5)
- obstacles2[5] = obstacles2[5].move(0,2.5)
- #if obs collide
- if obstacles2[0].colliderect(restrictions[1]):
- obstacles2[0] = pygame.Rect(0, 458, 10,10)
- if obstacles2[1].colliderect(restrictions[1]):
- obstacles2[1] = pygame.Rect(0, 458, 10,10)
- if obstacles2[2].colliderect(platforms2_b[3]):
- obstacles2[2] = pygame.Rect(525, 0, 10,10)
- if obstacles2[3].colliderect(platforms2_b[3]):
- obstacles2[3] = pygame.Rect(565, 0, 10,10)
- if obstacles2[4].colliderect(platforms2_b[5]):
- obstacles2[4] = pygame.Rect(442, 0, 10,10)
- if obstacles2[5].colliderect(platforms2_b[5]):
- obstacles2[5] = pygame.Rect(370, 0, 10,10)
- for obs2 in obstacles2:
- if (player.colliderect(obs2)) and health_x > 0:
- health_x = health_x - 2
- health = pygame.Rect(25, 20, health_x, 15)
- if player.colliderect(door2):
- level, player = level_counter(level,player)
- if level == 3:
- #floor
- pygame.draw.rect(screen, (0,255,0), floor3)
- #if collide floor
- if player.colliderect(floor3):
- speed_x = 0
- speed_y = 0
- player.bottom = floor3.top
- jumping = False
- playerFalling = False
- for plat3a in platforms3_a:
- pygame.draw.rect(screen, (250,250,250), plat3a)
- if player.colliderect(plat3a) and (speed_y <= 0):
- speed_x = 0
- jumping = False
- speed_y += grav
- player.top = plat3a.bottom
- playerFalling = True
- for plat3b in platforms3_b:
- pygame.draw.rect(screen, (0,255,0), plat3b)
- if player.colliderect(plat3b) and (speed_y >= 0):
- speed_x = 0
- speed_y = 0
- player.bottom = plat3b.top
- jumping = False
- playerFalling = False
- platforms3_b[1] = platforms3_b[1].move(0,-1)
- platforms3_a[1] = platforms3_a[1].move(0,-1)
- if platforms3_b[1].top < 418:
- platforms3_a[1] = pygame.Rect(0,555,100,2)
- platforms3_b[1] = pygame.Rect(0,548,100,7)
- #obstacles
- for obs3 in obstacles3:
- pygame.draw.rect(screen, (210,160,20), obs3)
- if player.colliderect(obs3):
- health_x = health_x - 1
- health = pygame.Rect(25, 20, health_x, 15)
- for superobs in superobstacle:
- pygame.draw.rect(screen, (255,0,0), superobs)
- if player.colliderect(superobs):
- health_x = health_x - 6
- health = health = pygame.Rect(25, 20, health_x, 15)
- #Draw goal
- screen.blit(imageg, goal)
- #Move objects
- #Move obstacles
- obstacles3[0] = obstacles3[0].move(0,1)
- obstacles3[1] = obstacles3[1].move(0,1)
- obstacles3[2] = obstacles3[2].move(0,1)
- obstacles3[3] = obstacles3[3].move(0,1)
- obstacles3[4] = obstacles3[4].move(-5,0)
- obstacles3[5] = obstacles3[5].move(3.5,0)
- superobstacle[0] = superobstacle[0].move(-4,0)
- superobstacle[1] = superobstacle[1].move(4,0)
- #Move platforms
- platforms3_a[2] = platforms3_a[2].move(-2,0)
- platforms3_b[2] = platforms3_b[2].move(-2,0)
- if platforms3_a[2].left < 0:
- platforms3_a[2].right = 600
- platforms3_b[2].right = 600
- platforms3_a[3] = platforms3_a[3].move(2,0)
- platforms3_b[3] = platforms3_b[3].move(2,0)
- if platforms3_a[3].right > 600:
- platforms3_a[3].left = 0
- platforms3_b[3].left = 0
- platforms3_a[4] = platforms3_a[4].move(0,-2)
- platforms3_b[4] = platforms3_b[4].move(0,-2)
- if platforms3_a[4].top < 105:
- platforms3_a[4].top = 250
- platforms3_b[4].top = 243
- #Move player with mobile platforms
- if player.bottom == platforms3_b[2].top:
- player = player.move(-2,0)
- if player.bottom == platforms3_b[3].top:
- player = player.move(2,0)
- if player.bottom == platforms3_b[4].top:
- player = player.move(0,2)
- if obstacles3[0].colliderect(floor3):
- obstacles3[0] = pygame.Rect(510, 427, 10,10)
- if obstacles3[1].colliderect(floor3):
- obstacles3[1] = pygame.Rect(410, 427, 10,10)
- if obstacles3[2].colliderect(floor3):
- obstacles3[2] = pygame.Rect(310, 427, 10,10)
- if obstacles3[3].colliderect(floor3):
- obstacles3[3] = pygame.Rect(210, 427, 10,10)
- if obstacles3[4].colliderect(restrictions[0]):
- obstacles3[4] = pygame.Rect(600, 520, 10,10)
- if obstacles3[5].colliderect(restrictions[1]):
- obstacles3[5] = pygame.Rect(0,387,10,10)
- if superobstacle[0].colliderect(restrictions[0]):
- superobstacle[0] = pygame.Rect(600,280,20,20)
- if superobstacle[1].colliderect(restrictions[1]):
- superobstacle[1] = pygame.Rect(0,200,20,20)
- if player.colliderect(goal):
- level = 0
- if level != 0 and level != 4 :
- #Update duck image
- x, y = duck_update(image, duckjumpupdate, player, jumping)
- player = player.move(speed_x, speed_y)
- # Draw health bar
- pygame.draw.rect(screen, (250, 250, 250), health_bar)
- # Draw health
- pygame.draw.rect(screen, (0, 255, 0), health)
- if lives > 2:
- screen.blit(IMAGE, life[0])
- if lives > 1:
- screen.blit(IMAGE, life[1])
- if lives > 0:
- screen.blit(IMAGE, life[2])
- #Manage health
- if health_x <= 0 and lives > 1:
- lives = lives - 1
- health_x = 100
- if health_x <= 0 and lives == 1:
- game_run = False
- if health_x < 60:
- pygame.draw.rect(screen, (255, 165, 0), health)
- if health_x < 30:
- pygame.draw.rect(screen, (255,0,0), health)
- textsurface = timer.render(str(timerDisplayMinutes) + ":" + "{:02}".format(timerDisplaySeconds), False, (255, 255, 255))
- screen.blit(textsurface,(200,16))
- #Change clock
- clock.tick(60)
- timerDisplayMilliseconds += 1
- if timerDisplayMilliseconds == 60:
- timerDisplaySeconds += 1
- timerDisplayMilliseconds = 0
- if timerDisplaySeconds == 60:
- timerDisplayMinutes = min(59, timerDisplayMinutes + 1)
- timerDisplaySeconds = 0
- #Display
- pygame.display.update()
- #Time delay
- pygame.time.delay(10)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement