rishabbansal21

Day-11

Jan 20th, 2021 (edited)
388
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame, random
  2. pygame.init()
  3.  
  4. clock = pygame.time.Clock()
  5.  
  6. sw = 800
  7. sh = 600
  8. screen = pygame.display.set_mode((sw,sh))
  9. pygame.display.set_caption("TILE SMASHERS")
  10.  
  11. score = 0
  12. font_32 = pygame.font.Font('freesansbold.ttf', 32)
  13. font_64 = pygame.font.Font('freesansbold.ttf', 64)
  14.  
  15.  
  16. #TIME-OUT-MODE
  17. def Time_out_mode(st):
  18.     print("HERE")
  19.     global font_64, font_32, clock
  20.     start_time = st
  21.  
  22.     xy = [random.randint(50,700), random.randint(50,500)]
  23.     def print_score(scr):
  24.         screen.blit(font_32.render("SCORE: "+str(scr), True, (0,0,0)), (10,10))
  25.  
  26.     def generate_box(x,y):
  27.         return(pygame.Rect(x,y,100,100))
  28.  
  29.     def isClicked(xy,mx,my):
  30.         global score
  31.         if xy[0] < mx < xy[0]+100 and xy[1] < my < xy[1]+100:
  32.             score += 1
  33.             print(score)
  34.             return True
  35.         return False
  36.  
  37.     clicked = False
  38.     TimeoutMode = True
  39.     start = pygame.time.get_ticks()
  40.     while TimeoutMode:
  41.         screen.fill((200,200,200))
  42.         for event in pygame.event.get():
  43.             if event.type == pygame.QUIT:
  44.                 TimeoutMode = False
  45.             if event.type == pygame.MOUSEBUTTONDOWN:
  46.                 if event.button == 1:
  47.                     clicked = True
  48.             if event.type == pygame.MOUSEBUTTONUP:
  49.                 if event.button == 1:
  50.                     clicked = False
  51.  
  52.         box = generate_box(xy[0], xy[1])
  53.         pygame.draw.rect(screen, (255,0,0), box)
  54.         mx,my = pygame.mouse.get_pos()
  55.  
  56.         current_time = pygame.time.get_ticks()
  57.         if current_time - start > 1000:
  58.             start = pygame.time.get_ticks()
  59.             xy = [random.randint(0,700), random.randint(0,500)]
  60.  
  61.         if clicked:
  62.             if (current_time-start<1000) and isClicked(xy,mx,my):
  63.                 xy = [random.randint(0,700), random.randint(0,500)]
  64.                 pygame.draw.rect(screen, (255,0,0), box)
  65.                 start = pygame.time.get_ticks()
  66.  
  67.         game_time = pygame.time.get_ticks()
  68.  
  69.         if game_time - start_time > 60000:
  70.             screen.fill((200,200,200))
  71.             msg = pygame.font.Font('freesansbold.ttf',64)
  72.             screen.blit(msg.render("GAME OVER!!", True, (0,0,0)), (180,200))
  73.  
  74.             fsc = pygame.font.Font('freesansbold.ttf', 32)
  75.             screen.blit(fsc.render("FINAL SCORE: "+str(score), True, (0,0,0)), (280,300))
  76.  
  77.         print_score(score)
  78.         clock.tick(20)
  79.         pygame.display.update()
  80.  
  81.  
  82.  
  83. #ARCADE MODE
  84. life = 10
  85. clicked = False
  86. def Arcade_mode():
  87.     global clicked, font_32, font_64, life, score
  88.  
  89.     xy = [random.randint(50,700),random.randint(50,500)]
  90.     sCoord = (10,10)
  91.     def score_print(scr):
  92.         screen.blit(font_32.render("Score: "+str(scr), True, (0,0,0)), sCoord)
  93.  
  94.     def generate_box(x,y):
  95.         return(pygame.Rect(x,y,100,100))
  96.  
  97.     def isClicked(cxy, cmx, cmy, w, h):
  98.         if cxy[0] < cmx < cxy[0] + w and cxy[1] < cmy < cxy[1] + h:
  99.             return True
  100.         return False
  101.  
  102.     def draw_lifes(l):
  103.         for i in range(l):
  104.             pygame.draw.circle(screen, (100,100,0), (760 - 30*i, 20), 15)
  105.  
  106.     start = pygame.time.get_ticks()
  107.     MainRun = True
  108.     w = 150
  109.     h = 150
  110.     box state = True
  111.     while MainRun:
  112.         screen.fill((200,200,200))
  113.         for event in pygame.event.get():
  114.             if event.type == pygame.QUIT:
  115.                 MainRun = False
  116.             if event.type == pygame.MOUSEBUTTONDOWN:
  117.                 if event.button == 1:
  118.                     clicked = True
  119.             if event.type == pygame.MOUSEBUTTONUP:
  120.                 if event.button == 1:
  121.                     clicked = False
  122.  
  123.         box = generate_box(xy[0], xy[1])
  124.         pygame.draw.rect(screen, (255,0,0), box)
  125.  
  126.         if score%10==0 and score>0 and boxstate == True:
  127.             boxstate = False
  128.             w -= 20
  129.             h -= 20
  130.  
  131.         if score%10==1:
  132.             boxstate = True    
  133.  
  134.         mx,my = pygame.mouse.get_pos()
  135.         current_time = pygame.time.get_ticks()
  136.         if (current_time - start > 2000):
  137.             if clicked == False:
  138.                 life -= 1
  139.             start = pygame.time.get_ticks()
  140.             xy = [random.randint(50,700), random.randint(50,500)]
  141.  
  142.         if clicked:
  143.             if (current_time - start < 2000) and isClicked(xy, mx, my, w, h):
  144.                 pygame.draw.rect(screen, (0,255,0), box)
  145.                 clicked = False
  146.                 score += 1
  147.                 start = pygame.time.get_ticks()
  148.                 xy = [random.randint(50,700), random.randint(0,500)]
  149.  
  150.             elif (current_time - start < 2000) and not(isClicked(xy, mx, my, w, h)):
  151.                 clicked = False
  152.                 life -= 1
  153.                 print(life)
  154.                 xy = [random.randint(50,700), random.randint(50,500)]
  155.                 start = pygame.time.get_ticks()
  156.  
  157.         draw_lifes(life)
  158.  
  159.         if life <= 0:
  160.             screen.fill((200,200,200))
  161.             msg = font_64.render("GAME OVER!!", True, (0,0,0))
  162.             screen.blit(msg, (180,200))
  163.  
  164.             fsc = font_32.render("FINAL SCORE: "+str(score), True, (0,0,0))
  165.             screen.blit(fsc, (280,300))
  166.  
  167.         score_print(score)
  168.         clock.tick(60)
  169.         pygame.display.update()
  170.  
  171.  
  172. clicked = False
  173. mode = 0
  174. state = 0
  175. MainRun = True
  176. while MainRun:
  177.     screen.fill((200,200,200))
  178.     for event in pygame.event.get():
  179.         if event.type == pygame.QUIT:
  180.             MainRun = False
  181.  
  182.         if event.type == pygame.MOUSEBUTTONDOWN:
  183.             if event.button == 1:
  184.                 clicked = True
  185.         if event.type == pygame.MOUSEBUTTONUP:
  186.             if event.button == 1:
  187.                 clicked = False
  188.  
  189.     mx, my = pygame.mouse.get_pos()
  190.  
  191.     if clicked and state == 0:
  192.         if 285<mx<420 and sh-360<my<sh-323: mode = 1
  193.         elif 285<mx<420 and sh-320<my<sh-283: mode = 2
  194.         elif sw//2-95 < mx < sw//2+95 and sh-150 < my <sh-30: state = 1
  195.  
  196.     if mode == 1:
  197.         pygame.draw.rect(screen, (255,0,0), pygame.Rect(285,sh-365,200,37),2)
  198.     elif mode == 2:
  199.         pygame.draw.rect(screen, (255,0,0), pygame.Rect(285,sh-325,200,37), 2)
  200.  
  201.     if state == 1:
  202.         pygame.draw.rect(screen, (0,255,0), pygame.Rect(sw//2-95,sh-107,230,70),  2)
  203.         if mode == 1:
  204.             MainRun = False
  205.             Time_out_mode(pygame.time.get_ticks())
  206.  
  207.         if mode == 2:
  208.             MainRun = False
  209.             Arcade_mode()
  210.  
  211.  
  212.     Welcome_Message = font_64.render("TILE SMASHERS", True, (0,0,200))
  213.     screen.blit(Welcome_Message, (150, 20))
  214.  
  215.     Select_Mode = font_32.render("SELECT MODE:", True, (0,0,200))
  216.     screen.blit(Select_Mode, (10, sh-400))
  217.  
  218.     Time_out = font_32.render("TIME-OUT", True, (0,0,200))
  219.     screen.blit(Time_out, (290, sh-360))
  220.  
  221.     Arcade = font_32.render("ARCADE", True, (0,0,200))
  222.     screen.blit(Arcade, (290, sh- 320))
  223.  
  224.     Start = font_64.render("START", True, (0,0,200))
  225.     screen.blit(Start, (sw//2 - 80, sh-100))
  226.    
  227.     clock.tick(10)
  228.     pygame.display.update()
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
RAW Paste Data