我目前正在研究一个学校项目,这是一个使用Python Arcade进行的游戏。这是我的第一次编码经验,所以我还是一个初学者。不幸的是,项目将于几天后到期,这就是为什么我会非常感谢您的帮助!
游戏的基本结构(一次跳跃运行)已经在起作用,我只需要添加更多的平台和敌人,但是在尝试重新启动功能时,我遇到了一个问题。游戏获胜或结束后,应该重新开始。重置玩家和敌人是可行的,但随后游戏被卡住,无法移动玩家,也无法移动敌人。我想重启后,游戏由于某种原因没有调用更新功能,但这只是我的假设,老实说我不知道问题出在哪里。
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 :(得分:0)
您有一个setup
函数,还应该有一个拆除函数,该函数可以在代码中的特定条件下重新启动游戏