projet-dino-club_info/tests.py

204 lines
No EOL
7.8 KiB
Python

import pygame
import random
pygame.init()
pygame.font.init()
win = pygame.display.set_mode((500, 500)) # Set the canvas size
pygame.display.set_caption("test dino") # Set the title of the screen (useless for a browser game)
bigFont = pygame.font.SysFont('Impact', 50) # Initiate font for the big 'you lose'
smallFont = pygame.font.SysFont('Impact', 35) # Initiate font for the tes=xt telling to press Space
posGround = 405 # Y coordinate of the ground
posPlayer = 40 # X coordinate the player will stay at
posy = posGround -64 # Initiate the posy of the player
jumpCount = 21 # Jumpcount stays at 21, when the player presses Space it goes form - to + 20 (which)
walkCount = 0 # Increases steadily for the walk sprites to chain
bgCount = 0 # Used to loop the background, WILL NEED TO BE ADJUSTED WITH THE SPRITE
groundCount = 0 # Used to loop the ground, WILL NEED TO BE ADJUSTED WITH THE SPRITE
jump = False # Is toggled when the player jumps
class Obstacle: # define all obstacles
def __init__(self, position, width):
self.position = position
self.width = width
#keeps the greatest score
highscore = 0
# define all obstacles
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))
# the function that returns the height as a function of the Jumpcount
# (from -20 to 20 as these are the roots of the polynomial)
def jumpHeight(count):
return (-0.02*count*count+8)*20
# Load all the sprites of the guy walking WILL NEED TO BE ADJUSTED WITH THE SPRITE
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')]
# Load all the sprites of the obstacles WILL NEED TO BE ADJUSTED WITH THE SPRITE
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')]
# Load the background WILL NEED TO BE ADJUSTED WITH THE SPRITE
bg = pygame.image.load('bg.jpg')
# Load the ground WILL NEED TO BE ADJUSTED WITH THE SPRITE
ground = pygame.image.load('ground.png')
# Checks to keep the window openned
program_Running = True
# Checks to keep the game running (if not displays lost screen)
game_Running = True
while program_Running:
while game_Running:
pygame.time.delay(int(1000/60))
obstacles = [obs1, obs2, obs3, obs4, obs5, obs6] # Creates a list of obstacles
# If we hit the quit (close window) ==> Breaks out of both loops
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: # JumpCount == 21 means the player is on the ground, ie the last jump is over
jump = True
jumpCount = -20
if jump and jumpCount <= 20:
posy = posGround - int(jumpHeight(jumpCount)) - 64
jumpCount += 1
for i in range(0, 6):
# checks if any of the obstacles are at the point where the player is
if abs(obstacles[i].position - posPlayer) < obstacles[i].width/2:
# checks in that case if the player is high enough not to touch or not
if posy > posGround - 64 - 50:
game_Running = False
win.fill((0, 0, 0)) # Cleans the window
win.blit(bg, (0+int(bgCount/3), 0)) # Displays the Bg (with a 1/3 ratio compared to the 1/frame incrementation)
win.blit(ground, (groundCount, posGround)) # Displays the ground WILL NEED TO BE ADJUSTED WITH THE SPRITE AND TO BE LOOPED
win.blit(walkSprite[(walkCount % 18)//3], (posPlayer, posy)) # Displays the caracter WILL NEED TO BE ADJUSTED WITH THE SPRITE
# Displays current score
scoreText = smallFont.render(str(walkCount), False, (255, 0, 0))
win.blit(scoreText, (430, 0))
# Displays highest score
highScoreText = smallFont.render(str(highscore), False, (255, 0, 0))
win.blit(highScoreText, (30, 0))
for i in range(0, 6): # Checks across every Obstacle
if obstacles[i].position < 500: # if it is in the FOV
# Blits the corresponding image (w/ respect to width)
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))
# Moves the obstacle 5px left
obstacles[i].position -= 5
# Checks if an obstacle is out of the FOV in the negatives (farther away from 0 that its width)
if obstacles[i].position < -1 * obstacles[i].width:
# Sets a random width for the obstacle
obstacles[i].width = random.randrange(10, 70, 10)
# Tps it to 2400
obstacles[i].position = 2400
# Debuguer print(i, ": position: ", obstacles[i].position)
# Update
pygame.display.update()
walkCount += 1 # Walks one step
groundCount -= 5 # the ground's moving fast
# <!> TEMPORARY SOLUTION : TO BE FIXED
if groundCount <= -641:
groundCount = 0
if walkCount > highscore:
highscore = walkCount
# The two texts when you lose
lostText = bigFont.render('YOU LOST', False, (255, 0, 0))
lostText2 = smallFont.render("Press Spacebar to replay", False, (255, 0, 0))
# Clean the window
win.fill((0, 0, 0))
# Print both texts
win.blit(lostText, (150, 150))
win.blit(lostText2, (75, 320))
# Displays current score
scoreText = smallFont.render("Score : " + str(walkCount), False, (255, 0, 0))
win.blit(scoreText, (175, 210))
# Displays highest score
highScoreText = smallFont.render("highest Score : " + str(highscore), False, (255, 0, 0))
win.blit(highScoreText, (100, 250))
# Update
pygame.display.update()
# Check to quit (the other check is within hte game loop)
for event in pygame.event.get():
if event.type == pygame.QUIT:
program_Running = False
# Checks for another game:
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))
pygame.quit()