pygame中的绘图问题

问题描述 投票:0回答:1

我正在尝试在 pygame 中制作一个相对简单的游戏:

import pygame as pg
import sys
import math

# game settings
RES = WIDTH, HEIGHT = 1600, 900
FPS = 60

PLAYER_POS = 1.5, 5 #mini_map
PLAYER_ANGLE = 0
PLAYER_SPEED = 0.004
PLAYER_ROT_SPEED = 0.002


# initializes game
class Game:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode(RES)
        self.clock = pg.time.Clock()
        self.delta_time = 1
        self.new_game()
        self.initialize_world_map = self.map.initialize_world_map

    def new_game(self):
        self.map = Map(self)
        self.player = Player(self)

    def update(self):
        self.player.update()
        pg.display.flip()
        self.delta_time = self.clock.tick(FPS)
        pg.display.set_caption(f'{self.clock.get_fps() :.1f}')
        self.map.draw()

    def draw(self):
        self.screen.fill('black')
        self.map.draw()
        self.player.draw_player()

    def check_events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_SPACE):
                pg.quit()
                sys.exit()

    def run(self):
        while True:
            self.check_events()
            self.update()
            self.screen.fill('black')
            self.map.draw()
            self.player.draw_player()

_ = False
mini_map = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 1],
[1, _, _, 3, 3, 3, 3, _, _, _, 2, 2, 2, _, _, 1],
[1, _, _, _, _, _, 4, _, _, _, _, _, 2, _, _, 1],
[1, _, _, _, _, _, 4, _, _, _, _, _, 2, _, _, 1],
[1, _, _, 3, 3, 3, 3, _, _, _, _, _, _, _, _, 1],
[1, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 1],
[1, _, _, _, 4, _, _, _, 4, _, _, _, _, _, _, 1],
[1, 1, 1, 3, 1, 3, 1, 1, 1, 3, _, _, 3, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 3, _, _, 3, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 3, _, _, 3, 1, 1, 1],
[1, 1, 3, 1, 1, 1, 1, 1, 1, 3, _, _, 3, 1, 1, 1],
[1, 4, _, _, _, _, _, _, _, _, _, _, _, _, _, 1],
[3, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 1],
[1, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 1],
[1, _, _, 2, _, _, _, _, _, 3, 4, _, 4, 3, _, 1],
[1, _, _, 5, _, _, _, _, _, _, 3, _, 3, _, _, 1],
[1, _, _, 2, _, _, _, _, _, _, _, _, _, _, _, 1],
[1, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 1],
[3, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 1],
[1, 4, _, _, _, _, _, _, 4, _, _, 4, _, _, _, 1],
[1, 1, 3, 3, _, _, 3, 3, 1, 3, 3, 1, 3, 1, 1, 1],
[1, 1, 1, 3, _, _, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 3, 3, 4, _, _, 4, 3, 3, 3, 3, 3, 3, 3, 3, 1],
[3, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 3],
[3, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 3],
[3, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 3],
[3, _, _, 5, _, _, _, 5, _, _, _, 5, _, _, _, 3],
[3, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 3],
[3, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 3],
[3, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 3],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],


]


class Map:
    def __init__(self, game):
        self.initialize_world_map = mini_map
        self.game = game
        # self.mini_map = mini_map
        self.get_map()

    def world_map(self):
        pass


    def get_map(self):
        for j, row in enumerate(self.initialize_world_map):
            for i, value in enumerate(row):
                if value:
                    self.initialize_world_map[j][i] = value

    # draws map
    def draw(self):
        for pos in self.initialize_world_map:
            pg.draw.rect(self.game.screen, 'white', (pos[0] * 100, pos[1] * 100, 100, 100), 2)


class Player:
    def __init__(self, game):
        self.map = map
        self.game = game
        self.angle = PLAYER_ANGLE
        self.x, self.y = PLAYER_POS

    def movement(self):
        sin_a = math.sin(self.angle)
        cos_a = math.cos(self.angle)
        dx, dy = 0, 0
        speed = PLAYER_SPEED * self.game.delta_time
        speed_sin = speed * sin_a
        speed_cos = speed * cos_a

        keys = pg.key.get_pressed()
        if keys[pg.K_d]:
            dx += speed_cos
            dy += speed_sin
        if keys[pg.K_a]:
            dx += -speed_cos
            dy += -speed_sin
        if keys[pg.K_s]:
            dx += -speed_sin
            dy += speed_cos
        if keys[pg.K_w]:
            dx += speed_sin
            dy += -speed_cos

        self.check_wall_collision(dx, dy)

        if keys[pg.K_UP]:
            self.angle -= PLAYER_ROT_SPEED * self.game.delta_time
        if keys[pg.K_RIGHT]:
            self.angle += PLAYER_ROT_SPEED * self.game.delta_time
        self.angle %= math.tau

    def check_wall(self, x, y):
        return (x, y) not in self.game.initialize_world_map

    def check_wall_collision(self, dx, dy):
        if self.check_wall(int(self.x + dx), int(self.y)):
            self.x += dx
        if self.check_wall(int(self.x), int(self.y + dy)):
            self.y += dy

    def draw_player(self):
        pg.draw.line(self.game.screen, 'yellow', (self.x * 100, self.y * 100),
                     (self.x * 100 + WIDTH * math.cos(self.angle),
                      self.y * 100 + WIDTH * math.sin(self.angle)), 2)
        pg.draw.circle(self.game.screen, 'green', (int(self.x * 100), int(self.y * 100)), 15)

    def update(self):
        self.movement()

    @property
    def pos(self):
        return self.x, self.y

    @property
    def map_pos(self):
        return int(self.x), int(self.y)



if __name__ == '__main__':
    game = Game()
    game.run()
    Game.__init__(game)

它应该生成 mini_map 列表框中的所有数字,但它只是在左上角附近生成几个框。我尝试重写类映射中的函数并稍微切换一下变量,但没有成功。我真正的问题不是它没有运行,而是我不明白为什么。任何帮助将不胜感激。

python pygame draw
1个回答
0
投票

self.initialize_world_map”是一个嵌套列表,其中包含地图中的值。绘制网格时必须迭代网格的行和列。

class Map:
    # [...]

    def draw(self):
        for j, row in enumerate(self.initialize_world_map):
            for i, value in enumerate(row):
                if value:
                    pg.draw.rect(self.game.screen, 'white', (j * 100, i * 100, 100, 100), 2)

请注意,

get_map
根本不执行任何操作,因为它仅将网格中的值分配给自身

© www.soinside.com 2019 - 2024. All rights reserved.