【发布时间】:2020-05-06 14:21:57
【问题描述】:
我正在研究外星人入侵项目 1,并尝试编写代码来限制飞船的移动,使其停留在屏幕上。然而,这并没有发生。我可以让程序以零错误运行,但船不会留在屏幕上。专门限制船只移动的部分在def update(self):
任何帮助都会很棒,谢谢大家!
import pygame
class Ship():
def __init__(self, ai_settings, screen):
# initialize the ship and set its starting position
self.screen = screen
self.ai_settings = ai_settings
# load the ship image and get its rect
self.image = pygame.image.load('alien_invasion/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
# start each new ship at the center of the bottom of the screen
self.center = float(self.rect.centerx)
# movement flag
self.moving_right = False
self.moving_left = False
def update(self):
# update the ships position based on the movement flag
# update the ships center value, not the rect
if self.moving_right and self.rect.right < self.screen_rect.right:
self.center += self.ai_settings.ship_speed_factor
if self.moving_left and self.rect.left > 0:
self.center -= self.ai_settings.ship_speed_factor
# update rect object from self.center
self.rect.centerx = self.center
def blitme(self):
# draw ship at current location
self.screen.blit(self.image, self.rect)
【问题讨论】:
标签: python python-3.x visual-studio function pygame