Advertisement
rishabbansal21

Day-9

Jan 18th, 2021 (edited)
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.61 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5. clock = pygame.time.Clock()
  6.  
  7. sw = 800
  8. sh = 600
  9. screen = pygame.display.set_mode((sw,sh))
  10. pygame.display.set_caption("PING--PONG")
  11. bg_color = pygame.Color('grey12')
  12. game_font = pygame.font.Font('freesansbold.ttf', 60)
  13.  
  14. level = 1
  15. opponent_speed = 6
  16.  
  17. score_time = None
  18.  
  19. def StartGame(OS):
  20.     global score_time
  21.     sw = 800
  22.     sh = 600
  23.  
  24.     ball = pygame.Rect(sw//2 - 15, sh//2 - 15, 30, 30)
  25.     player = pygame.Rect(sw-20, sh//2 - 60, 10, 120)
  26.     opponent = pygame.Rect(10, sh//2 - 60, 10, 120)
  27.  
  28.     bg_color = pygame.Color('grey12')
  29.  
  30.     ball_speed_x = 6*random.choice((-1,1))
  31.     ball_speed_y = 6*random.choice((-1,1))
  32.  
  33.     player_speed = 0
  34.     opponent_speed = OS
  35.  
  36.     player_score = 0
  37.     opponent_score = 0
  38.  
  39.     game_font = pygame.font.Font('freesansbold.ttf', 32)
  40.  
  41.     score_time = None
  42.     def ball_restart():
  43.         global score_time
  44.         ball.center = (sw//2 + 7, sh//2)
  45.         #ball.x = sw//2 - 15
  46.         #ball.y = sh//2 - 15
  47.         current_time = pygame.time.get_ticks()
  48.         if current_time - score_time <= 2000:
  49.             ball_speed_x = 0
  50.             ball_speed_y = 0
  51.  
  52.         if current_time - score_time > 2000:
  53.             ball_speed_x = 6*random.choice((-1,1))
  54.             ball_speed_y = 6*random.choice((-1,1))
  55.             score_time = None
  56.  
  57.     running  = True
  58.     while running:
  59.         screen.fill(bg_color)
  60.         for event in pygame.event.get():
  61.             if event.type == pygame.QUIT:
  62.                 running = False
  63.  
  64.             if event.type == pygame.KEYDOWN:
  65.                 if event.key == pygame.K_UP: player_speed -= 7
  66.                 if event.key == pygame.K_DOWN: player_speed += 7
  67.  
  68.             if event.type == pygame.KEYUP:
  69.                 if event.key == pygame.K_UP: player_speed += 7
  70.                 if event.key == pygame.K_DOWN: player_speed -= 7
  71.        
  72.  
  73.         #SCORE
  74.         if ball.left <= 0:
  75.             score_time = pygame.time.get_ticks()
  76.             player_score += 1
  77.             ball_restart()
  78.  
  79.         if ball.right >= sw:
  80.             score_time = pygame.time.get_ticks()
  81.             opponent_score += 1
  82.             ball_restart()
  83.  
  84.         if score_time:
  85.             ball_restart()
  86.  
  87.         #COLLISION
  88.         if ball.colliderect(player) or ball.colliderect(opponent):
  89.             ball_speed_x *= -1
  90.  
  91.         #BALL MOVEMENT
  92.         ball.x += ball_speed_x
  93.         ball.y += ball_speed_y
  94.  
  95.         if ball.top <= 0 or ball.bottom >= sh:
  96.             ball_speed_y *= -1
  97.        
  98.         if ball.left <= 0 or ball.right >= sw:
  99.             ball_speed_x *= -1
  100.  
  101.         #PLAYER MOVEMENT
  102.         player.y += player_speed
  103.         if player.top <= 0:
  104.             player.top = 0
  105.         if player.bottom >= sh:
  106.             player.bottom = sh
  107.  
  108.         #OPPONENT MOVEMENT
  109.         if opponent.bottom < ball.y:
  110.             opponent.bottom += opponent_speed
  111.         if opponent.top > ball.y:
  112.             opponent.top -= opponent_speed
  113.  
  114.         pygame.draw.rect(screen, (200,200,200), player)
  115.         pygame.draw.rect(screen, (200,200,200), opponent)
  116.         pygame.draw.ellipse(screen, (200,200,200), ball)
  117.         pygame.draw.aaline(screen, (200,200,200), (sw//2,0), (sw//2,sh))
  118.  
  119.         player_text = game_font.render(str(player_score), True, (200,200,200))
  120.         screen.blit(player_text, (sw//2 + 20, sh//2 - 16))
  121.  
  122.         opponent_text = game_font.render(str(opponent_score), True, (200,200,200))
  123.         screen.blit(opponent_text, (sw//2 - 42, sh//2 - 16))
  124.  
  125.         clock.tick(30)
  126.         pygame.display.update()
  127.  
  128.  
  129. welcomscreen = True
  130. while welcomscreen:
  131.     screen.fill(bg_color)
  132.     for event in pygame.event.get():
  133.         if event.type == pygame.QUIT:
  134.             welcomscreen = False
  135.  
  136.         if event.type == pygame.KEYDOWN:
  137.             if event.key == pygame.K_SPACE:
  138.                 welcomscreen = False
  139.                 StartGame(opponent_speed)
  140.             if event.key == pygame.K_1:
  141.                 level = 1
  142.                 opponent_speed = 6
  143.  
  144.             elif event.key == pygame.K_2:
  145.                 level = 2
  146.                 opponent_speed = 10
  147.  
  148.             elif event.key == pygame.K_3:
  149.                 level = 3
  150.                 opponent_speed = 13
  151.  
  152.     #pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2, sh-140, 350, 70), 2)
  153.  
  154.     if level == 1:
  155.         pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2-190, sh-410, 350, 70), 2)
  156.     elif level == 2:
  157.         pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2-190, sh-310, 350, 70), 2)
  158.     elif level == 3:
  159.         pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2-190, sh-210, 350, 70), 2)
  160.  
  161.  
  162.     Welcome_message = game_font.render("PING PONG", True, (200,200,200))
  163.     screen.blit(Welcome_message, (sw//2 - 170, 20))
  164.  
  165.     Select_level = game_font.render("SELECT LEVEL", True, (200,200,200))
  166.     screen.blit(Select_level, (sw//2 - 200, sh-500))
  167.  
  168.     Easy = game_font.render("EASY", True, (200,200,200))
  169.     screen.blit(Easy, (sw//2 - 90, sh-400))
  170.  
  171.     Medium = game_font.render("MEDIUM", True, (200,200,200))
  172.     screen.blit(Medium, (sw//2-130, sh-300))
  173.  
  174.     Hard = game_font.render("HARD", True, (200,200,200))
  175.     screen.blit(Hard, (sw//2 - 90, sh-200))
  176.  
  177.     Start_game = game_font.render("PRESS SPACE TO START", True, (200,200,200))
  178.     screen.blit(Start_game, (sw//2-360, sh-100))
  179.  
  180.     clock.tick(30)
  181.     pygame.display.update()
  182.  
  183.  
  184. -------------------------------------------------------------------------------------------------
  185.  
  186. import pygame
  187. import random
  188.  
  189. clock = pygame.time.Clock()
  190.  
  191. black = (0,0,0)
  192. red = (255,0,0)
  193. white = (255,255,255)
  194.  
  195. class Block(pygame.sprite.Sprite):
  196.     def __init__(self, color, width, height):
  197.         super().__init__()
  198.  
  199.         self.image = pygame.Surface([width,height])
  200.         self.image.fill(color)
  201.  
  202.         self.rect = self.image.get_rect()
  203.  
  204. pygame.init()
  205.  
  206. sw = 700
  207. sh = 400
  208.  
  209. screen = pygame.display.set_mode((sw,sh))
  210.  
  211. block_list = pygame.sprite.Group()
  212. all_sprites_list = pygame.sprite.Group()
  213.  
  214. for i in range(10):
  215.     block = Block(black, 30, 30)
  216.  
  217.     block.rect.x = random.randrange(sw)
  218.     block.rect.y = random.randrange(sh)
  219.  
  220.     block_list.add(block)
  221.     all_sprites_list.add(block)
  222.  
  223.  
  224. player = Block(red, 30,30)
  225. all_sprites_list.add(player)
  226.  
  227. score = 0
  228.  
  229. running = True
  230. while running:
  231.     screen.fill(white)
  232.     for event in pygame.event.get():
  233.         if event.type == pygame.QUIT:
  234.             running = False
  235.  
  236.     pos = pygame.mouse.get_pos()
  237.     player.rect.x = pos[0]
  238.     player.rect.y = pos[1]
  239.    
  240.     blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
  241.  
  242.     for block in blocks_hit_list:
  243.         score += 1
  244.         print(score)
  245.  
  246.     all_sprites_list.draw(screen)
  247.  
  248.     clock.tick(20)
  249.     pygame.display.update()
  250.  
  251.  
  252.  
  253.  
  254. -----------------------------------------------------------------------------------------------
  255.  
  256.  
  257.  
  258. import pygame
  259. import random
  260.  
  261. clock = pygame.time.Clock()
  262.  
  263. pygame.init()
  264.  
  265. screen = pygame.display.set_mode((200,100))
  266. pygame.display.set_caption("TILE SMASHER")
  267.  
  268. game_font = pygame.font.Font('freesansbold.ttf', 60)
  269.  
  270. running = True
  271. while running:
  272.     screen.fill((200,200,200))
  273.     for event in pygame.event.get():
  274.         if event.type == pygame.QUIT:
  275.             running = False
  276.  
  277.         if event.type == pygame.MOUSEBUTTONDOWN:
  278.             if event.button == 1:     #LEFT
  279.                 print("LEFT CLICK")
  280.             elif event.button == 2:   #MIDDLE
  281.                 print("MIDDLE CLICK")
  282.             elif event.button == 3:   #RIGHT
  283.                 print("RIGHT")
  284.             elif event.button == 4:   #WHEEL UP
  285.                 print("WHEEL UP")
  286.             elif event.button == 5:   #WHEEL DOWN
  287.                 print("WHEEL DOWN")
  288.  
  289.     clock.tick(10)
  290.     pygame.display.update()
  291.  
  292.  
  293.  
  294. --------------------------------------------------------------------------------------------
  295.  
  296.  
  297. import pygame, random
  298. pygame.init()
  299.  
  300. clock = pygame.time.Clock()
  301.  
  302. sw = 800
  303. sh = 600
  304.  
  305. screen = pygame.display.set_mode((sw,sh))
  306. pygame.display.set_caption("TILES")
  307. game_font = pygame.font.Font('freesansbold.ttf', 60)
  308.  
  309. xy = [100,100]
  310. score = 0
  311.  
  312. font = pygame.font.Font('freesansbold.ttf', 32)
  313. sCoord = (10,10)
  314. def score_print(sc):
  315.     screen.blit(font.render("SCORE: " + str(sc), True, (0,0,0)), sCoord)
  316.  
  317. def generate_box(x,y):
  318.     return(pygame.Rect(x,y,100,100))
  319.  
  320. def isClicked(cxy, cmx, cmy):
  321.     if cxy[0] < cmx < cxy[0] + 100 and cxy[1] < cmy < cxy[1] + 100:
  322.         return True
  323.     return False
  324.  
  325. start_time = pygame.time.get_ticks()
  326. clicked = False
  327. running = True
  328. while running:
  329.     screen.fill((200,200,200))
  330.     for event in pygame.event.get():
  331.         if event.type == pygame.QUIT:
  332.             running = False
  333.  
  334.         if event.type == pygame.MOUSEBUTTONDOWN:
  335.             if event.button == 1:
  336.                 clicked = True
  337.  
  338.         if event.type == pygame.MOUSEBUTTONUP:
  339.             if event.button == 1:
  340.                 clicked = False
  341.  
  342.     #RANDOM SPAWNING CODE
  343.     current_time = pygame.time.get_ticks()
  344.     if current_time - start_time > 2000:
  345.         start_time = pygame.time.get_ticks()
  346.         xy = [random.randint(50,700), random.randint(50,500)]
  347.  
  348.     #DETECTION
  349.     mx, my = pygame.mouse.get_pos()
  350.     if clicked:
  351.         if (current_time - start_time < 2000) and isClicked(xy, mx, my):
  352.             start_time = pygame.time.get_ticks()
  353.             xy = [random.randint(50,700), random.randint(50,500)]
  354.             score += 1
  355.             print(score)
  356.  
  357.     box = generate_box(xy[0], xy[1])
  358.     pygame.draw.rect(screen, (255,0,0), box)
  359.  
  360.     score_print(score)
  361.  
  362.     clock.tick(20)
  363.     pygame.display.update()
  364.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement