Obecnie pracuję nad szkolnym projektem, czyli grą w Python Arcade. To moje pierwsze doświadczenie w programowaniu, więc wciąż jestem początkującym. Niestety projekt ma się pojawić za kilka dni, dlatego bardzo bym wdzięczny za pomoc!

Podstawowa struktura gry (jest to skok i bieg) już działa i muszę tylko dodać więcej platform i wrogów, ale podczas próby uruchomienia funkcji restartu napotkałem problem. Po wygranej lub skończonej grze ma zostać zrestartowana. Zresetowanie gracza i wroga działa, ale gra utknęła w tym momencie i nie jest możliwe przesunięcie gracza, ani też wrogowie się nie poruszają. Przypuszczam, że po ponownym uruchomieniu gra z jakiegoś powodu nie wywołuje funkcji aktualizacji, ale to tylko moje założenie i szczerze mówiąc, nie wiem, w czym jest problem.

import arcade

WIN_WIDTH = 700
WIN_HEIGHT = 400
WIN_TITLE = "Jump and Run"
PLAYER_SPEED = 8
JUMP_SPEED = 17
GRAVITY = 0.8
ENEMY_SPEED = 2
SPRITE_SCALING_PLAYER = 0.25
SPRITE_SCALING_ENEMY = 0.7
SPRITE_SCALING_PLATFORM_LONG = 0.5
SPRITE_SCALING_PLATFORM_MIDDLE = 0.25
SPRITE_SCALING_PLATFORM_SMALL = 0.25
LEFT_MARGIN = 75
RIGHT_MARGIN = 350

INSTRUCTIONS_PAGE_0 = 0
INSTRUCTIONS_PAGE_1 = 1
GAME_RUNNING = 2
GAME_OVER = 3

