python飞船移动问题?

提问:python飞船移动问题

飞船根据4个方向键上下左右移动,且移动到边框时从对面出来,比如移动到最右边然后从最左边出来
但是现在问题是左右移动没问题但是上下移动就实现不了,就比如移动到最上面时飞船不会从下面出来,而是一直往上就消失了,想了半天实在没找到问题所在,求大神解答

图片

图片说明

代码

"""
    项目: 飞船外星人游戏
    作者: 硕
    日期:  2019/7/13
    版本: v_1.0
"""


import pygame

from pygame.sprite import Sprite

import sys

class setting:
    """
       系统设置类:

       背景长宽 背景颜色 飞船速度

    """

    def __init__(self):

        self.screen_width = 1200

        self.screen_length = 800

        self.ship_speed_factor = 3

        self.bg_color = (230, 230, 230)

        self.bullet_speed_factor = 1

        self.bullet_width = 3

        self.bullet_height = 15

        self.bullet_color = 60, 60, 60

class Ship:

    def __init__(self, screen, setting):

        self.screen = screen

        self.image = pygame.image.load("images/ship.bmp")

        self.rect = self.image.get_rect()

        self.screen_rect = screen.get_rect()

        self.setting = setting

        self.rect.centerx = self.screen_rect.centerx

        self.rect.centery = self.screen_rect.bottom - 100

        self.x = float(self.rect.centerx)   

        self.y = float(self.rect.centery)

        self.moving_right = False

        self.moving_left = False

        self.moving_up = False

        self.moving_down = False

    def blitme(self):

        self.screen.blit(self.image, self.rect)

    def update(self, setting):

        ai_setting = setting

        if self.moving_right:

            if self.rect.right == self.screen_rect.right:

                self.x = 0

                self.x += self.setting.ship_speed_factor

            else:

                self.x += self.setting.ship_speed_factor

        elif self.moving_left:

            if self.rect.left == self.screen_rect.left:

                self.x = ai_setting.screen_width

                self.x -= self.setting.ship_speed_factor

            else:

                self.x -= self.setting.ship_speed_factor

        elif self.moving_up:

            if self.rect.top == self.screen_rect.top:

                self.y = ai_setting.screen_length

                self.y -= self.setting.ship_speed_factor
            else:

                self.y -= self.setting.ship_speed_factor

        elif self.moving_down:

            if self.rect.bottom == self.screen_rect.bottom:

                self.y = 0

                self.y += self.setting.ship_speed_factor
            else:

                self.y += self.setting.ship_speed_factor

        self.rect.centerx = self.x

        self.rect.centery = self.y

def check_keydown_events(event, ship):

    if event.key == pygame.K_RIGHT:

        ship.moving_right = True

    elif event.key == pygame.K_LEFT:

        ship.moving_left = True

    elif event.key == pygame.K_UP:

        ship.moving_up = True

    elif event.key == pygame.K_DOWN:

        ship.moving_down = True

def check_keyup_events(event, ship):

    if event.key == pygame.K_RIGHT:

        ship.moving_right = False

    elif event.key == pygame.K_LEFT:

        ship.moving_left = False

    elif event.key == pygame.K_UP:

        ship.moving_up = False

    elif event.key == pygame.K_DOWN:

        ship.moving_down = False

def check_events(ship):

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            sys.exit()

        elif event.type == pygame.KEYDOWN:

            check_keydown_events(event, ship)

        elif event.type == pygame.KEYUP:

            check_keyup_events(event, ship)

def update_events(screen, ship, setting):

    screen.fill(setting.bg_color)

    ship.blitme()

    pygame.display.flip()


def run_game():

    pygame.init()

    screen = pygame.display.set_mode((1200, 800))

    pygame.display.set_caption("Alien Invasion")

    ai_setting = setting()

    ship = Ship(screen,ai_setting)

    while True:

        check_events(ship)

        update_events(screen, ship, ai_setting)

        ship.update(ai_setting)

def main():

    run_game()

if __name__ == '__main__':

    main()

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问