我是编程新手,希望得到指导/反馈。
下面是一个完整的工作脚本:
我已经设法让玩家精灵由 WASD 控制,小行星精灵现在也在屏幕上渲染,并通过一些物理来移动它。它也应该从墙上反弹,但不会。但由于某种原因,我相信更新函数没有正确调用 Asteroid 类 - 除非它有其他问题。
非常感谢所有帮助和未来的指导!
import arcade
import random
""" Universal variables """
SPRITE_SCALING_PLAYER = 0.5
SPRITE_SCALING_ASTEROID = 0.35
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
MOVEMENT_SPEED = 5
class Player(arcade.Sprite):
# PLAYER
def update(self):
# COLLISION
self.center_x += self.change_x
self.center_y += self.change_y
if self.left < 0:
self.left = 0
elif self.right > SCREEN_WIDTH - 1:
self.right = SCREEN_WIDTH - 1
if self.bottom < 0:
self.bottom = 0
elif self.top > SCREEN_HEIGHT - 1:
self.top = SCREEN_HEIGHT - 1
class Asteroid(arcade.Sprite):
# ASTEROID
def __init__(self, filename, sprite_scaling):
super().__init__(filename, sprite_scaling)
self.change_x = 0
self.change_y = 0
def update(self):
# Move the asteroid
self.center_x += self.change_x
self.center_y += self.change_y
# rebound
if self.left < 0:
self.change_x *= -1
if self.right > SCREEN_WIDTH:
self.change_x *= -1
if self.bottom < 0:
self.change_y *= -1
if self.top > SCREEN_HEIGHT:
self.change_y *= -1
# MAIN GAME CLASS
class MyGame(arcade.Window):
""" Our custom Window Class"""
def __init__(self):
""" Initializer """
# Call the parent class initializer
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Alien")
# Background image will be stored in this variable
self.background = ("space_bg.png")
# Variables that will hold sprite lists
self.all_sprite_list = ["ufo_sprite.png", "asteroid.gif"]
# Set up player
self.player_sprite = self.all_sprite_list[0]
# Set up asteroid
self.asteroid_sprite = self.all_sprite_list[1]
# Don't show the mouse cursor
self.set_mouse_visible(False)
# arcade.set_background_color(arcade.color.BLACK)
def setup(self):
""" Set up the game and initialize the variables. """
# background
self.background = arcade.load_texture(self.background)
# Sprite lists
self.all_sprite_list = arcade.SpriteList()
# Set up the player
self.player_sprite = Player("ufo_sprite.png", SPRITE_SCALING_PLAYER)
self.player_sprite.center_x = (SCREEN_WIDTH * 0.50)
self.player_sprite.center_y = (SCREEN_HEIGHT * 0.50)
self.all_sprite_list.append(self.player_sprite)
# Set up asteroid
self.asteroid_sprite = Asteroid("asteroid.gif", SPRITE_SCALING_ASTEROID)
Asteroid.center_x = random.randrange(SCREEN_WIDTH)
Asteroid.center_y = random.randrange(SCREEN_HEIGHT)
Asteroid.change_x = random.randrange(-4, 4)
Asteroid.change_y = random.randrange(-4, 4)
self.all_sprite_list.append(self.asteroid_sprite)
def on_draw(self):
# needed before other drawn elements
arcade.start_render()
# draw background
arcade.draw_texture_rectangle(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2,
SCREEN_WIDTH, SCREEN_HEIGHT, self.background)
# draw sprites
self.all_sprite_list.draw()
def update(self, delta_time):
""" Movement and game logic """
self.all_sprite_list.update()
def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed. """
if key == arcade.key.W:
self.player_sprite.change_y = MOVEMENT_SPEED
elif key == arcade.key.S:
self.player_sprite.change_y = -MOVEMENT_SPEED
elif key == arcade.key.A:
self.player_sprite.change_x = -MOVEMENT_SPEED
elif key == arcade.key.D:
self.player_sprite.change_x = MOVEMENT_SPEED
# elif key == arcade.key.SPACE:
# self.player_sprite.change_x = MOVEMENT_SPEED
def on_key_release(self, key, modifiers):
"""Called when the user releases a key. """
if key == arcade.key.W or key == arcade.key.S:
self.player_sprite.change_y = 0
elif key == arcade.key.A or key == arcade.key.D:
self.player_sprite.change_x = 0
# elif key == arcade.key.SPACE:
# self.player_sprite.change_y = (SCREEN_HEIGHT * 0.005)
def main():
""" Main method """
window = MyGame()
window.setup()
arcade.run()
arcade.schedule(update, 1 / 80)
if __name__ == "__main__":
main()