簡體   English   中英

Pygame,用新玩家x,y改變場景使玩家無法移動,因為它在循環中,我該如何解決這個問題?

[英]Pygame, Changing scenes with new player x,y makes the player not move because its in a loop, how do I fix this?

正如標題所示,如果我使用新播放器 position 更改場景,它會卡住,因為它處於循環中.y = new x 和 new y 但它到達該位置並卡住了,有一個播放器 class 和一個場景 class 我認為這就是問題所在,我仍然是一個初學者,我很驚訝我能走到這一步,幫助不勝感激謝謝!

import sys
from pygame.locals import *
import time

pygame.init()
display_w = 1024
display_h = 768

world = pygame.Surface((2500, 2500))  # 2550, 2500
window = pygame.display.set_mode((display_w, display_h), HWSURFACE | DOUBLEBUF | RESIZABLE)
pygame.display.set_icon(pygame.image.load("game_icon.png").convert_alpha())
pygame.display.set_caption("Work in progress")
clock = pygame.time.Clock()
font = pygame.font.SysFont("PressStart2P.ttf", 30, False)
tiles = pygame.image.load("Tile_set.png")
tiles1 = pygame.image.load("Tile_set1.png")

background = [
    pygame.image.load("background.png").convert(),
    pygame.image.load("background1.png").convert(),
]
black = 0, 0, 0
blue = 0, 0, 255


class Player(object):
    """The controllable player in game"""

    def __init__(self, x, y, width, height, speed):
        self.x, self.y = x, y
        self.width, self.height = width, height
        self.image = pygame.image.load("sprite_sheet.png").convert_alpha()
        self.speed = speed
        self.timer = 0
        self.frames = 1
        self.animation_direction = 2
        self.rect = self.image.subsurface(0, 0, 28, 45)
        self.interact = pygame.Rect(self.x, self.y, 10, 10)
        self.shadow = tiles.subsurface(363, 468, 31, 19)
        self.pos_x, self.pos_y = 105, 77

    def animation(self):
        x_coord = 50 * self.frames
        y_coord = 50 * self.animation_direction
        self.character = self.image.subsurface(x_coord, y_coord, self.width, self.height).convert_alpha()
        self.timer += 0
        if self.timer >= 10:
            self.timer = 0
            self.frames += 1
            if self.frames >= 9:
                self.frames = 1

    def movement(self,):
        self.keys = pygame.key.get_pressed()
        if self.keys[pygame.K_LEFT] or self.keys[pygame.K_a]:
            self.x -= self.speed
            self.pos_x += player.speed
            self.animation_direction = 1
            self.timer += 2
        if self.keys[pygame.K_RIGHT] or self.keys[pygame.K_d]:
            self.x += self.speed
            self.pos_x -= player.speed
            self.animation_direction = 3
            self.timer += 2
        if self.keys[pygame.K_UP] or self.keys[pygame.K_w]:
            self.y -= self.speed
            self.pos_y += player.speed
            self.animation_direction = 0
            self.timer += 2
        if self.keys[pygame.K_DOWN] or self.keys[pygame.K_s]:
            self.y += self.speed
            self.pos_y -= player.speed
            self.animation_direction = 2
            self.timer += 2

    def interaction(self):
        if self.keys[pygame.K_e] or self.keys[pygame.K_x]:
            if self.animation_direction == 1:
                self.interact = pygame.Rect(self.x - 15, self.y, 30, 40)
            if self.animation_direction == 3:
                self.interact = pygame.Rect(self.x + 15, self.y, 30, 40)
            if self.animation_direction == 0:
                self.interact = pygame.Rect(self.x, self.y - 15, 30, 40)
            if self.animation_direction == 2:
                self.interact = pygame.Rect(self.x, self.y + 15, 30, 40)

    def draw(self):
        world.blit(self.shadow, (self.x, self.y + 35))
        world.blit(self.character, (self.x, self.y))


player = Player(375, 275, 30, 50, 3.5)



