1

我目前正在开发 Pong 游戏。游戏的一个方面是显示当前级别。我想要发生的是有 3 个级别。当分数等于 5 时,我想显示“2 级”。当分数等于 10 时,我想显示“Level 3”,然后在 15 时我想显示“You Win!” 现在发生的事情是,只要分数等于 5,级别数就会在每次更新窗口时计数(超快)。我在哪里或如何编写它以使其按我想要的方式运行?为此,我在 Pong 类中创建了一个名为 level_up 的函数。

import arcade
import random

# These are Global constants to use throughout the game
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 300
BALL_RADIUS = 10

PADDLE_WIDTH = 10
PADDLE_HEIGHT = 50
MOVE_AMOUNT = 5

SCORE_HIT = 1
SCORE_MISS = 5
LEVEL_UP = 1


"""Point will identify the x and y coordinates of the ball"""
class Point:
    def __init__(self):
        """The __init__ initializes the x and y coordinates"""
        self.x=float(0)
        self.y=float(0)

"""Velocity will identify the velocity"""
class Velocity:
    def __init__(self):
        """The __init__ initializes the x and y velocity"""
        self.dx=float(0)
        self.dy=float(0)

"""Ball will identify the coordinates and the movement of the ball"""
class Ball:
    def __init__(self):
        """The __init__ will initialize the Point and Velocity class values"""
        self.center=Point()
        #self.center will call the self.x and self.y float values that are found in the Point class.
        self.velocity=Velocity()
        #self.velocity will call the self.dx and self.dy values that are found in the Velocity class.
        self.velocity.dx=2
        self.velocity.dy=2
    def draw(self):
        """This creates the ball"""
        arcade.draw_circle_filled(self.center.x, self.center.y,
                                  BALL_RADIUS, arcade.color.FLUORESCENT_YELLOW)    
   
    def advance(self):
        self.center.x += self.velocity.dx
        self.center.y += self.velocity.dy
        
    def bounce_horizontal(self):
        self.velocity.dx *=-1
        
    def bounce_vertical(self):
        self.velocity.dy *=-1
        
    def restart(self):
        self.center.x=0
        self.center.y=random.uniform(0,SCREEN_HEIGHT)
        self.velocity.dx=random.uniform(1,8)
        self.velocity.dy=random.uniform(0,8)
"""Paddle will represent the paddle"""       
class Paddle: 
    def __init__(self):
        """The __init__ will initialize the location of the paddle"""
        self.center=Point()
        #self.center calls the Point class
        self.center.x=SCREEN_WIDTH
        self.center.y=SCREEN_HEIGHT//2
    def draw(self):
        arcade.draw_rectangle_filled(self.center.x, self.center.y,
                                     PADDLE_WIDTH, PADDLE_HEIGHT, arcade.color.FLUORESCENT_PINK)         
    def move_up(self):
        self.center.y+=MOVE_AMOUNT
        if self.center.y > SCREEN_HEIGHT:
            self.center.y -= MOVE_AMOUNT
    def move_down(self):
        self.center.y-=MOVE_AMOUNT
        if self.center.y < 0:
            self.center.y += MOVE_AMOUNT
    