class Game(arcade.Window):

    def __init__(self):
        super().__init__(WIN_WIDTH, WIN_HEIGHT, WIN_TITLE) 
        self.set_mouse_visible(False) 

        self.platform_list = None 
        self.enemy_list = None 
        self.player_list = None 

        self.player_sprite = None

        self.physics_engine = None


        self.view_bottom = None
        self.view_left = None

        self.background = None
        self.game_over = False
        self.game_won = False

        self.end_of_map_right = 5000
        self.end_of_map_left = 0

        self.current_state = INSTRUCTIONS_PAGE_0

        self.instructions = []
        texture = arcade.load_texture("images/title_screen.png")
        self.instructions.append(texture)

        texture = arcade.load_texture("images/title_screen.png")
        self.instructions.append(texture)

    def setup(self):

        self.platform_list = arcade.SpriteList()
        self.enemy_list = arcade.SpriteList()
        self.player_list = arcade.SpriteList()

        for x in range(0, 1):
            platform = arcade.Sprite("images/platform_long.png", SPRITE_SCALING_PLATFORM_LONG)
            platform.bottom = 0
            platform.left = x
            self.platform_list.append(platform)

        self.player_sprite = arcade.Sprite("images/player1.png", SPRITE_SCALING_PLAYER)
        self.player_list.append(self.player_sprite)
        self.player_sprite.center_x = 100
        self.player_sprite.center_y = 40
        self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
                                                             self.platform_list,
                                                             gravity_constant=GRAVITY)

        self.enemy_sprite = arcade.Sprite("images/enemy.png", SPRITE_SCALING_ENEMY)
        self.enemy_sprite.center_x = 200
        self.enemy_sprite.center_y = 75
        self.enemy_sprite.change_x = ENEMY_SPEED
        self.enemy_list.append(self.enemy_sprite)
        self.enemy_sprite.boundary_right = 200
        self.enemy_sprite.boundary_left = 50

        self.background = arcade.load_texture("images/background.png")

        self.view_left = 0
        self.view_bottom = 0


    def draw_instructions_page(self, page_number):
        page_texture = self.instructions[page_number]
        arcade.draw_texture_rectangle(WIN_WIDTH // 2, WIN_HEIGHT // 2,
                                      page_texture.width,
                                      page_texture.height, page_texture, 0)


    def draw_game_over(self):
        output = "Game Over"
        arcade.draw_text(output, 240, 400, arcade.color.WHITE, 54)

        output = "Click to restart"
        arcade.draw_text(output, 310, 300, arcade.color.WHITE, 24)

    def draw_game(self):
        arcade.draw_texture_rectangle(WIN_WIDTH // 2 + self.view_left, WIN_HEIGHT // 2, WIN_WIDTH, WIN_HEIGHT, self.background) #Hintergrund einzeichnen
        self.player_list.draw()
        self.platform_list.draw()
        self.enemy_list.draw() 

    def on_draw(self):
        arcade.start_render()

        if self.current_state == INSTRUCTIONS_PAGE_0:
            self.draw_instructions_page(0)

        elif self.current_state == INSTRUCTIONS_PAGE_1:
            self.draw_instructions_page(1)

        elif self.current_state == GAME_RUNNING:
            self.draw_game()


        elif self.current_state == GAME_OVER:
            self.draw_game()

    def on_mouse_press(self, x, y, button, modifiers):
        if self.current_state == INSTRUCTIONS_PAGE_0:
            self.current_state = INSTRUCTIONS_PAGE_1
        elif self.current_state == INSTRUCTIONS_PAGE_1:
            self.setup()
            self.current_state = GAME_RUNNING
        elif self.current_state == GAME_OVER:
            self.setup()
            self.current_state = GAME_RUNNING

    def on_key_press(self, key, modifiers):

        if key == arcade.key.SPACE: 
            if self.physics_engine.can_jump(): 
                self.player_sprite.change_y = JUMP_SPEED 
        elif key == arcade.key.A: 
            self.player_sprite.change_x = -PLAYER_SPEED 
        elif key == arcade.key.D: 
            self.player_sprite.change_x = PLAYER_SPEED


    def on_key_release(self, key, modifiers):

        if key == arcade.key.A or key == arcade.key.D: 
            self.player_sprite.change_x = 0 


    def update(self, delta_time):

        if self.current_state == GAME_RUNNING:

            if  self.player_sprite.center_y <= -100 or len(arcade.check_for_collision_with_list(self.player_sprite, self.enemy_list)) > 0: 
                self.game_over = True

            self.enemy_list.update() 
            self.physics_engine.update() 
            if self.player_sprite.right >= self.end_of_map_right:
                self.game_won = True

            for self.enemy_sprite in self.enemy_list:
                if len(arcade.check_for_collision_with_list(self.enemy_sprite, self.player_list)) > 0:
                    self.enemy_sprite.change_x *=-1
                elif  self.enemy_sprite.boundary_right is not None and self.enemy_sprite.right > self.enemy_sprite.boundary_right:
                    self.enemy_sprite.change_x = -ENEMY_SPEED
                elif self.enemy_sprite.boundary_left is not None and self.enemy_sprite.left < self.enemy_sprite.boundary_left:
                    self.enemy_sprite.change_x = ENEMY_SPEED

            scrolling = False

            left_boundary = self.view_left + LEFT_MARGIN
            if self.player_sprite.left < left_boundary:
                self.view_left -= left_boundary - self.player_sprite.left
                scrolling = True 

            right_boundary = self.view_left + WIN_WIDTH - RIGHT_MARGIN 
            if self.player_sprite.right > right_boundary: 
                self.view_left += self.player_sprite.right - right_boundary 
                scrolling = True

            if scrolling: 
                self.view_left = int(self.view_left)
                arcade.set_viewport(self.view_left, WIN_WIDTH + self.view_left, self.view_bottom, WIN_HEIGHT + self.view_bottom)

            if self.game_over or self.game_won:
                self.current_state = GAME_OVER
                self.set_mouse_visible(True)


def main():
    window = Game()
    arcade.run()

if __name__ == '__main__':
    main()
0
gollumeye 14 grudzień 2019, 23:35
Gdzie w kodzie ponownie uruchamiasz grę?
 – 
Chris Doyle
14 grudzień 2019, 23:39
W funkcji on_mouse_press... jeśli nastąpi koniec gry, obecny stan powinien zmienić się na game_running, co powinno spowodować ponowne uruchomienie gry
 – 
gollumeye
15 grudzień 2019, 00:29

1 odpowiedź

Masz funkcję setup, powinieneś także mieć funkcję tear down , która ponownie uruchamia grę w określonych warunkach w kodzie

0
maestro.inc 15 grudzień 2019, 00:38