class Door:
    def __init__(self, x, y, width, height):
        self.x, self.y = x, y
        self.width, self.height = width, height
        self.image = tiles.subsurface(pygame.Rect(328, 432, width, height))
        # self.rect = self.image.subsurface(0, 0, 32, 32)

    def draw(self):
        world.blit(self.image, (self.x, self.y))


door = Door(368, 35, 32, 64)


class Tree:
    def __init__(self, x, y, width, height):
        self.x, self.y, self.width, self.height = x, y, width, height
        self.image = (pygame.transform.scale(tiles.subsurface(pygame.Rect(99, 147, self.width, self.height)), (62, 82)))

    def amount(self):
        for y in range(0, 1650, 50):
            world.blit(self.image, (0, y))
        for y in range(0, 1650, 50):
            world.blit(self.image, (45, y))
        for y in range(0, 1650, 50):
            world.blit(self.image, (90, y))

    def draw(self):
        world.blit(self.image, (self.x, self.y))


tree = Tree(230, 35, 48, 58)

door_list = [
    pygame.Rect(door.x, door.y, door.width, door.height),
]
sprite_list = [
    pygame.Rect(tree.x, tree.y, tree.width, tree.height),
    pygame.Rect(0, 0, 1000, 1),
    pygame.Rect(0, 0, 1, 1000),
    pygame.Rect(800, 0, 1, 1000),
    pygame.Rect(0, 600, 1000, 1),
    pygame.Rect(0, 0, 800, 64)
    # grass.rect.get_rect(topleft=(grass.x, grass.y)),
]

tree_list = [pygame.Rect(tree.x, tree.y, tree.width, tree.height),
             ]


class Scene:
    def __init__(self):
        self.sceneNum = 1

    def Scene_1(self):
        window.fill(black)
        window.blit(world, (player.pos_x, player.pos_y))
        world.blit(background[0], (0, 0))
        player_pos = (player.x, player.y)
        world_pos = (player.pos_x, player.pos_y)
        player.movement()
        player.interaction()

        player_rect = pygame.Rect(player.x, player.y, player.width, player.height)

        for tile in sprite_list:
            if player_rect.colliderect(tile):
                (player.x, player.y) = player_pos
                (player.pos_x, player.pos_y) = world_pos
                print("hit!")
                player.frames = 0

        for doors in door_list:
            if player.interact.colliderect(doors):
                print("Interaction is working")
                scenes.sceneNum += 1

        for trees in tree_list:
            if player.interact.colliderect(trees):
                print("Tree!")

        door.draw()
        # Sprites.grass.draw()
        tree.draw()
        player.animation()
        player.draw()

    def Scene_2(self):
        window.fill(black)
        window.blit(world, (player.pos_x, player.pos_y))
        world.blit(background[1], (0,0))

        player_pos = (player.x, player.y)
        world_pos = (player.pos_x, player.pos_y)
        player.x, player.y = 718, 1223
        player.pos_x, player.pos_y = -238,-871

        player.movement()
        player.interaction()

        player_rect = pygame.Rect(player.x, player.y, player.width, player.height)
        for trees in tree_list:
            if player_rect.colliderect(trees):
                (player.x, player.y) = player_pos
                (player.pos_x, player.pos_y) = world_pos

                print("Tree!")
                player.frames = 0
        tree.amount()
        player.animation()
        player.draw()

    def Draw_Scene(self):
        if self.sceneNum == 1:
            self.Scene_1()
        elif self.sceneNum == 2:
            self.Scene_2()


scenes = Scene()

running = True
while running:
    start_time = time.time()
    scenes.Draw_Scene()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == VIDEORESIZE:
            screen = pygame.display.set_mode((event.w, event.h), RESIZABLE)
        elif event.type == pygame.KEYUP:
            if player.keys:
                player.frames = 0
            if event.key == pygame.K_e or pygame.K_x:
                player.interact = pygame.Rect(-10, -10, 5, 5)
    print(player.x,player.y,player.pos_y,player.pos_x)
    (clock.tick(60))
    text = font.render("FPS " + str(int(1.0 / (time.time() - start_time))), True, blue)
    window.blit(text, (30, 20))
    pygame.display.flip()

我不知道這是否有幫助,但是將循環分成其他循環,當你想改變場景時,打破第一個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM