import pygame
import random
display_w = 800
display_h = 600
display = pygame.display.set_mode((display_w, display_h))
pygame.display.set_caption('Game')
pers_width = 60
pers_height = 100
make_jump = False
pers_x = display_w // 4
pers_y = display_h - 100 - pers_height
dy = 18
enemy_width = 25
enemy_height = 70
enemy_x = display_w - 50
enemy_y = display_h - enemy_height - 100
dx = 20
check = 0
class Enemy:
def __init__(self, x, y, width, height, speed):
self.x = x
self.y = y
self.width = width
self.height = height
self.speed = speed
def move(self):
if self.x >= -self.width:
pygame.draw.rect(display, (150,20,200), (self.x,self.y,self.width, self.height))
self.x -= self.speed
return True
else:
return False
def return_self(self, radius):
self.x = radius
def draw_enemies(enemies):
for x in enemies:
check = x.move()
if not check:
radius = find_radius(enemies)
x.return_self(radius)
def create_enemies(enemies):
enemies.append(Enemy(display_w + 50, display_h - 170, 20, 70, 4))
enemies.append(Enemy(display_w - 500, display_h -160, 30, 60, 4))
enemies.append(Enemy(display_w - 200, display_h -180, 25, 80, 4))
def find_radius(enemies):
maximum = max(enemies[0].x, enemies[1].x, enemies[2].x)
choice = random.randrange(0, 5)
if maximum < display_w:
radius = display_w
if radius - maximum < 50:
radius += 150
else:
radius = maximum
if choice == 0:
radius += random.randrange(10, 15)
else:
radius += random.randrange(200, 350)
return radius
def jump():
global pers_y, make_jump, dy
if dy >= -18:
pers_y -= dy
dy -= 1
else:
dy = 18
make_jump = False
if make_jump:
jump()
def rungame():
global make_jump
game = True
enemies = []
create_enemies(enemies)
while game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
make_jump = True
if make_jump:
jump()
display.fill((238, 238, 238))
draw_enemies(enemies)
pygame.draw.rect(display, (251,186,0),(pers_x, pers_y, pers_width, pers_height))
pygame.display.update()
pygame.time.Clock().tick(80)
rungame()
pygame.init()