Reputation: 11
update: Based on the answers, I tried some changes. I change the part of 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
into the next:
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
BUT STILL NOT WORKING. Feels bad.
I tried to used the same way of the textbook taught me to make a similar pygame, but now get stacked. The game named Alien Invasion, well-known as a python newbie's first try and the textbook Python Crash Course's project.
I have learned the Python Crash Course Chapter 12 and typed every line of the textbook codes my self. I thought I understand them. So I transfer the game into a horizon version. But I failed. I cannot use the key up and down to control my ship, is there anyone can help me? I put it on my github with the link https://github.com/Ruwzy/Python-Crash-Course-Practises/tree/master/PCC_12/Practice_12_5, and also the codes below.
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()
I cannot move the ship to go up and down, wish someone could help me. I really spent a lot of time on debugging it.
Upvotes: 1
Views: 166
Reputation: 211135
The issue is caused by the method 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
In this method self.rect.centery
is changed, but at the end it is overwritten by elf.rect.centery = self.center
.
Change it to:
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)
Upvotes: 1