我正在为我的班子开展各种女运动员游戏,并且主要工作。 然而,当我试图编造一个“GAME”栏目,以在最后生命丧失时创建“GAME”旗帜时,除了在屏幕上制作的文字外,一切都可行。 所有其他工作都是很短的拖延,才能回到比赛主菜园。
import random
import math
import time
import arcade
import os
from typing import cast
import arcade.gui
INITIAL_ASTEROID_COUNT = 3
SCALE = 0.5
BOUNDARY_PADDING = 300
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
SCREEN_TITLE = "Asteroid Smasher"
LEFT_BOUNDARY = -BOUNDARY_PADDING
RIGHT_BOUNDARY = WINDOW_WIDTH + BOUNDARY_PADDING
BOTTOM_BOUNDARY = -BOUNDARY_PADDING
TOP_BOUNDARY = WINDOW_HEIGHT + BOUNDARY_PADDING
MENU = "MENU"
GAME = "GAME"
HOW_TO = "HOW_TO"
GAME_OVER = "GAME_OVER"
class MyGame(arcade.Window):
"""Main application class"""
def __init__(self):
"""Initialize game window"""
super().__init__(WINDOW_WIDTH, WINDOW_HEIGHT, SCREEN_TITLE)
self.scene = MENU
self.background = arcade.load_texture("Resources/Background/stars.png")
self.uimanager = arcade.gui.UIManager()
self.uimanager.enable()
self.v_box = arcade.gui.UIBoxLayout(space_between=20)
button_style = {
"font_name": ("Algerian"),
"font_size": 25,
"font_color": (61, 212, 45),
"bg_color": (0, 0, 0)
}
start_button = arcade.gui.UIFlatButton(text="New Game", width=300, style=button_style)
start_button.on_click = self.on_click_start
self.uimanager.add(
arcade.gui.UIAnchorWidget(
anchor_x= center_x ,
anchor_y= center_y ,
child=start_button
)
)
how_to_button = arcade.gui.UIFlatButton(text="How To Play", width=300, style=button_style)
how_to_button.on_click = self.how_to_play
self.uimanager.add(
arcade.gui.UIAnchorWidget(
anchor_x= center_x ,
anchor_y= center_y ,
child=how_to_button
)
)
self.v_box.add(start_button)
self.v_box.add(how_to_button)
self.uimanager.add(
arcade.gui.UIAnchorWidget(
anchor_x= center_x ,
anchor_y= center_y ,
child=self.v_box
)
)
self.how_to_manager = arcade.gui.UIManager()
self.how_to_manager.enable()
return_button_style = {
"font_name": ("Algerian"),
"font_size": 25,
"font_color": (61, 212, 45),
"bg_color": (0, 0, 0)
}
back_button = arcade.gui.UIFlatButton(text="Back to Menu", width=300, style=return_button_style)
back_button.on_click = self.return_to_menu
self.how_to_manager.add(
arcade.gui.UIAnchorWidget(
anchor_x= left ,
anchor_y= bottom ,
child=back_button
)
)
# Set the working directory (where we expect to find files) to the same
# directory this .py file is in. You can leave this out of your own
# code, but it is needed to easily run the examples using "python -m"
# as mentioned at the top of this program.
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)
self.frame_count = 0
self.game_over = False
# Sprite lists
self.player_sprite_list = arcade.SpriteList()
self.asteroid_list = arcade.SpriteList()
self.bullet_list = arcade.SpriteList()
self.ship_life_list = arcade.SpriteList()
# Set up the player
self.score = 0
self.player_sprite = None
self.lives = 3
# Sounds
self.laser_sound = arcade.load_sound(":resources:sounds/hurt5.wav")
self.hit_sound1 = arcade.load_sound(":resources:sounds/explosion1.wav")
self.hit_sound2 = arcade.load_sound(":resources:sounds/explosion2.wav")
self.hit_sound3 = arcade.load_sound(":resources:sounds/hit1.wav")
self.hit_sound4 = arcade.load_sound(":resources:sounds/hit2.wav")
def on_draw(self):
"""Render screen"""
self.clear()
arcade.start_render()
if self.scene == MENU:
arcade.draw_lrwh_rectangle_textured(0, 0,
WINDOW_WIDTH, WINDOW_HEIGHT,
self.background)
self.uimanager.draw()
elif self.scene == GAME:
arcade.draw_lrwh_rectangle_textured(0, 0,
WINDOW_WIDTH, WINDOW_HEIGHT,
self.background)
self.asteroid_list.draw()
self.ship_life_list.draw()
self.bullet_list.draw()
self.player_sprite_list.draw()
output = f"Score: {self.score}"
arcade.draw_text(output, 10, 70, arcade.color.WHITE, 13)
output = f"Asteroid Count: {len(self.asteroid_list)}"
arcade.draw_text(output, 10, 50, arcade.color.WHITE, 13)
elif self.scene == HOW_TO:
arcade.draw_lrwh_rectangle_textured(0, 0,
WINDOW_WIDTH, WINDOW_HEIGHT,
arcade.load_texture("Resources/Background/howto.png"))
self.how_to_manager.draw()
elif self.scene == GAME_OVER:
text = "Game Over"
arcade.draw_text(text, 250, 300, (61, 212, 45), 30,
400, center , "Algerian", False, False,
center , center )
def on_update(self, x):
"""Update game state"""
self.frame_count += 1
if not self.game_over:
self.asteroid_list.update()
self.bullet_list.update()
self.player_sprite_list.update()
for bullet in self.bullet_list:
asteroids = arcade.check_for_collision_with_list(bullet,
self.asteroid_list)
for asteroid in asteroids:
# expected AsteroidSprite, got Sprite instead
self.split_asteroid(cast(AsteroidSprite, asteroid))
asteroid.remove_from_sprite_lists()
bullet.remove_from_sprite_lists()
# Remove bullet if it goes off-screen
size = max(bullet.width, bullet.height)
if bullet.center_x < 0 - size:
bullet.remove_from_sprite_lists()
if bullet.center_x > WINDOW_WIDTH + size:
bullet.remove_from_sprite_lists()
if bullet.center_y < 0 - size:
bullet.remove_from_sprite_lists()
if bullet.center_y > WINDOW_HEIGHT + size:
bullet.remove_from_sprite_lists()
if not self.player_sprite.respawning:
asteroids = arcade.check_for_collision_with_list(self.player_sprite,
self.asteroid_list)
if len(asteroids) > 0:
if self.lives > 0:
self.lives -= 1
self.player_sprite.respawn()
self.split_asteroid(cast(AsteroidSprite, asteroids[0]))
asteroids[0].remove_from_sprite_lists()
self.ship_life_list.pop().remove_from_sprite_lists()
print("Crash")
else:
self.game_over = True
elif self.game_over:
self.game_over_screen()
def game_over_screen(self):
self.scene = GAME_OVER
time.sleep(10)
self.scene = MENU
self.start_new_game()
def main():
"""Main function to start game"""
window = MyGame()
window.start_new_game()
arcade.run()
if __name__ == "__main__":
main()
以上是我的一些法典,主要是MyGame init, 最新版,摘录和游戏_over_ Screen方法。 我可以分享整个源代码,但不想在这里太多,除非有必要,这样就与这方面工作直接改变的方法相左。 最初,我手提的是从游戏中直接写成的“透视”方法,但把它移到“筛选”上,因为假定这是最初的问题,而且不会改变其运作方式。
目前,在袭击了一名roid子并夺走了最后生命之后,它冻结了拖延的时间,然后是主要菜单检查返回。 任何建议都非常欢迎。