Код:
import pygame
from random import randint
Move_Speed = 1
def load_image(name):
fullname = 'data'+'/'+name
try:
image = pygame.image.load(fullname).convert()
except:
print('Cannot load image:', name)
raise SystemExit()
return image
class Shell(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = img_c
self.rect = pygame.Rect(x, y, WIDTH_S, HEIGHT_S)
self.rect.x = x
self.rect.y = y
self.speed_x = 10
self.speed_y = 10
self.Strike = False
self.add(sprites)
def teleport_xy(self, strike, Players):
if strike == True:
self.Strike = True
for k in Players:
self.rect.x = k.rect.x + 10
self.rect.y = k.rect.y - 40
self.collide(platforms)
def update(self):
if self.Strike == True:
self.Strike = False
self.rect.x = 1000
self.rect.y = 1000
def strike(self):
self.rect.y -= self.speed_y
self.collide(platforms)
if self.rect.y < 0:
self.update()
def collide(self, platforms):
for p in platforms:
if pygame.sprite.collide_rect(self, p):
if self.speed_y > 0: #Вверх
self.speed_y = 0
self.update()
if self.speed_y < 0: #Вниз
self.speed_y = 0
self.update()
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = img_a
self.rect = pygame.Rect(x, y, WIDTH_P, HEIGHT_P)
self.rect.x = x
self.rect.y = y
self.xvel = 0
self.yvel = 0
self.add(sprites)
def update(self, left, right, up, down):
if left == True:
self.xvel = -Move_Speed
if right == True:
self.xvel = Move_Speed
if up == True:
self.yvel = -Move_Speed
if down == True:
self.yvel = Move_Speed
if not(left or right):
self.xvel = 0 #стоим когда нет указаний
if not(up or down):
self.yvel = 0
self.rect.x += self.xvel
self.collide(self.xvel, 0, platforms)
self.rect.y += self.yvel
self.collide(0, self.yvel, platforms)
def collide(self, xvel, yvel, platforms):
for p in platforms:
if pygame.sprite.collide_rect(self, p): # если есть пересечение платформы с игроком
if xvel > 0: # если движется вправо
self.rect.right = p.rect.left # то не движется вправо
if xvel < 0: # если движется влево
self.rect.left = p.rect.right # то не движется влево
if yvel > 0: # если движется вниз
self.rect.bottom = p.rect.top # то не движется вниз
if yvel < 0: # если движется вверх
self.rect.top = p.rect.bottom # то не движется вверх
class Enemy(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = img_b
self.rect = pygame.Rect(x, y, WIDTH_E, HEIGHT_E)
self.rect.x = x
self.rect.y = y
self.xvel = 0
self.yvel = 0
self.add(sprites)
pygame.init()
#Константы
sprites = pygame.sprite.Group()
not_all_sprites = pygame.sprite.Group()
win_height = 800
win_widht = 800
WIDTH_P = 40
HEIGHT_P = 40 #Размеры Игрока
WIDTH_S = 20
HEIGHT_S = 40
WIDTH_E = 40
HEIGHT_E = 40
window = pygame.display.set_mode((win_widht, win_height))
screen = pygame.Surface((win_widht, win_height))
zet = pygame.Surface((40, 40))
rtn_x = 280
rtn_y = 640
a_x = 1000
a_y = 1000
count = 0
img_a = pygame.image.load('13.png')
img_b = pygame.image.load('43.png')
img_c = pygame.image.load('Без имени-01.bmp')
img_d = load_image('Стенка2.png')
img_e = load_image('Куст.png')
img_f = load_image('Меню.bmp')
img_g = load_image('Штаб.png')
myfont = pygame.font.SysFont('monospace', 15)
strike = False
level = [
"====================", #Уровень
"====================",
"====================",
"=== ++ / ++ ====",
"=== -++ - - ++- ====",
"=== +- - - -+ ====",
"=== + ====",
"=== -- - - - -- ====",
"=== - ====",
"=== - - - - ====",
"=== - +- - -+ - ====",
"=== -++- - -++- ====",
"=== -++- - -++- ====",
"=== - - - - ====",
"=== - - - - ====",
"=== - --- - ====",
"=== -_- ====",
"====================",
"====================",
"===================="]
z_x = 320
z_y = 120
PLATFORM_WIDTH = int(win_widht / 20)
PLATFORM_HEIGHT = int(win_height / 20)
PLATFORM_COLOR = "#FF6262"
zet.set_colorkey((0, 0, 0))
platforms = []
Players = []
Shells = []
x = y = 0 # координаты
for row in level: # вся строка
for col in row: # каждый символ
if col == "-":
#создаем блок, заливаем его цветом и рисеум его
pf = pygame.sprite.Sprite()
pf.image = img_d
pf.rect = pygame.Rect(x, y, PLATFORM_WIDTH, PLATFORM_HEIGHT) # Размер
sprites.add(pf)
platforms.append(pf)
pf.rect.x = x
pf.rect.y = y #Местоположение x и y
if col == "+":
#создаем блок, заливаем его цветом и рисеум его
bush = pygame.sprite.Sprite()
bush.image = img_e
bush.rect = bush.image.get_rect() # Размер
not_all_sprites.add(bush)
bush.rect.x = x
bush.rect.y = y
if col == "=":
#создаем блок, заливаем его цветом и рисеум его
wall = pygame.sprite.Sprite()
wall.image = img_f
wall.rect = wall.image.get_rect() # Размер
sprites.add(wall)
platforms.append(wall)
wall.rect.x = x
wall.rect.y = y
if col == "_":
#создаем блок, заливаем его цветом и рисеум его
hq = pygame.sprite.Sprite()
hq.image = img_g
hq.rect = hq.image.get_rect() # Размер
sprites.add(hq)
hq.rect.x = x
hq.rect.y = y
x += PLATFORM_WIDTH #блоки платформы ставятся на ширине
y += PLATFORM_HEIGHT#то же самое и с высотой
x = 0 #на каждой новой строчке начинаем с нуля
done = False
clock = pygame.time.Clock()
player = Player(280, 640)
shell = Shell(1000,1000)
left = right = False
up = down = False
Players.append(player)
Shells.append(shell)
while done == False:
for e in pygame.event.get():
if e.type == pygame.QUIT:
done = True
keys = pygame.key.get_pressed()
if e.type == pygame.KEYDOWN and e.key == pygame.K_w:#Передвижение
up = True
if e.type == pygame.KEYDOWN and e.key == pygame.K_s:#-
down = True
if e.type == pygame.KEYDOWN and e.key == pygame.K_a:#-
left = True
if e.type == pygame.KEYDOWN and e.key == pygame.K_d:
right = True
if e.type == pygame.KEYDOWN and e.key == pygame.K_SPACE:
if strike == False:
strike = True
shell.teleport_xy(strike, Players)
if e.type == pygame.KEYUP and e.key == pygame.K_w:
up = False
if e.type == pygame.KEYUP and e.key == pygame.K_s:
down = False
if e.type == pygame.KEYUP and e.key == pygame.K_a:
left = False
if e.type == pygame.KEYUP and e.key == pygame.K_d:
right = False
player.update(left, right, up, down)
if strike == True:
shell.strike()
for x in Shells:
if x.Strike == False:
strike = False
string = myfont.render('Очков: '+str(count), 0, (255,0,0)) #Счет
# Вывод в окно
sprites.draw(screen)
not_all_sprites.draw(screen)
zet.blit(img_b, (0, 0) )
screen.blit(string, (0, 50))
screen.blit(zet, (z_x, z_y))
window.blit(screen, (0, 0))
screen.fill((0, 0, 0))
pygame.display.update()
clock.tick(100)
pygame.quit()