Pygame Танчики

Исходный код

Простая игра

import pygame

class Tank(pygame.sprite.Sprite):
   def init(self, x, y, size):
      pygame.sprite.Sprite.init(self)
      self.image = pygame.Surface((size, size))
      self.image.fill((0, 0, 0))
      self.rect = self.image.get_rect(center=(x, y))
      self.speed = 3
      self.motion = 1

class Bullet(pygame.sprite.Sprite):
   def init(self, x, y, motion, speed=7):
      pygame.sprite.Sprite.init(self)
      self.image = pygame.Surface((5, 5))
      self.image.fill((255, 0, 0))
      self.rect = self.image.get_rect(center=(x, y))
      self.motion = motion
      self.speed = speed
def update(self):
    if self.rect.x > 700 or self.rect.x < 0 or self.rect.y > 700 or self.rect.y < 0:
        self.kill()
    if self.motion == 1:
        self.rect.y -= self.speed
    if self.motion == 2:
        self.rect.x += self.speed
    if self.motion == 3:
        self.rect.y += self.speed
    if self.motion == 4:
        self.rect.x -= self.speed

pygame.init()
screen = pygame.display.set_mode((700, 700))
pygame.display.set_caption("Tank")

tank = Tank(40, 40, 40)

bullet_group = pygame.sprite.Group()

run = True
   while run:
      for e in pygame.event.get():
         if e.type == pygame.QUIT:
            run = False
         elif e.type == pygame.KEYDOWN:
            if e.key == pygame.K_SPACE:
               bullet_group.add(Bullet(tank.rect.centerx, tank.rect.centery, tank.motion))

key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
    tank.rect.x -= tank.speed
    tank.motion = 4
if key[pygame.K_RIGHT]:
    tank.rect.x += tank.speed
    tank.motion = 2
if key[pygame.K_UP]:
    tank.rect.y -= tank.speed
    tank.motion = 1
if key[pygame.K_DOWN]:
    tank.rect.y += tank.speed
    tank.motion = 3

bullet_group.update()

screen.fill((0, 255, 255))
screen.blit(tank.image, tank.rect)
bullet_group.draw(screen)
pygame.display.flip()
pygame.time.delay(20)

pygame.quit()

Танчики

import random

import pygame

class Tank(pygame.sprite.Sprite):
   def init(self, x, y, size):
      pygame.sprite.Sprite.init(self)
      self.image = pygame.Surface((size, size))
      self.image.fill((0, 0, 0))
      self.rect = self.image.get_rect(center=(x, y))
      self.speed = 3
      self.motion = 1

class Bullet(pygame.sprite.Sprite):
   def init(self, x, y, motion, speed=7):
   pygame.sprite.Sprite.init(self)
   self.image = pygame.Surface((5, 5))
   self.image.fill((255, 0, 0))
   self.rect = self.image.get_rect(center=(x, y))
   self.motion = motion
   self.speed = speed


def update(self):
    if self.rect.x > 700 or self.rect.x < 0 or self.rect.y > 700 or self.rect.y < 0:
        self.kill()
    if self.motion == 1:
        self.rect.y -= self.speed
    if self.motion == 2:
        self.rect.x += self.speed
    if self.motion == 3:
        self.rect.y += self.speed
    if self.motion == 4:
        self.rect.x -= self.speed

class Enemy(pygame.sprite.Sprite):
   def init(self, x=None, y=None, direction=None, speed=4):
      pygame.sprite.Sprite.init(self)
      self.image = pygame.Surface((20, 20))
      self.image.fill((random.randint(0, 255), random.randint(0,     255), random.randint(0, 255)))
      self.rect = self.image.get_rect(center=(x or   random.randint(0, 700), y or random.randint(0, 700)))
      self.direction = direction or random.choice(('ver', 'hor'))
      self.speed = speed


def update(self):
    if self.direction == 'ver':
        self.rect.y += self.speed
        if self.rect.y > 700:
            self.speed = -abs(self.speed)
        if self.rect.y < 0:
            self.speed = abs(self.speed)
    elif self.direction == 'hor':
        self.rect.x += self.speed
        if self.rect.x > 700:
            self.speed = -abs(self.speed)
        if self.rect.x < 0:
            self.speed = abs(self.speed)


pygame.init()
screen = pygame.display.set_mode((700, 700))
pygame.display.set_caption("Tank")

tank = Tank(40, 40, 40)

bullet_group = pygame.sprite.Group()
enemy_group = pygame.sprite.Group()

font = pygame.font.Font('murderer.ttf', 25)

period_enemy = 200
current_period_enemy = 0

result = 0
next_level = 5

run = True
while run:
   for e in pygame.event.get():
      if e.type == pygame.QUIT:
         run = False
      elif e.type == pygame.KEYDOWN:
         if e.key == pygame.K_SPACE:
            if len(bullet_group.sprites()) < 5:
               bullet_group.add(Bullet(tank.rect.centerx,   tank.rect.centery, tank.motion))

key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
    tank.rect.x -= tank.speed
    tank.motion = 4
if key[pygame.K_RIGHT]:
    tank.rect.x += tank.speed
    tank.motion = 2
if key[pygame.K_UP]:
    tank.rect.y -= tank.speed
    tank.motion = 1
if key[pygame.K_DOWN]:
    tank.rect.y += tank.speed
    tank.motion = 3

if current_period_enemy >= period_enemy:
    enemy_group.add(Enemy())
    current_period_enemy = 0

bullet_group.update()
enemy_group.update()

if pygame.sprite.spritecollide(tank, enemy_group, False):
    run = False

if pygame.sprite.groupcollide(bullet_group, enemy_group, True, True):
    result += 1
    if result >= next_level:
        next_level += 5
        period_enemy /= 2

screen.fill((0, 255, 255))
screen.blit(tank.image, tank.rect)
bullet_group.draw(screen)
enemy_group.draw(screen)
screen.blit(font.render(f'Result: {result}', True, (0, 0, 0), (0, 255, 255)), (30, 30))
screen.blit(font.render(f'Level: {int(next_level/5)}', True, (0, 0, 0), (0, 255, 255)), (200, 30))
pygame.display.flip()
pygame.time.delay(20)
current_period_enemy += 1

pygame.quit()

Полезная ссылка

Полезная ссыла

Простая 2D-игра на Python, разработанная для того, чтобы научить вас работе с модулем pygame.

pygame_tutorials

Python-игры