-2

我正在重制一个视频游戏来学习 pygame 和 livewires。我正在使用 livewires,因为它似乎是用精灵加载背景图形的好方法。

我试图让一个预加载的精灵水平移动,同时在正确的位置保持移动(在这种情况下它向上 50 像素)。

我可以使用 pygame 获得精灵移动,或者我可以将背景与精灵加载在正确的位置,或者我可以让精灵移动,但两者似乎不会同时发生。

为了额外的好处,当角色移动不同的位置时,我还需要屏幕向右滚动。

这是我的代码:

import pygame, sys
from livewires import games
from pygame.locals import *

games.init(screen_width = 640, screen_height = 480, fps = 50) #setup up the window siz

class Mario(games.Sprite):

    def update(self, pressed_keys):
        move = 50 #Setup the origianl position of the character
        if pressed_keys[K_RIGHT]: move += 1 #press right key to move forward
        if pressed_keys[K_LEFT]: move -= 1 #press left key to move back     

    def main():
        pygame.init()
        screen_image = games.load_image("World 1-1.bmp", transparent = False) #setup the background image
        games.screen.background = screen_image
        mario_image = games.load_image("Mario3.bmp")
        mario = games.Sprite(image = mario_image, x = move, y=370) #setup the position of the character
        sprites.add(mario)
        pygame.display.update()

        while True:
            for event in pygame.event.get():
                if event.type == QUIT: return pygame.quit() #if the player quits

            keys_pressed = pygame.key.get_pressed()

    games.screen.mainloop()

    main()
4

1 回答 1

3

如果您只使用 Pygame 或使用 Livewires,对我来说似乎要简单得多。不要试图强迫两个模块一起工作,如果它们不是故意的。此外,Livewires 的介绍页面说该模块是 Python 课程的附加组件,而不是独立的游戏模块。我建议你只使用 Pygame,因为它一个独立的游戏模块。

此外,您上面的代码似乎有点草率(请不要个人认为),我将在下面向您展示如何制作入门 Pygame 文件。

Pygame 文件的主要部分是游戏循环。一个典型的 Pygame 游戏循环(或任何真正的游戏循环)包含三个基本部分:

  1. 事件检查器,用于检查任何事件
  2. 事件执行器,当相应事件发生时执行某些操作。
  3. 渲染图形的地方。

为了大致了解 Pygame 游戏的良好起始文件,下面是一个示例:

import pygame #import the pygame moudle into the namespace <module>

WIDTH = 640 # define a constant width for our window
HEIGHT = 480 # define a constant height for our window

display = pygame.display.set_mode((WIDTH, HEIGHT)) #create a pygame window, and
#initialize it with our WIDTH and HEIGHT constants

running = True # our variable for controlling our game loop

while running:
    for e in pygame.event.get(): # iterate ofver all the events pygame is tracking
        if e.type == pygame.QUIT: # is the user trying to close the window?
            running = False # if so break the loop
            pygame.quit() # quit the pygame module
            quit() # quit is for IDLE friendliness

    display.fill((255, 255, 255)) # fill the pygame screen with white
    pygame.display.flip() # update the screen

以上将是制作 Pygame 游戏的一个很好的起点。

但回到手头的问题:

我假设你只使用 Pygame,有几种方法可以让精灵/形状在 Pygame 中移动。

方法一:使用精灵类

要使您的马里奥精灵移动,您可以使用如下所示的精灵类。

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("path\to\file.png")
        self.image.set_colorkey() # make this the color of your outlines around your image(if any exit)
        self.rect = self.image.get_rect()
        self.rect.x = WIDTH / 2
        self.rect.y = HEIGHT / 2
        self.vx = 0
        self.vy = 0

    def update(self):
        self.vx = 0
        self.vy = 0
        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            self.vx = -5
        elif key[pygame.K_RIGHT]:
            self.vx = 5
        if key[pygame.K_UP]:
            self.vy = -5
        elif key[pygame.K_DOWN]:
            self.vy = 5
        self.rect.x += self.vx
        self.rect.y += self.vy

由于您的类继承自 Pygame 的 sprite 类,因此您必须将图像命名为 self.image,并且必须将图像的矩形命名为 self.rect。您还可以看到,该类有两个主要方法。一种用于创建精灵(init),一种用于更新精灵(update)

要使用您的类,请创建一个 Pygame 精灵组来保存您的所有精灵,然后将您的玩家对象添加到该组中:

sprites = pygame.sprite.Group()
player = Player()
sprtites.add(player)

并将您的精灵实际渲染到屏幕调用 sprites.update() 和 sprites.draw() 在您的游戏循环中,您可以在其中更新屏幕:

sprites.update()
window_name.fill((200, 200, 200))
sprites.draw(window_name)
pygame.display.flip()

我强烈推荐使用 sprite 类的原因是它会让你的代码看起来更干净,并且更容易维护。您甚至可以将每个精灵类移动到它们自己的单独文件中。

然而,在深入研究上述方法之前,您应该阅读pygame.Rect对象和pygame.sprite对象,因为您将使用它们。

方法二:使用A函数

如果你不想进入 sprite 类,你可以使用类似于下面的函数来创建你的游戏实体。

def create_car(surface, x, y, w, h, color):
    rect = pygame.Rect(x, y, w, h)
    pygame.draw.rect(surface, color, rect)
    return rect

如果您仍然想使用精灵,但不想创建一个类,只需稍微修改上面的函数:

def create_car(surface, x, y, color, path_to_img):
    img = pygame.image.load(path_to_img)
    rect = img.get_rect()
    surface.blit(img, (x, y))

这是我如何使用上述函数制作可移动矩形/精灵的示例:

import pygame

WIDTH = 640
HEIGHT = 480
display = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Moving Player Test")
clock = pygame.time.Clock()
FPS = 60

def create_car(surface, x, y, w, h, color):
    rect = pygame.Rect(x, y, w, h)
    pygame.draw.rect(surface, color, rect)
    return rect

running = True
vx = 0
vy = 0
player_x = WIDTH / 2 # middle of screen width
player_y = HEIGHT / 2 # middle of screen height
player_speed = 5
while running:
    clock.tick(FPS)
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False
            pygame.quit()
            quit()
        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_LEFT:
                vx = -player_speed
            elif e.key == pygame.K_RIGHT:
                vx = player_speed
            if e.key == pygame.K_UP:
                vy = -player_speed
            elif e.key == pygame.K_DOWN:
                vy = player_speed
        if e.type == pygame.KEYUP:
            if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT or\
               e.key == pygame.K_UP or e.key == pygame.K_DOWN:
                vx = 0
                vy = 0

    player_x += vx
    player_y += vy
    display.fill((200, 200, 200))
    ####make the player#####
    player = create_car(display, player_x, player_y, 10, 10, (255, 0, 0))
    pygame.display.flip()

我应该注意,我假设上面列出的每种方法都有一些事情。

您要么使用圆形、方形或某种类型的 pygame 形状对象。或者你使用精灵。如果您目前没有使用上述任何方法,我建议您这样做。一旦您开始构建更大、更复杂的游戏,这样做将使您的代码更易于维护。

于 2016-08-22T04:31:26.830 回答