rishabbansal21

Day-8

Jan 15th, 2021 (edited)
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.92 KB | None | 0 0
  1. import pygame
  2. import random
  3. pygame.init()
  4.  
  5. clock = pygame.time.Clock()
  6.  
  7. screen = pygame.display.set_mode((800,600))
  8. pygame.display.set_caption("FLAPPY BIRDS")
  9.  
  10. #BIRD
  11. x = 200
  12. y = 300
  13. jump = 0
  14. speed  = 0.5
  15. def draw_circle(x,y):
  16.     pygame.draw.circle(screen, (255,0,0), (x,y), 30)
  17.  
  18.  
  19. #PIPES
  20. pipe1 = [800, 0, 50, random.randint(20,250)]
  21. pipe2 = [400, 0, 50, random.randint(50,250)]
  22.  
  23. Pipes = []
  24. Pipes.append(pipe1)
  25. Pipes.append(pipe2)
  26.  
  27. def draw_pipes(PIPE):
  28.     #left, top, width, heihgt
  29.  
  30.     #TOP
  31.     pygame.draw.rect(screen, (0,255,0), (PIPE[0], PIPE[1], PIPE[2], PIPE[3]))
  32.  
  33.     #BOTTOM
  34.     pygame.draw.rect(screen, (0,255,0), (PIPE[0], 150+PIPE[3], PIPE[2], PIPE[3]+400))
  35.  
  36. score = 0
  37.  
  38. running = True
  39.  
  40. while running:
  41.     screen.fill((120,120,255))
  42.  
  43.     for event in pygame.event.get():
  44.         if event.type == pygame.QUIT:
  45.             running = False
  46.  
  47.         if event.type == pygame.KEYDOWN:
  48.             if event.key == pygame.K_SPACE: jump = 1
  49.  
  50.         if event.type == pygame.KEYUP:
  51.             if event.key == pygame.K_SPACE: jump = 0
  52.  
  53.     #BIRD MOVEMENT
  54.     draw_circle(x,y)
  55.     if jump == 1:
  56.         y -= 3
  57.     else:
  58.         y += speed
  59.  
  60.     #PIPE MOVEMENT
  61.     for i in Pipes:
  62.         draw_pipes(i)
  63.         i[0] -= 3
  64.         if i[0] < 0:
  65.             i[0] = 800
  66.             i[3] = random.randint(50,250)
  67.  
  68.     #GAME OVER AND SCORE
  69.     for i in Pipes:
  70.         if i[0] == 200:
  71.             if y <= i[3] or y >= 150+i[3]:
  72.                 print("GAME OVER")
  73.                 running = False
  74.             elif i[3] < y < 150+i[3]:
  75.                 score += 1
  76.                 print(score)
  77.  
  78.     clock.tick(30)
  79.     pygame.display.update()
  80.  
  81.  
  82.  
  83. ------------------------------------------------------------------------------------------------------
  84.  
  85. import pygame
  86. import random
  87.  
  88. pygame.init()
  89.  
  90. screen = pygame.display.set_mode((288,512))
  91. pygame.display.set_caption("FLAPPY BIRDS")
  92.  
  93. background = pygame.image.load('imgs/background.png')
  94. base = pygame.image.load('imgs/base.png')
  95.  
  96. #BIRD
  97. x = 100
  98. y = 300
  99. jump = 0
  100. speed = 2
  101. birdimg = pygame.image.load('imgs/bird.png')
  102. def draw_bird(x,y):
  103.     screen.blit(birdimg, (x,y))
  104.  
  105.  
  106. #PIPES
  107. pipeupimg = pygame.image.load('imgs/pipe-up.png')
  108. pipedownimg = pygame.image.load('imgs/pipe-down.png')
  109. pipe1 = [300, -170]
  110. pipe2 = [550, -100]
  111. Pipes = []
  112. Pipes.append(pipe1)
  113. Pipes.append(pipe2)
  114. def draw_pipe(PIPE):
  115.     screen.blit(pipeupimg, (PIPE[0], PIPE[1]))
  116.     screen.blit(pipedownimg, (PIPE[0], PIPE[1]+420))
  117.  
  118. #SCORE
  119. score = 0
  120. font = pygame.font.Font('freesansbold.ttf', 32)
  121. sCoord = (10,10)
  122. def print_score(sc):
  123.     screen.blit(font.render("SCORE: " + str(sc), True, (255,255,255)), sCoord)
  124.  
  125.  
  126. running = True
  127. while running:
  128.     screen.blit(background, (0,0)) #UPPER PART
  129.     #screen.blit(base, (0,410))
  130.  
  131.     for event in pygame.event.get():
  132.         if event.type == pygame.QUIT:
  133.             running = False
  134.  
  135.         if event.type == pygame.KEYDOWN:
  136.             if event.key == pygame.K_SPACE: jump = 1
  137.  
  138.         if event.type == pygame.KEYUP:
  139.             if event.key == pygame.K_SPACE: jump = 0
  140.  
  141.     #GAME OVER AND SCORE
  142.     for i in Pipes:
  143.         if i[0] == 100:
  144.             if y < i[1] + 320 or y > i[1]+420:
  145.                 print("GAME OVER")
  146.                 running  = False
  147.  
  148.             elif i[0]+320 < y < i[1]+420:
  149.                 score += 1
  150.                 print(score)
  151.  
  152.     #BIRD MOVEMENT
  153.     draw_bird(x,y)
  154.     if jump == 1:
  155.         y -= 2
  156.     else:
  157.         y += speed
  158.  
  159.     #PIPE MOVEMENT
  160.     #draw_pipe(Pipes[0])
  161.     #draw_pipe(Pipes[1])
  162.     for i in Pipes:
  163.         draw_pipe(i)
  164.         i[0] -= 0.5
  165.         if i[0] < 0:
  166.             i[0] = 500
  167.             i[1] = random.randint(-250,-100)
  168.  
  169.     screen.blit(base, (0,410)) #LOWER PART
  170.     print_score(score)
  171.     pygame.display.update()
  172.  
  173.  
  174.  
  175.  
  176. ------------------------------------------------------------------------------------------------------
  177.  
  178.  
  179.  
  180.  
  181. import pygame
  182. import random
  183.  
  184. pygame.init()
  185.  
  186. sw = 800
  187. sh = 600
  188. screen = pygame.display.set_mode((sw,sh))
  189. pygame.display.set_caption("PING - PONG")
  190.  
  191.  
  192. def bluescreen():
  193.     bluescreenRunning = True
  194.     while bluescreenRunning:
  195.         screen.fill((0,0,255))
  196.         for event in pygame.event.get():
  197.             if event.type == pygame.QUIT:
  198.                 bluescreenRunning = False
  199.  
  200.         pygame.display.update()
  201.  
  202.  
  203.  
  204. running  = True
  205. while running:
  206.     screen.fill((255,0,0))
  207.     for event in pygame.event.get():
  208.         if event.type == pygame.QUIT:
  209.             running = False
  210.  
  211.         if event.type == pygame.KEYDOWN:
  212.             if event.key == pygame.K_SPACE:
  213.                 running = False
  214.                 bluescreen()
  215.  
  216.     pygame.display.update()
  217.  
  218.  
  219.  
  220. -----------------------------------------------------------------------------------------------------
  221.  
  222. import pygame
  223. import random
  224.  
  225. clock = pygame.time.Clock()
  226. pygame.init()
  227.  
  228. sw = 800
  229. sh = 600
  230.  
  231. screen = pygame.display.set_mode((sw,sh))
  232. pygame.display.set_caption("PING - PONG")
  233.  
  234. ball = pygame.Rect(sw//2 - 15, sh//2 - 15, 30, 30)
  235. player = pygame.Rect(sw-20, sh//2 - 60, 10, 120)
  236. opponent = pygame.Rect(10, sh//2 - 60, 10, 120)
  237.  
  238. bg_color = pygame.Color('grey12')
  239.  
  240. ball_speed_x = 6*random.choice((-1,1))
  241. ball_speed_y = 6*random.choice((-1,1))
  242.  
  243. player_speed = 0
  244. opponent_speed = 6
  245.  
  246. player_score = 0
  247. opponent_score = 0
  248.  
  249. game_font = pygame.font.Font('freesansbold.ttf', 32)
  250.  
  251. score_time = None
  252. def ball_restart():
  253.     global score_time
  254.     ball.center = (sw//2 + 7, sh//2)
  255.     #ball.x = sw//2 - 15
  256.     #ball.y = sh//2 - 15
  257.     current_time = pygame.time.get_ticks()
  258.     if current_time - score_time <= 2000:
  259.         ball_speed_x = 0
  260.         ball_speed_y = 0
  261.  
  262.     if current_time - score_time > 2000:
  263.         ball_speed_x = 6*random.choice((-1,1))
  264.         ball_speed_y = 6*random.choice((-1,1))
  265.         score_time = None
  266.  
  267.  
  268. running  = True
  269. while running:
  270.     screen.fill(bg_color)
  271.     for event in pygame.event.get():
  272.         if event.type == pygame.QUIT:
  273.             running = False
  274.  
  275.         if event.type == pygame.KEYDOWN:
  276.             if event.key == pygame.K_UP: player_speed -= 7
  277.             if event.key == pygame.K_DOWN: player_speed += 7
  278.  
  279.         if event.type == pygame.KEYUP:
  280.             if event.key == pygame.K_UP: player_speed += 7
  281.             if event.key == pygame.K_DOWN: player_speed -= 7
  282.    
  283.  
  284.     #SCORE
  285.     if ball.left <= 0:
  286.         score_time = pygame.time.get_ticks()
  287.         player_score += 1
  288.         ball_restart()
  289.  
  290.     if ball.right >= sw:
  291.         score_time = pygame.time.get_ticks()
  292.         opponent_score += 1
  293.         ball_restart()
  294.  
  295.     if score_time:
  296.         ball_restart()
  297.  
  298.     #COLLISION
  299.     if ball.colliderect(player) or ball.colliderect(opponent):
  300.         ball_speed_x *= -1
  301.  
  302.     #BALL MOVEMENT
  303.     ball.x += ball_speed_x
  304.     ball.y += ball_speed_y
  305.  
  306.     if ball.top <= 0 or ball.bottom >= sh:
  307.         ball_speed_y *= -1
  308.    
  309.     if ball.left <= 0 or ball.right >= sw:
  310.         ball_speed_x *= -1
  311.  
  312.     #PLAYER MOVEMENT
  313.     player.y += player_speed
  314.     if player.top <= 0:
  315.         player.top = 0
  316.     if player.bottom >= sh:
  317.         player.bottom = sh
  318.  
  319.     #OPPONENT MOVEMENT
  320.     if opponent.bottom < ball.y:
  321.         opponent.bottom += opponent_speed
  322.     if opponent.top > ball.y:
  323.         opponent.top -= opponent_speed
  324.  
  325.     pygame.draw.rect(screen, (200,200,200), player)
  326.     pygame.draw.rect(screen, (200,200,200), opponent)
  327.     pygame.draw.ellipse(screen, (200,200,200), ball)
  328.     pygame.draw.aaline(screen, (200,200,200), (sw//2,0), (sw//2,sh))
  329.  
  330.     player_text = game_font.render(str(player_score), True, (200,200,200))
  331.     screen.blit(player_text, (sw//2 + 20, sh//2 - 16))
  332.  
  333.     opponent_text = game_font.render(str(opponent_score), True, (200,200,200))
  334.     screen.blit(opponent_text, (sw//2 - 42, sh//2 - 16))
  335.  
  336.     clock.tick(30)
  337.     pygame.display.update()
  338.  
  339.  
  340. ------------------------------------------------------------------------------------------------------
  341.  
Add Comment
Please, Sign In to add comment