平铺python游戏(pygame)

时间:2020-05-21 14:33:14

标签: python python-3.x pygame game-engine

使用pygame库在python中制作方块游戏。在游戏中,通过该功能的计时器应从左上方开始工作。一切都为我显示,但是由于某种原因该帐户未保留。您需要考虑我需要一个带有秒数的变量,因为我将随着秒数的增加而增加敌人的数量。
这是代码:

import sys
import pygame as pg
import random

#         R    G    B
WHITE = (255, 255, 255,)
BLACK = (0, 0, 0)
DARKGREY = (40, 40, 40)
LIGHTGREY = (43, 43, 43)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 200, 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 MobY(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(0, WIDTH, TILESIZE)
        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(0, WIDTH, TILESIZE)
            self.rect.y = random.randrange(-100, -40)
            self.speedy = random.randrange(6, 9)


class MobX(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 = 0  # random.randrange(0, WIDTH, TILESIZE)
        self.rect.y = random.randrange(0, HEIGHT, TILESIZE)
        self.speedy = random.randrange(6, 9)

    def update(self):
        self.rect.x += self.speedy
        if self.rect.left > WIDTH + TILESIZE:
            self.rect.x = 0  # random.randrange(0, WIDTH, TILESIZE)
            self.rect.y = random.randrange(0, HEIGHT, TILESIZE)
            self.speedy = random.randrange(6, 9)


class MobYY(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(0, WIDTH, TILESIZE)
        self.rect.y = GRIDHEIGHT * TILESIZE
        self.speedy = random.randrange(6, 9)

    def update(self):
        self.rect.y -= self.speedy
        if self.rect.bottom < 0:
            self.rect.x = random.randrange(0, WIDTH, TILESIZE)
            self.rect.y = GRIDHEIGHT * TILESIZE
            self.speedy = random.randrange(6, 9)


class MobXX(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 = GRIDWIDTH * TILESIZE
        self.rect.y = random.randrange(0, HEIGHT, TILESIZE)
        self.speedy = random.randrange(6, 9)

    def update(self):
        self.rect.x -= self.speedy
        if self.rect.right < 0:
            self.rect.x = GRIDWIDTH * TILESIZE
            self.rect.y = random.randrange(0, HEIGHT, TILESIZE)
            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(1):
            m = MobY()
            self.all_sprites.add(m)
            self.mobs.add(m)
        for i in range(1):
            m = MobX()
            self.all_sprites.add(m)
            self.mobs.add(m)
        for i in range(1):
            m = MobYY()
            self.all_sprites.add(m)
            self.mobs.add(m)
        for i in range(1):
            m = MobXX()
            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 time(self):
        self.font = pg.font.Font(None, 50)
        self.font_color = pg.Color('springgreen')
        self.frame_count = 0
        self.total_seconds = self.frame_count // FPS
        self.seconds = self.total_seconds % 60
        self.output_string = "TIME: {0}".format(self.seconds)
        self.text = self.font.render(self.output_string, True, self.font_color)
        self.screen.blit(self.text, [10, 10])
        self.frame_count += 1
        if self.seconds >= 10:
            self.playing = False

    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)
        self.time()
        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()

1 个答案:

答案 0 :(得分:2)

time类中调用的Game类内的main_loop方法中,

每个周期都要设置self.frame_count = 0,因此它在0和1之间不断变化。您需要删除self.frame_count = 0并将其放入__init__方法中,以免重置每一帧。