微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

到达屏幕底部后,Python Pygame Sprite不向上移动

如何解决到达屏幕底部后,Python Pygame Sprite不向上移动

我正在研究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()

解决方法

_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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。