python - python 中的瓷砖游戏

标签 python python-3.x pygame tile

我使用 PyGame 库在 python 中制作了一个瓷砖游戏。游戏的本质是:有一个我们控制的方格,而敌人会出现在四面八方。我对敌人的外观有疑问,我无法使沿 X 坐标的敌人准确地出现在图 block 上。现在它们出现在 X 坐标上的随机点,但它们需要位于图 block 上。
这是代码:

import sys
import pygame as pg
import random

WHITE = (255, 255, 255,)
BLACK = (0, 0, 0)
DARKGREY = (40, 40, 40)
LIGHTGREY = (100, 100, 100)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

WIDTH = 1008  # 16 * 64 или 32 * 32 или 64 * 16
HEIGHT = 768  # 16 * 48 или 32 * 24 или 64 * 12
FPS = 60
TITLE = "TITLE GAME"
BGCOLOR = DARKGREY

TILESIZE = 48
GRIDWIDTH = WIDTH / TILESIZE
GRIDHEIGHT = HEIGHT / TILESIZE


class Player(pg.sprite.Sprite):
    def __init__(self, game, x, y):
        self.groups = game.all_sprites
        pg.sprite.Sprite.__init__(self, self.groups)
        self.game = game
        self.image = pg.Surface((TILESIZE, TILESIZE))
        self.image.fill(YELLOW)
        self.rect = self.image.get_rect()
        self.x = x
        self.y = y

    def move(self, dx=0, dy=0):
        if not self.colide_with_walls(dx, dy):
            self.x += dx
            self.y += dy

    def colide_with_walls(self, dx=0, dy=0):
        for wall in self.game.walls:
            if wall.x == self.x + dx and wall.y == self.y + dy:
                return True
        return False

    def update(self):
        self.rect.x = self.x * TILESIZE
        self.rect.y = self.y * TILESIZE


class Wall(pg.sprite.Sprite):
    def __init__(self, game, x, y):
        self.groups = game.all_sprites, game.walls
        pg.sprite.Sprite.__init__(self, self.groups)
        self.game = game
        self.image = pg.Surface((TILESIZE, TILESIZE))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.x, self.y = x, y
        self.rect.x = x * TILESIZE
        self.rect.y = y * TILESIZE


class Mob(pg.sprite.Sprite):
    def __init__(self):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.Surface((TILESIZE, TILESIZE))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(WIDTH - self.rect.width)
        self.rect.y = random.randrange(-100, -40)
        self.speedy = random.randrange(6, 9)

    def update(self):
        self.rect.y += self.speedy
        if self.rect.top > HEIGHT + TILESIZE:
            self.rect.x = random.randrange(WIDTH - self.rect.width)
            self.rect.y = random.randrange(-100, -40)
            self.speedy = random.randrange(6, 9)


class Game:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        pg.key.set_repeat(1, 15)

    def new(self):
        self.all_sprites = pg.sprite.Group()
        self.walls = pg.sprite.Group()
        self.mobs = pg.sprite.Group()
        self.player = Player(self, 10, 10)
        for i in range(10):
            m = Mob()
            self.all_sprites.add(m)
            self.mobs.add(m)
        for x in range(-1, 22):
            Wall(self, x, -1)
        for x in range(-1, 22):
            Wall(self, x, 16)
        for x in range(-1, 17):
            Wall(self, -1, x)
        for x in range(-1, 17):
            Wall(self, 21, x)

    def run(self):
        self.playing = True
        while self.playing:
            self.dt = self.clock.tick(FPS) / 1000
            self.events()
            self.update()
            self.draw()

    def quit(self):
        pg.quit()
        sys.exit()

    def update(self):
        self.all_sprites.update()

    def draw_grid(self):
        for x in range(0, WIDTH, TILESIZE):
            pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
        for y in range(0, HEIGHT, TILESIZE):
            pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))

    def draw(self):
        self.screen.fill(BGCOLOR)
        self.draw_grid()
        self.all_sprites.draw(self.screen)
        pg.display.flip()

    def events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.quit()
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    self.quit()
                if event.key == pg.K_LEFT:
                    self.player.move(dx=-1)
                if event.key == pg.K_RIGHT:
                    self.player.move(dx=1)
                if event.key == pg.K_UP:
                    self.player.move(dy=-1)
                if event.key == pg.K_DOWN:
                    self.player.move(dy=1)

g = Game()
while True:
    g.new()
    g.run()

最佳答案

您可以使用randrange的第三个参数,即step。在Mob类中,设置矩形对象的x值,如下所示:

self.rect.x = random.randrange(0, WIDTH, TILESIZE) 

它们将与瓷砖完美对齐。

关于python - python 中的瓷砖游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61919495/

相关文章:

python - 为什么 Python 的 'StandardScaler' 和 Matlab 的 'zscore' 之间的标准化不同?

class - python 3 中的 types.ClassType 发生了什么?

python django scrapy 将项目返回到 Controller

python - 移动 slider 时更改音量

python - PyOpenGL + PyGame 上的奇怪 "X"

python - 从内存中永久删除 Sprite Pygame

python - 如何根据python中的 header 提取某些csv数据

python - 如何在 SQLAlchemy 中正确绑定(bind)对(元组数组、多维数组)?

python PIL : verify extension before saving

python - 向内置类型的多重继承子类的构造函数添加可选参数?