更新: 根据答案,我尝试了一些更改。 我更改了ship.py的一部分
def update(self):
if self.moving_up and self.rect.top < self.screen_rect.top:
self.rect.centery += self.ai_settings.ship_speed_fator
if self.moving_down and self.rect.bottom > 0:
self.rect.centery -= self.ai_settings.ship_speed_factor
# update the rect based on the self.center
self.rect.centery = self.center
进入下一个:
def update(self):
"""based on the moving signal to change the ship's position"""
#update the ship's center value, not the rect
if self.moving_up and self.rect.top < self.screen_rect.top:
self.rect.center += self.ai_settings.ship_speed_fator
if self.moving_down and self.rect.bottom > 0:
self.rect.center -= self.ai_settings.ship_speed_factor
# update the rect based on the self.center
self.rect.centery = self.center
但是仍然无法正常工作。 感觉不好。
我尝试使用与教科书相同的方法来教我做类似的pygame,但现在却堆积如山。该游戏名为Alien Invasion,它是python新手的首次尝试,也是教科书Python Crash Course的项目。
我已经学习了Python速成课程第12章,并输入了教科书的每一行代码来编写自己的代码。我以为我了解他们。 因此,我将游戏转换为地平线版本。但是我失败了。我无法使用上下键来控制我的飞船,有人可以帮助我吗?我通过链接https://github.com/Ruwzy/Python-Crash-Course-Practises/tree/master/PCC_12/Practice_12_5以及下面的代码将其放在github上。
settings.py
class Settings():
"""Store all the classes of the Alien_Invasion's settings"""
def __init__(self):
""" initialize the game's setting"""
# Screen Settings
self.screen_width = 1600
self.screen_height = 800
self.bg_color = (230, 230, 230)
# ship settings
self.ship_speed_factor = 1.5
game_functions.py
import sys
import pygame
def check_keydown_events(event, ship):
"""response to the keydown"""
if 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):
"""response to the keyup"""
if event.key == pygame.K_UP:
ship.moving_up = False
elif event.key == pygame.K_DOWN:
ship.moving_down = False
def check_events(ship):
"""response to the keyboard and the mouse"""
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_screen(ai_settings, screen, ship):
"""update the screen's image, and change to the new screen"""
# Redraw the screen each time starts the loop
screen.fill(ai_settings.bg_color)
ship.blitme()
# show the lastest drawing screen
pygame.display.flip()
ship.py
import pygame
class Ship():
def __init__(self, ai_settings, screen):
""" initialize the ship and set its original position"""
self.screen = screen
self.ai_settings = ai_settings
# load the ship's image and get its rect
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# set every new ship to the center of left
self.rect.center = self.screen_rect.midleft
self.rect.left = self.screen_rect.left
#store float type value in setting center
self.center = float(self.rect.centery)
# moving signal
self.moving_up = False
self.moving_down = False
def update(self):
"""based on the moving signal to change the ship's position"""
#update the ship's center value, not the rect
if self.moving_up and self.rect.top < self.screen_rect.top:
self.rect.centery += self.ai_settings.ship_speed_fator
if self.moving_down and self.rect.bottom > 0:
self.rect.centery -= self.ai_settings.ship_speed_factor
# update the rect based on the self.center
self.rect.centery = self.center
def blitme(self):
""" draw the ship at a certain position"""
self.screen.blit(self.image, self.rect)
alien_invasion.py
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
""" initialize the game and creat a screen"""
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien_Invasion")
# built a ship
ship = Ship(ai_settings, screen)
# Start the game's main loop
while True:
gf.check_events(ship)
ship.update()
gf.update_screen(ai_settings, screen, ship)
run_game()
我无法将船上下移动,希望有人可以帮助我。我真的花了很多时间进行调试。
答案 0 :(得分:1)
问题是由方法update
引起的:
def update(self): if self.moving_up and self.rect.top < self.screen_rect.top: self.rect.centery += self.ai_settings.ship_speed_fator if self.moving_down and self.rect.bottom > 0: self.rect.centery -= self.ai_settings.ship_speed_factor # update the rect based on the self.center self.rect.centery = self.center
在此方法中,self.rect.centery
被更改,但最后被elf.rect.centery = self.center
覆盖。
更改为:
def update(self):
if self.moving_up and self.rect.top < self.screen_rect.top:
self.center += self.ai_settings.ship_speed_fator
if self.moving_down and self.rect.bottom > 0:
self.center -= self.ai_settings.ship_speed_factor
# update the rect based on the self.center
self.rect.centery = round(self.center)