简体   繁体   中英

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

As the title suggests if I change scenes with a new player position it gets stuck because its in the loop I understand why its happening I just don't know how to make it move with its new position, I have tried player.x,player.y = new x and new y but it goes to the location and gets stuck, there is a player class and also a scene class I think thats where the problem lies, I am still a beginner and im surprised I got this far, help is appreciated thank you!

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()

I don't know if this would help, but divide the loop in to other loops, and when you want to change scenes, break the first one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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