[英]Restart function in my python arcade game doesn´t work properly
I´m currently working on a school project, which is a game with Python Arcade.我目前正在做一个学校项目,这是一个 Python Arcade 游戏。 It is my first coding experience so I´m still a beginner.这是我第一次编码经验,所以我仍然是初学者。 Unfortunately the projekt is due in a few days, which is why I would really appreciate some help!不幸的是,项目将在几天后到期,这就是为什么我非常感谢您的帮助!
The basic structure of the game (it´sa jump´n run) is already working and I just need to add a more platforms and enemys, but while trying to make a restart function, I have encountered a problem.游戏的基本结构 (it'sa jump'n run) 已经可以运行了,我只需要添加更多平台和敌人,但是在尝试重新启动功能时,我遇到了问题。 After the game is won or over, it is supposed to restart.比赛获胜或结束后,应该重新开始。 Resetting the player and enemys works, but the game is stuck then and it isn´t possible to move the player, nor are the enemys moving.重置玩家和敌人是有效的,但游戏会卡住,玩家无法移动,敌人也无法移动。 I suppose that after restarting, the game doesn´t call the update function for some reason, but that is just my assumption and I honestly don´t know what the problem is.我想在重新启动后,由于某种原因游戏不会调用更新功能,但这只是我的假设,老实说我不知道问题是什么。
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()
您有一个setup
功能,您还应该有一个拆卸功能,可以在您的代码中的特定条件下重新启动游戏
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.