class Pong(arcade.Window):
    """
    This class handles all the game callbacks and interaction
    It assumes the following classes exist:
        Point
        Velocity
        Ball
        Paddle
    This class will then call the appropriate functions of
    each of the above classes.
    You are welcome to modify anything in this class,
    but should not have to if you don't want to.
    """

    def __init__(self, width, height):
        """
        Sets up the initial conditions of the game
        :param width: Screen width
        :param height: Screen height
        """
        super().__init__(width, height)

        self.ball = Ball()
        self.paddle = Paddle()
        self.score = 0
        self.level = 1
        

        # These are used to see if the user is
        # holding down the arrow keys
        self.holding_left = False
        self.holding_right = False

        arcade.set_background_color(arcade.color.BLACK)

    def on_draw(self):
        """
        Called automatically by the arcade framework.
        Handles the responsiblity of drawing all elements.
        """

        # clear the screen to begin drawing
        arcade.start_render()

        # draw each object
        self.ball.draw()
        self.paddle.draw()

        self.draw_score()
        self.draw_level()

    def draw_score(self):
        """
        Puts the current score on the screen
        """
        score_text = "Score: {}".format(self.score)
        start_x = 190
        start_y = SCREEN_HEIGHT - 20
        arcade.draw_text(score_text, start_x=start_x, start_y=start_y, font_size=12,
                         color=arcade.color.DEEP_SKY_BLUE)
        
    def draw_level(self):
        """Displays the level"""
        level_text = f"LEVEL {self.level}"
        start_x= 175
        start_y=SCREEN_HEIGHT - 40
        arcade.draw_text(level_text, start_x=start_x, start_y=start_y, font_size=20,
                         color=arcade.color.ELECTRIC_GREEN)

    def update(self, delta_time):
        """
        Update each object in the game.
        :param delta_time: tells us how much time has actually elapsed
        """

        # Move the ball forward one element in time
        self.ball.advance()

        # Check to see if keys are being held, and then
        # take appropriate action
        self.check_keys()

        # check for ball at important places
        self.check_miss()
        self.check_hit()
        self.check_bounce()
        
        
        

    def check_hit(self):
        """
        Checks to see if the ball has hit the paddle
        and if so, calls its bounce method.
        :return:
        """
        too_close_x = (PADDLE_WIDTH / 2) + BALL_RADIUS
        too_close_y = (PADDLE_HEIGHT / 2) + BALL_RADIUS

        if (abs(self.ball.center.x - self.paddle.center.x) < too_close_x and
                    abs(self.ball.center.y - self.paddle.center.y) < too_close_y and
                    self.ball.velocity.dx > 0):
            # we are too close and moving right, this is a hit!
            self.ball.bounce_horizontal()
            self.score += SCORE_HIT
            
    def level_up(self):
        if self.score ==5:
            self.level += LEVEL_UP
            
        if self.score ==10:
            self.level += LEVEL_UP
        

    def check_miss(self):
        """
        Checks to see if the ball went past the paddle
        and if so, restarts it.
        """
        if self.ball.center.x > SCREEN_WIDTH:
            # We missed!
            self.score -= SCORE_MISS
            self.ball.restart()

    def check_bounce(self):
        """
        Checks to see if the ball has hit the borders
        of the screen and if so, calls its bounce methods.
        """
        if self.ball.center.x < 0 and self.ball.velocity.dx < 0:
            self.ball.bounce_horizontal()

        if self.ball.center.y < 0 and self.ball.velocity.dy < 0:
            self.ball.bounce_vertical()

        if self.ball.center.y > SCREEN_HEIGHT and self.ball.velocity.dy > 0:
            self.ball.bounce_vertical()

    def check_keys(self):
        """
        Checks to see if the user is holding down an
        arrow key, and if so, takes appropriate action.
        """
        if self.holding_left:
            self.paddle.move_down()

        if self.holding_right:
            self.paddle.move_up()

    def on_key_press(self, key, key_modifiers):
        """
        Called when a key is pressed. Sets the state of
        holding an arrow key.
        :param key: The key that was pressed
        :param key_modifiers: Things like shift, ctrl, etc
        """
        if key == arcade.key.LEFT or key == arcade.key.DOWN:
            self.holding_left = True

        if key == arcade.key.RIGHT or key == arcade.key.UP:
            self.holding_right = True

    def on_key_release(self, key, key_modifiers):
        """
        Called when a key is released. Sets the state of
        the arrow key as being not held anymore.
        :param key: The key that was pressed
        :param key_modifiers: Things like shift, ctrl, etc
        """
        if key == arcade.key.LEFT or key == arcade.key.DOWN:
            self.holding_left = False

        if key == arcade.key.RIGHT or key == arcade.key.UP:
            self.holding_right = False

# Creates the game and starts it going
window = Pong(SCREEN_WIDTH, SCREEN_HEIGHT)
arcade.run()

4

1 回答 1

1

我刚刚回答了我自己的问题,我将 level_up 函数更改为:

def level_up(self):
        if self.score ==5 and self.level==1:
            self.level += LEVEL_UP
            
        if self.score ==10 and self.level==2:
            self.level += LEVEL_UP
于 2021-05-19T03:39:32.240 回答