python - 使用pygame和python跟随玩家的相机

标签 python class pygame sprite

<分区>

我有 50 个要跟随玩家的外星 Sprite ,但首先我想要一个摄像机来跟随玩家,无论他走到哪里。

我在一个旧游戏中使用过这个,但是在我的旧游戏中我没有使用类或 Sprite 组,所以效率很低。

所以在这个游戏中,我将玩家设置在屏幕中央,其他一切都在移动,为此我有一个变量,比如 CameraXCameraY当玩家移动时,两个摄像机变量会上下移动。但是,在我的脚本中,外星人没有得到更新。无论如何,这是我的脚本:

import pygame, random, sys
from pygame.locals import *
pygame.init()

black    = (   0,   0,   0)
white    = ( 255, 255, 255)
red      = ( 255,   0,   0)


screen_width = 1080
screen_height = 720
screen = pygame.display.set_mode([screen_width,screen_height])

alien_list = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()

Alien = "graphics\sprites\Alien.png"
Player = "graphics\sprites\Player.png"

CameraX = 0
CameraY = 0


def main():
    class Enemy(pygame.sprite.Sprite):

        def __init__(self, image):
            pygame.sprite.Sprite.__init__(self) 

            self.image = pygame.image.load(image).convert_alpha()
            self.rect = self.image.get_rect()

        def Create():
            for i in range(50):
                alien = Enemy(Alien)

                alien.rect.x = random.randrange(screen_width - 50 - CameraX)
                alien.rect.y = random.randrange(screen_height - 50 - CameraY)

                alien_list.add(alien)
                all_sprites.add(alien)


    player = Enemy(Player)
    all_sprites.add(player)

    done = False

    clock = pygame.time.Clock()

    score = 0

    moveCameraX = 0
    moveCameraY = 0

    player.rect.x = 476
    player.rect.y = 296

    Enemy.Create()

    while done == False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

        if event.type == KEYDOWN:
            if event.key == K_w:
                moveCameraY = -10
            if event.key == K_s:
                moveCameraY = 10
            if event.key == K_a:
                moveCameraX = -10
            if event.key == K_d:
                moveCameraX = 10

        if event.type == KEYUP:
            if event.key == K_w:
                moveCameraY = 0 
            if event.key == K_s:
                moveCameraY = 0
            if event.key == K_a:
                moveCameraX = 0
            if event.key == K_d:
                moveCameraX = 0

        screen.fill(white)

        enemys_hit = pygame.sprite.spritecollide(player, alien_list, True)

        for enemy in enemys_hit:
            score += 1
            print(score)

        all_sprites.draw(screen)

        clock.tick(40)

        pygame.display.flip()

    pygame.quit()

然后是运行整个过程的脚本:

import pygame
import GameEg
from pygame.locals import *
pygame.init()

game = GameEg.main()

game.main()

感谢您的时间和帮助

最佳答案

您不需要使用 CameraXCameraY 变量。相反,当您获得键盘输入时,只需让外星人都朝那个方向移动即可。您也没有任何实际移动玩家和外星人的代码。您需要向您的类添加一个更新函数,以更改源矩形的位置。

关于python - 使用pygame和python跟随玩家的相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122518/

相关文章:

python - 在网络抓取工具中使用多个网页

python - 自动针对 box.com 进行身份验证以获取 api 访问权限

python - 类方法和类方法中定义的变量

python - pygame 未关闭并返回控制台(python)

python - pygame事件队列功能在鼠标输入时崩溃

python - 我的直肠没有更新

Python:对于每次迭代,系统状态都会发生变化

python - 单个表上的 SQLAlchemy 多对多关系

c++ - 如何将文件中的团队信息解析并存储到类中?

c++ - C++ 构造函数中的混淆