Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import time
- from random import randrange, choice
- pygame.init()
- display_width = 800
- display_height = 600
- black = (0, 0, 0)
- white = (255, 255, 255)
- red = (255, 0, 0)
- block_color = (53, 115, 255)
- car_wid = 72
- gameDisplay = pygame.display.set_mode((display_width, display_height))
- pygame.display.set_caption('Joguinho atacado')
- clock = pygame.time.Clock()
- carImg = pygame.image.load('racecar.png')
- def count_text(count, text='Desviadas: {}', xy=(0, 10), color=black):
- font = pygame.font.SysFont(None, 25)
- text = font.render(text.format(count), True, color)
- gameDisplay.blit(text, xy)
- def enemy(ex, ey, ew, eh, color):
- pygame.draw.rect(gameDisplay, color, [ex, ey, ew, eh])
- def load_car(x, y):
- gameDisplay.blit(carImg, (x, y))
- def text_obj(text, font):
- surface = font.render(text, True, black)
- return surface, surface.get_rect()
- def message_display(text):
- large_text = pygame.font.Font('freesansbold.ttf', 115)
- surface, rect = text_obj(text, large_text)
- rect.center = ((display_width / 2), (display_height / 2))
- gameDisplay.blit(surface, rect)
- pygame.display.update()
- time.sleep(2)
- game_loop()
- def crash():
- message_display('You Crashed')
- def game_loop():
- global fx
- x = (display_width * 0.45)
- y = (display_height * 0.8)
- x_change = 0
- enemy_y = -600
- enemy_speed = 6.3
- enemy_wid = 100
- enemy_hei = 100
- enemy_x = randrange(0, display_width)
- dodged = 0
- gameexit = False
- lives = 1
- i = 0
- frut = False
- while not gameexit:
- i += 1
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT or event.key == pygame.K_a:
- x_change = -6.3
- if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
- x_change = 6.3
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_d or\
- event.key == pygame.K_a:
- x_change = 0
- x += x_change
- gameDisplay.fill(white)
- # things(thingx, thingy, thingw, thingh, color)
- enemy(enemy_x, enemy_y, enemy_wid, enemy_hei, block_color)
- posl = choice([False, False, False, True, choice([False, False, True]), False, False, False, False])
- if posl and ((str(i)[::-1][0]) == '0') and not frut and dodged % 2 == 0:
- fx = randrange(0, display_width - 20)
- enemy(fx, y+20, 20, 20, red)
- frut = True
- if frut:
- enemy(fx, y+20, 20, 20, red)
- if (fx < x < fx + 20) or (fx < x + car_wid < fx + 20):
- frut = False
- lives += 1
- enemy_y += enemy_speed
- load_car(x, y)
- count_text(dodged)
- count_text(lives, text='Vidas: {}', xy=(display_width-80, 10))
- if x > display_width - car_wid or x < 0 or lives <= 0:
- crash()
- if enemy_y > display_height:
- enemy_y = 0 - enemy_hei
- enemy_x = randrange(0, display_width - 20)
- dodged += 1
- enemy_speed += 2
- enemy_wid += (dodged * 1.6)
- if (y < enemy_y + enemy_hei) and (enemy_x < x < enemy_x + enemy_wid or
- enemy_x < x + car_wid < enemy_x + enemy_wid):
- lives -= 1
- enemy_y = 0 - enemy_hei
- enemy_x = randrange(0, display_width - 20)
- dodged += 1
- enemy_speed += 1.3
- pygame.display.update()
- clock.tick(60) # 60 fps ;-)
- game_loop()
- pygame.quit()
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement