【问题标题】:Python pygame sprite not moving back up after reaching bottom of screenPython pygame精灵在到达屏幕底部后没有向上移动
【发布时间】:2020-09-14 00:49:01
【问题描述】:

我正在重复 Python Crash Course v2 中的一个练习。我复制了太空侵略者,外星舰队上下移动(而不是左右移动)。现在我正在修改其中的部分内容,以创建另一个游戏,其中的狗应该在到达屏幕顶部和底部时上下移动。

问题:狗从右中角开始,然后向下移动到右下角,但没有向上移动。它在下面颤抖,好像它认为它已经到达顶部并迅速向下移动。我已经断断续续地盯着这个看了几个星期,如果能帮我找出我的错误,我真的很感激。

下面的代码将 dog 元素隔离在单个 .py 文件中。 (如果您没有使用 IDE,请编辑 pygame.QUIT 代码,使其正常关闭。)

您可以从我的 [Github for PlayBall pygame][1] 的图像文件夹中获取狗图像。或者您可以替换任何大约 96 x 56 像素的图像。

# -*- coding: utf-8 -*-
"""
Created on Mon Aug 31 19:25:26 2020

@author: Cathig
"""

import sys
import pygame
import pygame.font

class Settings:
    """A class to store all settings for Play Catch."""

    def __init__(self):
        """Initialize the game's settings."""
        # Screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (20, 230, 80)

        # Dog settings
        self.dog_speed = 1.0
        # dog direction of 1 represents down; -1 represents up.
        self.dog_direction = 1

class Dog:
    def __init__(self, pb_game):
        """Initialize the dog and set its starting position."""
        self.screen = pb_game.screen
        self.settings = pb_game.settings
        self.screen_rect = pb_game.screen.get_rect()

        # Load the dog image and get its rect.
        self.image = pygame.image.load('images/dog.png')
        self.rect = self.image.get_rect()

        # Start the dog at the center right of the screen.
        self.rect.midright = self.screen_rect.midright

        # Store a decimal value for the dog's vertical position.
        self.y = float(self.rect.y)

    def check_edges(self):
        """Return True if the dog is at the edge of the screen."""
        screen_rect = self.screen.get_rect()
        if self.rect.bottom >= screen_rect.bottom or self.rect.top <= 0:
            return True

    def update(self, dog_direction):
        """Move the dog down or up."""
        self.y += (self.settings.dog_speed * dog_direction)
        self.rect.y = float(self.y)

    def center_dog(self):
        self.rect.midright = self.screen_rect.midright
        self.y = float(self.rect.y)

    def blitme(self):
        """Draw the dog at its current location."""
        self.screen.blit(self.image, self.rect)

class PlayBall:
    """Overall class to manage game assets and behavior."""

    def __init__(self):
        """Initialize the game, and create game resources."""
        pygame.init()
        self.settings = Settings()

        # Set the window size and title bar text
        # Windowed
        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        # Full screen
        # self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        # self.settings.screen_width = self.screen.get_rect().width
        # self.settings.screen_height = self.screen.get_rect().height
        pygame.display.set_caption("Dog test")

        self.dog = Dog(self)

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            self._check_events()

            self._update_dog()

            self._update_screen()

    def _update_dog(self):
        """Respond appropriately if the dog has reached an edge."""
        dog_direction = self.settings.dog_direction
        if self.dog.check_edges():
            dog_direction *= -1
        self.dog.update(dog_direction)

    def _check_events(self):
        """Respond to key presses and mouse events."""
        # Gracefully exit when 'X' or alt+F4 close the window
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit() # Use with IDE
                # sys.exit() # If not using IDE, use sys.exit()

    def _start_game(self):
        """Start a new game."""
        self.dog.center_dog()

    def _update_screen(self):
        """Update images on the screen, and flip to the new screen."""
        self.screen.fill(self.settings.bg_color)
        self.dog.blitme()

        # Make the most recently drawn screen visible.
        pygame.display.flip()

if __name__ == '__main__':
    # Make a game instance, and run the game.
    pb = PlayBall()
    pb.run_game()

【问题讨论】:

    标签: python pygame sprite


    【解决方案1】:

    _update_dog 方法中,您将方向复制到一个临时变量,然后在命中边缘时反转该临时变量。更改在下一个循环中丢失。要保留更改,请同时更新设置变量。

    这是更新后的代码:

    def _update_dog(self):
        """Respond appropriately if the dog has reached an edge."""
        dog_direction = self.settings.dog_direction
        if self.dog.check_edges():
            dog_direction *= -1
            self.settings.dog_direction *= -1  # need to update settings also
        self.dog.update(dog_direction)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      相关资源
      最近更新 更多