Python pygame精灵到达屏幕底部后不向上移动

Python pygame精灵到达屏幕底部后不向上移动,python,pygame,sprite,Python,Pygame,Sprite,我正在复习Python速成班v2中的一个练习。我复制了外星人舰队上下移动(而不是左右移动)的太空入侵者。现在我正在修改其中的一些部分,以创建另一个游戏,游戏中有一只狗,当它到达屏幕的顶部和底部时,它会上下移动 问题:狗从右中开始,然后向下移动到右下角,但不向上移动。它在那里颤抖着,好像它认为自己已经到达了顶端,然后又很快地向下移动。我已经断断续续地盯着这几个星期了,如果能帮我找出错误,我将不胜感激 下面的代码将dog元素隔离在一个.py文件中。(如果您没有使用IDE,请编辑pygame.QUIT

我正在复习Python速成班v2中的一个练习。我复制了外星人舰队上下移动(而不是左右移动)的太空入侵者。现在我正在修改其中的一些部分,以创建另一个游戏,游戏中有一只狗,当它到达屏幕的顶部和底部时,它会上下移动

问题:狗从右中开始,然后向下移动到右下角,但不向上移动。它在那里颤抖着,好像它认为自己已经到达了顶端,然后又很快地向下移动。我已经断断续续地盯着这几个星期了,如果能帮我找出错误,我将不胜感激

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

您可以从my[Github for PlayBall pygame][1]的images文件夹中获取狗的图像,也可以替换任何96 x 56 px的图像

# -*- 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()
#-*-编码:utf-8-*-
"""
创建于2020年8月31日星期一19:25:26
@作者:Cathig
"""
导入系统
导入pygame
导入pygame.font
课程设置:
“”“用于存储播放捕获的所有设置的类。”“”
定义初始化(自):
“”“初始化游戏的设置。”“”
#屏幕设置
自身屏幕宽度=1200
自身屏幕高度=800
self.bg_color=(20、230、80)
#狗的环境
self.dog_速度=1.0
#狗方向1表示向下-1代表上升。
self.dog_方向=1
班犬:
定义初始(自我,pb游戏):
“”“初始化狗并设置其起始位置。”“”
self.screen=pb_game.screen
self.settings=pb_game.settings
self.screen_rect=pb_game.screen.get_rect()
#加载狗图像并获取其矩形。
self.image=pygame.image.load('images/dog.png')
self.rect=self.image.get_rect()
#启动屏幕右中央的狗。
self.rect.midright=self.screen_rect.midright
#为狗的垂直位置存储一个十进制值。
self.y=float(self.rect.y)
def检查_边(自):
“”“如果狗在屏幕边缘,则返回True。”“”
screen\u rect=self.screen.get\u rect()

如果在
\u update\u dog
方法中self.rect.bottom>=screen\u rect.bottom或self.rect.top,则将方向复制到临时变量,然后在碰到边缘时反转临时变量。更改在下一个循环中丢失。要保留更改,请同时更新设置变量

以下是更新的代码:

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)