简体   繁体   English

Pygame 问题:我的播放器图像不随滚动而移动

[英]Pygame problem : my player image don't move with scroll

Firts I am french so excuse-me for my english.首先我是法国人,所以对不起我的英语。 I have recently discoverd pygame and I love that so decided to create a rpg on pygame and... I have a little problem: with many, many difficulty I made a scrolling system on my pygame's rpg I evn do a system how make sure player's image gets back when he go up, puts himself in profile... BUT (a big but) my player's image don't move, the scrolling, move and the player's rect also, I don't know I can solve that so please help me.:!!!我最近发现了 pygame 并且我喜欢它,所以决定在 pygame 上创建一个 rpg 并且...当他 go 起来时,图像回来了,把自己放在个人资料中......但是(很大但是)我的玩家的图像不动,滚动,移动和玩家的矩形,我不知道我能解决这个问题,所以请帮我。:!!! The main.py file : main.py 文件:

from game import Game
from level import *
pygame.init()

lvl = Level()
screen = pygame.display.set_mode((1024, 768))
pygame.display.set_caption("RPG") 
game = Game()
cam = Camera(1024, 768)
running = True
lvl.generer()
print(game.player.rect)
clock = pygame.time.Clock()

while running:
    cam.update(game.player)
    #print(cam.rect.topleft)
    if game.pressed.get(pygame.K_RIGHT) and game.player.rect.x + game.player.rect.width < 1080:
        game.player.move_right()
        lvl.afficher(screen, 0, 0, cam.x, cam.y)
        #print(game.player.rect.x)

    elif game.pressed.get(pygame.K_LEFT) and game.player.rect.x > 0:
        game.player.move_left()
        lvl.afficher(screen, 0, 0, cam.x, cam.y)
        #print(game.player.rect.x)

    elif game.pressed.get(pygame.K_DOWN):
        game.player.move_down()
        lvl.afficher(screen, 0, 0, cam.x, cam.y)
        #screen.scroll(0, -game.player.velocity)
        #print(game.player.rect.y)

    elif game.pressed.get(pygame.K_UP):
        game.player.move_up()
        lvl.afficher(screen, 0, 0, cam.x, cam.y)
        #screen.scroll(0, game.player.velocity)
        #print(game.player.rect.y)

    #print(cam.rect.x, cam.rect.y)
    #print(cam.rect)

    screen.blit(game.player.image, game.player.rect)
    print(game.player.rect)

    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()

        elif event.type == pygame.KEYDOWN:
             game.pressed[event.key] = True

        elif event.type == pygame.KEYUP:
            game.pressed[event.key] = False```
the player.py class : 
```import pygame

class Player(pygame.sprite.Sprite):
    def __init__(self, game):
        super(Player, self).__init__()
        self.game = game
        self.health = 100
        self.maxHealth = 100
        self.attack = 10
        self.velocity = 10
        self.dimension = (64, 88)
        self.image = pygame.image.load("assets/player/player_face/player_face.png")
        self.original_image = pygame.image.load("assets/player/player_face/player_face.png")
        self.image = pygame.transform.scale(self.image, self.dimension) 
        self.rect = self.image.get_rect()
        self.rect.x = 512
        self.rect.y = 384
        self.face = [pygame.image.load("assets/player/player_face/player/sprite_00.png"), pygame.image.load("assets/player/player_face/player/sprite_01.png"), pygame.image.load("assets/player/player_face/player/sprite_02.png"), pygame.image.load("assets/player/player_face/player/sprite_03.png"), pygame.image.load("assets/player/player_face/player/sprite_04.png"), pygame.image.load("assets/player/player_face/player/sprite_05.png"), pygame.image.load("assets/player/player_face/player/sprite_06.png"), pygame.image.load("assets/player/player_face/player/sprite_07.png"), pygame.image.load("assets/player/player_face/player/sprite_08.png"), pygame.image.load("assets/player/player_face/player/sprite_09.png")]


    def move_right(self):
        self.rect.x += self.velocity
        self.image = pygame.image.load("assets/player/player_profileLeft/player_profile_left.png")
        self.image = pygame.transform.scale(self.image, self.dimension) 

    def move_left(self):
        self.rect.x -= self.velocity
        self.image = pygame.image.load("assets/player/player_profileRight/player_profile_right.png")
        self.image = pygame.transform.scale(self.image, self.dimension) 

    def move_down(self):
        self.rect.y += self.velocity
        #self.image = pygame.image.load("assets/player/player_face/player_face.png")
        #self.image = pygame.transform.scale(self.image, self.dimension)
        i = 0
        for elt in self.face:
            self.image = self.face[i]
            print("lol")
            self.image = pygame.transform.scale(self.image, self.dimension)
            i += 1


    def move_up(self):
        self.rect.y -= self.velocity
        self.image = pygame.image.load("assets/player/player_back/player_dos.png")
        self.image = pygame.transform.scale(self.image, self.dimension)

the level.py class: level.py class:

from pygame.locals import *
from player import *

class Camera:
    def __init__(self, widht, height):
        self.rect = pygame.Rect(0, 0, widht, height)
        self.widht = widht
        self.height = height
        self.topleft = list(self.rect.topleft)
        self.x = self.topleft[0]
        self.y = self.topleft[1]

    def update(self, target):
        self.rect.y = -target.rect.y + int(769 / 2)
        self.rect.x = -target.rect.x + int(1024 / 2)
        self.topleft = list(self.rect.topleft)
        self.x = self.topleft[0]
        self.y = self.topleft[1]
        self.rect = pygame.Rect(self.rect.x, self.rect.y, self.widht, self.height)
        #self.rect.topleft_x = self.rect.topleft(0)
        #self.coo = (0, 0)


class Level:
    def __init__(self):
        self.structure = 0


    def generer(self):
        with open("niveau.txt", "r") as fichier:
            structure_niveau = []
            for ligne in fichier:
                ligne_niveau = []
                for sprite in ligne:
                    if sprite != '\n':
                        ligne_niveau.append(sprite)
                structure_niveau.append(ligne_niveau)
            self.structure = structure_niveau

    def afficher(self, fenetre, x, y, camLeftX, camLeftY):
        tailleSprite = 64
        #Camera.__init__(self, x, y)

        grass = pygame.image.load("assets/bloc/grass.png").convert_alpha()

        tree = pygame.image.load("assets/bloc/tree_grass.png").convert_alpha()

        no_texture = pygame.image.load("assets/bloc/no_texture.png")

        num_ligne = 0
        for ligne in self.structure:
            num_case = 0
            for sprite in ligne:
                x = num_case * tailleSprite - camLeftX
                y = num_ligne * tailleSprite - camLeftY
                if sprite == 'G':
                    fenetre.blit(grass, (x, y))

                elif sprite == 'T':
                    fenetre.blit(tree, (x, y))
                    #print(self.x, self.y)

                else:
                    fenetre.blit(no_texture, (x, y))

                num_case += 1
            num_ligne += 1```

Ok I just found the solution but It's making an other problem I'll ask the question on a other post.好的,我刚刚找到了解决方案,但它产生了另一个问题,我将在另一篇文章中提出这个问题。 At x = num_case * tailleSprite - camLeftX and y = num_ligne * tailleSprite - camLeftY on level.py just replace the - by a +在 x = num_case * tailleSprite - camLeftX 和 y = num_ligne * tailleSprite - camLeftY on level.py 只需将 - 替换为 +

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM