0
0 комментариев

Версия python: 3

Операционная система: Windows 10

Ошибка где-то в этом куске кода:

[apcode language=»python»]

import pygame
from random import choice
import sys
  def load_image(name, colorkey=None):
    fullname = os.path.join('data', name)
    if colorkey is not None:
        image = pygame.image.load(fullname).convert()
        if colorkey == -1:
            colorkey = image.get_at((0, 0))
        image.set_colorkey(colorkey)
    else:
        image = pygame.image.load(fullname).convert()
    return image
  def terminate():
    pygame.quit()
    sys.exit()
  class AnimatedSprite(pygame.sprite.Sprite):
    def __init__(self, sheet, columns, rows, x, y, group):
        super().__init__(group)
        self.group = group
        self.jumping = False
        self.sheet = sheet
        self.columns = columns
        self.rows = rows
        self.x = x
        self.y = y
        self.frames = []
        self.cut_sheet(sheet, columns, rows)
        self.cur_frame = 0
        self.image = self.frames[self.cur_frame]
        self.rect = self.rect.move(x - self.rect[2], y - self.rect[3])
        self.mask = pygame.mask.from_surface(self.image)
     def cut_sheet(self, sheet, columns, rows):
        self.rect = pygame.Rect(0, 0, sheet.get_width() // columns,
                                sheet.get_height() // rows)
        for j in range(rows):
            for i in range(columns):
                frame_location = (self.rect.w * i, self.rect.h * j)
                self.frames.append(sheet.subsurface(pygame.Rect(
                    frame_location, self.rect.size)))
     def update(self):
        self.cur_frame += 1
        self.image = self.frames[self.cur_frame % len(self.frames)]
        self.image.set_colorkey(self.image.get_at((0, 0)))
        if self.jumping and self.cur_frame == len(self.frames):
            self.jumping = False
             self.frames = []
            self.cut_sheet(self.sheet, self.columns, self.rows)
            self.cur_frame = 0
            self.image = self.frames[self.cur_frame]
            self.rect = self.rect.move(self.x - self.rect[2], self.y - self.rect[3])
            self.mask = pygame.mask.from_surface(self.image)
            # self.image.set_colorkey(self.image.get_at((0, 0)))
        if not self.jumping and pygame.key.get_pressed()[32]:
            self.jump()
        self.cur_frame = self.cur_frame % len(self.frames)
     def jump(self):
        super().__init__(self.group)
        self.jumping = True
        self.frames = []
        self.cut_sheet(load_image('dino-jump.png'), 7, 1)
        self.cur_frame = 0
        self.image = self.frames[self.cur_frame]
        # self.image.set_colorkey(self.image.get_at((0, 0)))
        self.rect = self.rect.move(self.x - self.rect[2], self.y - self.rect[3])
        self.mask = pygame.mask.from_surface(self.image)
     def is_jumping(self):
        return self.jumping
  class WallForDino(pygame.sprite.Sprite):
    def __init__(self, parent, x):
        super().__init__(parent.walls_group, parent.all_spr)
        self.parent = parent
        self.image = load_image('kaktus.png')
        self.rect = self.image.get_rect().move(
            500 + (x * self.image.get_rect()[2]), 300 - self.image.get_rect()[3]
        )
        self.mask = pygame.mask.from_surface(self.image)
        self.image.set_colorkey(self.image.get_at((0, 0)))
        sprite = pygame.sprite.Sprite()
        sprite.image = self.image
        sprite.rect = self.rect
        parent.walls_group.add(sprite)
  class GoogleDino:
    def __init__(self, parent):
        self.parent = parent
         self.screen_size = self.screen_width, self.screen_height = 500, 500
        self.dino_screen = pygame.Surface(self.screen_size)
         self.fon = load_image('dino_fon.png')
        self.fon_x = 0
        self.dino_screen.blit(self.fon, (0, 135))
         self.dino_group = pygame.sprite.Group()
        self.walls_group = pygame.sprite.Group()
        self.all_spr = pygame.sprite.Group()
         x_walls = self.generate_kaktuses()
        for i in x_walls:
            a = WallForDino(self, i)
         self.dino = AnimatedSprite(load_image('dino-run.png'), 5, 1, self.screen_width // 7, 295, self.all_spr)
        self.dino_frame_per_game_frame = 0.5
        self.dino_jumping = False
        self.dino_group.add(self.dino)
        self.all_spr.add(self.dino)
         self.frame = 0
         self.dino_screen.fill((255, 255, 255))
        self.dino_screen.blit(self.fon, (self.fon_x, 135))
        self.dino.image.set_colorkey(self.dino.image.get_at((0, 0)))
        self.all_spr.draw(self.dino_screen)
        self.wall_move = -8
        self.score = 0
         self.font = pygame.font.Font(None, 30)
        string_rendered = self.font.render(str(self.score), 1, pygame.Color('black'))
        self.dino_screen.blit(string_rendered, (10, 10, 400, 20))
        print(string_rendered)
        self.running_game = True
        self.close_dino = 70
     def generate_kaktuses(self):
        amount = choice([_ for _ in range(50, 200)])
        amount = 2
        ooo = [_ for _ in range(1, 10)]
        sp = [choice(ooo)]
        for i in range(amount):
            sp.append(sp[-1] + choice(ooo))
        sp_1 = sp[:]
        for _ in range(3):
            for i in range(len(sp)):
                try:
                    a = sp[i:i + 3]
                    if 0 < a[1] - a[0] == a[2] - a[1] and a[1] - a[0] < 7:
                        sp_1.remove(sp[i])
                    elif 1 < a[1] - a[0] < 7:
                        sp_1.remove(sp[i])
                except Exception:
                    pass
            sp = sp_1[::]
        return sp
     def update(self):
        if not self.running_game and self.close_dino > 0:
            self.close_dino -= 1
            return self.dino_screen
        elif self.close_dino == 0:
            main_game.dino_is_active = False
        elif self.walls_group.__len__() == 0:
            text = self.font.render('You WIN!', False, pygame.Color('orange'))
            self.dino_screen.blit(text, (200, 320, 500, 500))
            self.running_game = False
            return self.dino_screen
         self.frame += 1
        self.dino_jumping = self.dino.is_jumping()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                terminate()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE and not self.dino_jumping:
                    self.dino.jump()
                elif event.key == pygame.K_ESCAPE:
                    self.parent.dino_is_active = False
        self.dino_screen.fill((255, 255, 255))
         for wall in self.walls_group:
            o = wall.rect
            if o[0] < - o[2]:
                self.walls_group.remove(wall)
            wall.rect = wall.rect.move(self.wall_move, 0)
        self.dino_screen.blit(self.fon, (self.fon_x, 135))
         if self.dino_jumping:
            self.dino_frame_per_game_frame = 0.35
            self.score += 2
        else:
            self.dino_frame_per_game_frame = 0.7
            self.score += 0.2
        string_rendered = self.font.render(str(int(self.score)), 1, pygame.Color('black'))
        self.dino_screen.blit(string_rendered, (10, 10, 400, 20))
         if self.frame * self.dino_frame_per_game_frame >= 1:
            self.dino.update()
            self.frame = 0
        self.dino.image.set_colorkey(self.dino.image.get_at((0, 0)))
         self.dino_group.draw(self.dino_screen)
        self.walls_group.draw(self.dino_screen)
        main_game.main_screen.blit(self.dino_screen, main_game.dino_rect)
         for i in self.walls_group:
            i.mask = pygame.mask.from_surface(i.image)
            print(self.dino.mask, i.mask, pygame.sprite.collide_mask(self.dino, i))
            if pygame.sprite.collide_mask(self.dino, i) is not None:
                print('STOP GAME!')
                text = self.font.render('You lose', False, pygame.Color('black'))
                self.dino_screen.blit(text, (200, 320, 500, 500))
                self.running_game = False
                for w in self.walls_group:
                    w.rect = w.image.get_rect().move(-1 * (main_game.WIDTH + 100), 0)
                break
         return self.dino_screen

[/apcode]

Используемая библиотека: pygame

Не работает функция pygame.sprite.collide_mask. Не видит столкновение по маске (маски есть)

Добавить комментарий