1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| import pygame
import random
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (159, 163, 168)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
CAR_COLOR = (181, 230, 29)
pygame.init()
class Car:
def __init__(self, x=0, y=0, dx=4, dy=0, width=30, height=30, color=RED):
self.image = ""
self.x = x
self.y = y
self. dx = dx
self.dy = dy
self.width = width
self.height = height
self.color = color
def load_image(self, img):
self.image = pygame.image.load('player.png').convert()
self.image.set_colorkey(GREEN)
def draw_image(self):
screen.blit(self.image, [self.x, self.y])
def move_x(self):
self.x += self.dx
def move_y(self):
self.y += self.dy
def draw_rect(self):
pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height], 0)
def check_out_of_screen(self):
if self.x+self.width > 400 or self.x < 0:
self.x -= self.dx
def check_collision(player_x, player_y, player_width, player_height, car_x, car_y, car_width, car_height):
if (player_x+player_width > car_x) and (player_x < car_x+car_width) and (player_y < car_y+car_height) and (player_y+player_height > car_y):
return True
else:
return False
size = (400, 700)
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
player = Car(175, 475, 0, 0, 70, 131, RED)
player.load_image("player.png")
collision = True
score = 0
font_40 = pygame.font.SysFont("Arial", 40, True, False)
font_30 = pygame.font.SysFont("Arial", 30, True, False)
text_title = font_40.render(" Car Chare", True, RED)
text_ins = font_30.render("Click to Play!", True, RED)
def draw_main_menu():
screen.blit(text_title, [size[0] / 2 - 106, size[1] / 2 - 100])
score_text = font_40.render("Score: " + str(score), True, RED)
screen.blit(score_text, [size[0] / 2 - 70, size[1] / 2 - 30])
screen.blit(text_ins, [size[0] / 2 - 85, size[1] / 2 + 40])
pygame.display.flip()
cars = []
car_count = 2
for i in range(car_count):
x = random.randrange(0, 340)
car = Car(x, random.randrange(-150, -50), 0, random.randint(5, 10), 60, 60, CAR_COLOR)
cars.append(car)
stripes = []
stripe_count = 20
stripe_x = 185
stripe_y = -10
stripe_width = 20
stripe_height = 80
space = 20
for i in range(stripe_count):
stripes.append([190, stripe_y])
stripe_y += stripe_height + space
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if collision and event.type == pygame.MOUSEBUTTONDOWN:
collision = False
for i in range(car_count):
cars[i].y = random.randrange(-150, -50)
cars[i].x = random.randrange(0, 350)
player.x = 175
player.dx = 0
score = 0
pygame.mouse.set_visible(False)
if not collision:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
player.dx = 4
elif event.key == pygame.K_LEFT:
player.dx = -4
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.dx = 0
elif event.key == pygame.K_RIGHT:
player.dx = 0
screen.fill(BLACK)
if not collision:
for i in range(stripe_count):
pygame.draw.rect(screen, WHITE, [stripes[i][0], stripes[i][1], stripe_width, stripe_height])
for i in range(stripe_count):
stripes[i][1] += 12
if stripes[i][1] > size[1]:
stripes[i][1] = -10000 - stripe_height
player.draw_image()
player.move_x()
player.check_out_of_screen()
for i in range(car_count):
cars[i].draw_rect()
cars[i].y += cars[i].dy
if cars[i].y > size[1]:
score += 10
cars[i].y = random.randrange(-150, -50)
cars[i].x = random.randrange(0, 340)
cars[i].dy = random.randint(4, 9)
for i in range(car_count):
if check_collision(player.x, player.y, player.width, player.height, cars[i].x, cars[i].y, cars[i].width, cars[i].height):
collision = True
pygame.mouse.set_visible(True)
break
txt_score = font_30.render("mètres: "+str(score), True, WHITE)
screen.blit(txt_score, [15, 15])
pygame.display.flip()
else:
draw_main_menu()
clock.tick(60)
pygame.quit() |
Partager