import pygame
import random
import os
import sys
pygame.init()
screen = pygame.display.set_mode((700, 600))
done = False
clock = pygame.time.Clock()
# Spaceship and Enemy UFO
ship = pygame.image.load("d:/CrashCourse/dodger/ship.png").convert_alpha()
ship = pygame.transform.scale(ship, (40, 40))
Вот начальное местоположение корабля
ship_location = [500, 500]
size_ufo = [20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
ufo = pygame.image.load("d:/CrashCourse/dodger/ufo.png").convert_alpha()
random_size = random.choice(size_ufo)
ufo = pygame.transform.scale(ufo, (random_size, random_size))
ufo_top = 50
ufo_left = 50
Они появляются случайно
x_change = 0
ufo_locationx = random.randrange(0, 700)
ufo_locationy = -600
ufo_speed = 2.5
Это часть столкновения, я не знаю, полезна ли она для AI
def collision(x1, y1, w1, h1, x2, y2, w2, h2):
if (x2 + w2>=x1>=x2 and y2+h2>=y1>=y2):
return True
elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1>=y2):
return True
elif (x2 + w2>=x1>=x2 and y2+h2>=y1+h1>=y2):
return True
elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2):
return True
else:
return False
while not done:
screen.fill([23, 46, 75])
screen.blit(ship, (ship_location[0], ship_location[1]))
screen.blit(ufo, (ufo_locationx, ufo_locationy))
ufo_locationy += ufo_speed
if ufo_locationy > 600:
ufo_locationy = 0 - random_size
ufo_locationx = random.randrange(0, 700)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
ship_location[0] += x_change
if collision(ship_location[0], ship_location[1], 40, 40, ufo_locationx, ufo_locationy, random_size-23, random_size-23) == True:
pygame.quit()
if ship_location[0] > 663 or ship_location[0] < 0:
x_change = -x_change
pygame.display.update()
pygame.display.flip()
clock.tick(60)
pygame.quit()