Avatar r50 a6ce93fe35b158fd29ba0e8681c918c22117160e9586a56eee4ffbc20df9bda1
Программирование

Питон игра

Добавлено 30 апр 2022 в 17:15
import random
import pygame
import time
pygame.init()
# Создание окна

width_window = 800
height_window = 800
screen = pygame.display.set_mode((width_window, height_window))
pygame.display.set_caption('Гонки за джуси бабкой')
# Контроль FPS
clock = pygame.time.Clock()
FPS = 60
running = True
go = True
p = True


def message(msg):
f2 = pygame.font.Font(None, 100)
msg = f2.render(f'{msg}', True,
(255, 0, 0))
screen.blit(msg, (200, 400))


f1 = pygame.font.Font(None, 50)
text1 = f1.render(f'Очки: {0}', True,
(255, 255, 255))
screen.blit(text1, (200, 400))
sss = 0


def es(go):
while go:
for event in pygame.event.get():
if event.type == pygame.QUIT:
go = False
screen.fill((255, 255, 255))
message("Ты выиграл!")
pygame.display.flip()
pygame.quit()


def pa(p):
while p:
for event in pygame.event.get():
if event.type == pygame.QUIT:
p = False
screen.fill((255, 255, 255))
f2 = pygame.font.Font(None, 100)
msg = f2.render("Хах, нашел пасхалку", True,
(0, 200, 0))
screen.blit(msg, (60, 400))
pygame.display.flip()
pygame.quit()


class Main:
def __init__(self, y, y1, speed, bg, x_car, speed_car, car, y_gold, x_gold, gold):
self.speed = speed
self.bg = pygame.image.load(bg)
self.y = y
self.y1 = y1
self.x_car = x_car
self.speed_car = speed_car
self.car = pygame.image.load(car)
self.y_gold = y_gold
self.x_gold = x_gold
self.gold = pygame.image.load(gold)
self.count = 0
self.f1 = pygame.font.Font(None, 50)
self.text1 = self.f1.render(
f'Очки: {self.count}', True, (255, 255, 255))

# road

def move_up(self):
self.y += self.speed
self.y1 += self.speed
if self.y >= 800:
self.y = -800
if self.y1 >= 800:
self.y1 = -800

def show_bg(self):
screen.blit(self.bg, (0, self.y))
screen.blit(self.bg, (0, self.y1))

# car

def control(self):
if self.x_car < 155 - 88 or self.x_car > 672:
self.speed = 3
elif self.x_car < 155 - 44 or self.x_car > 682 - 60:
self.speed = 5
else:
self.speed = 10

def move_left(self):
if not self.x_car <= 5:
self.x_car -= self.speed_car

def move_right(self):
if not self.x_car >= 800 - 90:
self.x_car += self.speed_car

def show_car(self):
screen.blit(self.car, (self.x_car, 480))

# gold
def move_gold(self):
self.y_gold += self.speed
if self.y_gold >= 800:
self.y_gold = -800

def show_gold(self):
screen.blit(self.gold, (self.x_gold, self.y_gold))

def catch(self):
if self.x_gold - 50 <= self.x_car <= self.x_gold + 50 and self.y_gold - 60 <= 480 <= self.y_gold + 60:
self.f1 = pygame.font.Font(None, 50)
self.text1 = self.f1.render(
f'Очки: {self.count + 1}', True, (255, 255, 255))
self.count += 1
screen.blit(self.text1, (160, 50))
self.x_gold = random.randint(100, 500)
self.y_gold -= 700
screen.blit(self.gold, (self.x_gold, self.y_gold))

def show_txt(self):
screen.blit(self.text1, (160, 50))

def win(self):
if self.count == 15:
return True


ground = Main(0, -800, 10, 'bg.jpg', 375, 5, 'car.png', -
500, random.randint(100, 500), 'gold.png')
up = False
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
up = True
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
sss += 1
if up:
ground.move_up()
ground.move_gold()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
ground.move_left()
ground.control()
if keys[pygame.K_RIGHT]:
ground.move_right()
ground.control()
if sss == 20:
pa(p)
if ground.win():
running = False
es(go)
ground.catch()
ground.show_bg()
ground.show_gold()
ground.show_txt()
ground.show_car()
pygame.display.flip()
pygame.quit()
65a877971d