from pygame import mixer
def show_score(x,y):
font_score=font.render(‘Score: ‘+str(score),True,(255,0,0))
screen.blit(font_score,(x,y))
def game_over(x,y):
font_game_over=font1.render(‘GAME OVER!’,True,(255,0,0))
screen.blit(font_game_over,(x,y))
#intialize the pygame
pygame.init()
#create the screen
screen=pygame.display.set_mode((1000,400))
#title and icon
pygame.display.set_caption(‘Dinosaur’)
icon=pygame.image.load(‘logo.png’)
pygame.display.set_icon(icon)
#background
background_img=pygame.image.load(‘background.jpg’)
background_x=0
background_y=0
background_width=1000
background_hight=400
background=pygame.transform.scale(background_img,(background_width,background_hight))
#dinosaur
dinosaur_img=pygame.image.load(‘dinosaur.png’)
dinosaur_x=30
dinosaur_y=330
dinosaur_width=50
dinosaur_hight=53
dinosaur=pygame.transform.scale(dinosaur_img,(dinosaur_width,dinosaur_hight))
y_dinosaur_velocity=7
gravity=0.2
#tree
tree_img=pygame.image.load(‘tree.png’)
tree_x=900
tree_y=330
tree_width=25
tree_hight=43
tree=pygame.transform.scale(tree_img,(tree_width,tree_hight))
x_tree_velocity=5
#tree2
tree2_img=pygame.image.load(‘tree2.png’)
tree2_x=1400
tree2_y=330
tree2_width=60
tree2_hight=43
tree2=pygame.transform.scale(tree2_img,(tree2_width,tree2_hight))
#score
score=0
font=pygame.font.Font(‘freesansbold.ttf’,32)
score_x=10
score_y=10
#game over
font1=pygame.font.Font(‘freesansbold.ttf’,70)
game_over_x=300
game_over_y=200
#Sound
bgmusic=mixer.Sound(‘bgmusic.mp3’)
sound1=mixer.Sound(‘tick.wav’)
sound2=mixer.Sound(‘te.wav’)
mixer.Sound.play(bgmusic)
clock=pygame.time.Clock()
#game loop
running=True
jump=False
pausing=False
while running:
clock.tick(60)
#RGB – Red, Green, Blue
screen.fill((255,255,255))
background1_rect=screen.blit(background,(background_x,background_y))
background2_rect=screen.blit(background,(background_x+1000,background_y))
background_x -= x_tree_velocity
if background_x+1000<=0:
background_x=0
dinosaur_rect=screen.blit(dinosaur,(dinosaur_x,dinosaur_y))
#dinosaur jump
if 200<=dinosaur_y<=330:
if jump==True:
y_dinosaur_velocity -= gravity
dinosaur_y -=y_dinosaur_velocity
else:
jump=False
if dinosaur_y>=330 and jump==False:
dinosaur_y=330
y_dinosaur_velocity=7
gravity=0.2
#tree1
tree_rect=screen.blit(tree,(tree_x,tree_y))
tree_x -= x_tree_velocity
if tree_x<=-20:
score += 1
tree_x=900
#tree2
tree2_rect=screen.blit(tree2,(tree2_x,tree2_y))
tree2_x -= x_tree_velocity
if tree2_x<=-20:
score += 1
tree2_x=900
#Collision Detection
if dinosaur_rect.colliderect(tree_rect) or dinosaur_rect.colliderect(tree2_rect):
pausing=True
mixer.Sound.play(sound2)
y_dinosaur_velocity=0
x_tree_velocity=0
gravity=0
game_over(game_over_x,game_over_y)
mixer.Sound.stop(bgmusic)
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_SPACE:
if dinosaur_y==330:
jump=True
mixer.Sound.play(sound1)
if pausing:
background_x=0
background_y=0
tree_x=900
tree_y=330
x_tree_velocity=5
tree2_x=1400
tree2_y=330
dinosaur_x=30
dinosaur_y=330
y_dinosaur_velocity=7
gravity=0.2
score=0
pausing=False
show_score(score_x,score_y)
pygame.display.update()