提问: 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值