Advertisement
Guest User

corridinha

a guest
Jan 21st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 KB | None | 0 0
  1. import pygame
  2. import time
  3. from random import randrange, choice
  4.  
  5. pygame.init()
  6.  
  7. display_width = 800
  8. display_height = 600
  9.  
  10. black = (0, 0, 0)
  11. white = (255, 255, 255)
  12. red = (255, 0, 0)
  13.  
  14. block_color = (53, 115, 255)
  15.  
  16. car_wid = 72
  17.  
  18. gameDisplay = pygame.display.set_mode((display_width, display_height))
  19. pygame.display.set_caption('Joguinho atacado')
  20. clock = pygame.time.Clock()
  21.  
  22. carImg = pygame.image.load('racecar.png')
  23.  
  24.  
  25. def count_text(count, text='Desviadas: {}', xy=(0, 10), color=black):
  26.     font = pygame.font.SysFont(None, 25)
  27.     text = font.render(text.format(count), True, color)
  28.     gameDisplay.blit(text, xy)
  29.  
  30.  
  31. def enemy(ex, ey, ew, eh, color):
  32.     pygame.draw.rect(gameDisplay, color, [ex, ey, ew, eh])
  33.  
  34.  
  35. def load_car(x, y):
  36.     gameDisplay.blit(carImg, (x, y))
  37.  
  38.  
  39. def text_obj(text, font):
  40.     surface = font.render(text, True, black)
  41.     return surface, surface.get_rect()
  42.  
  43.  
  44. def message_display(text):
  45.     large_text = pygame.font.Font('freesansbold.ttf', 115)
  46.     surface, rect = text_obj(text, large_text)
  47.     rect.center = ((display_width / 2), (display_height / 2))
  48.     gameDisplay.blit(surface, rect)
  49.  
  50.     pygame.display.update()
  51.  
  52.     time.sleep(2)
  53.  
  54.     game_loop()
  55.  
  56.  
  57. def crash():
  58.     message_display('You Crashed')
  59.  
  60.  
  61. def game_loop():
  62.     global fx
  63.     x = (display_width * 0.45)
  64.  
  65.     y = (display_height * 0.8)
  66.  
  67.     x_change = 0
  68.  
  69.     enemy_y = -600
  70.     enemy_speed = 6.3
  71.     enemy_wid = 100
  72.     enemy_hei = 100
  73.     enemy_x = randrange(0, display_width)
  74.  
  75.     dodged = 0
  76.  
  77.     gameexit = False
  78.     lives = 1
  79.     i = 0
  80.     frut = False
  81.     while not gameexit:
  82.         i += 1
  83.         for event in pygame.event.get():
  84.             if event.type == pygame.QUIT:
  85.                 pygame.quit()
  86.                 quit()
  87.  
  88.             if event.type == pygame.KEYDOWN:
  89.                 if event.key == pygame.K_LEFT or event.key == pygame.K_a:
  90.                     x_change = -6.3
  91.                 if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
  92.                     x_change = 6.3
  93.  
  94.             if event.type == pygame.KEYUP:
  95.                 if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_d or\
  96.                         event.key == pygame.K_a:
  97.                     x_change = 0
  98.  
  99.         x += x_change
  100.         gameDisplay.fill(white)
  101.  
  102.         # things(thingx, thingy, thingw, thingh, color)
  103.         enemy(enemy_x, enemy_y, enemy_wid, enemy_hei, block_color)
  104.         posl = choice([False, False, False, True, choice([False, False, True]), False, False, False, False])
  105.         if posl and ((str(i)[::-1][0]) == '0') and not frut and dodged % 2 == 0:
  106.  
  107.             fx = randrange(0, display_width - 20)
  108.             enemy(fx, y+20, 20, 20, red)
  109.             frut = True
  110.         if frut:
  111.  
  112.             enemy(fx, y+20, 20, 20, red)
  113.             if (fx < x < fx + 20) or (fx < x + car_wid < fx + 20):
  114.                 frut = False
  115.                 lives += 1
  116.         enemy_y += enemy_speed
  117.         load_car(x, y)
  118.         count_text(dodged)
  119.         count_text(lives, text='Vidas: {}', xy=(display_width-80, 10))
  120.         if x > display_width - car_wid or x < 0 or lives <= 0:
  121.             crash()
  122.  
  123.         if enemy_y > display_height:
  124.             enemy_y = 0 - enemy_hei
  125.             enemy_x = randrange(0, display_width - 20)
  126.             dodged += 1
  127.             enemy_speed += 2
  128.             enemy_wid += (dodged * 1.6)
  129.  
  130.         if (y < enemy_y + enemy_hei) and (enemy_x < x < enemy_x + enemy_wid or
  131.                                           enemy_x < x + car_wid < enemy_x + enemy_wid):
  132.             lives -= 1
  133.             enemy_y = 0 - enemy_hei
  134.             enemy_x = randrange(0, display_width - 20)
  135.             dodged += 1
  136.             enemy_speed += 1.3
  137.  
  138.         pygame.display.update()
  139.         clock.tick(60)  # 60 fps ;-)
  140.  
  141.  
  142. game_loop()
  143. pygame.quit()
  144. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement