0

so I was doing the exercise of python crash course 12-6. which places a ship on the left side of the screen and allows the player to move the ship up and down. Make the ship fire the bullet that travels right across the screen when the player presses the space-bar.

I've tried to print the length of the bullets, the animation runs which means it is recognizing that the button is being pressed and the bullets appear but don't move. The bullet appears but stays frozen. It seems the for loop has something to do with it.

I've tried to compare the Alian Invasion code three times which is explained in the chapter for this exercise, which sound stupid but i don't see anything obviously wrong to me.

I'm new to python and pygame and even to stackoverflow, any help will be highly appreciated.

The below is the main code to run the game.

import pygame
import sys

from sideways_shooter import SidewaysShooter 
from stone import Stone 
from settings import Settings

class SidewaysShooterGame:
    def __init__(self):
        pygame.init()

        self.settings = Settings()
        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Sideways Shooter Game")

        self.sideways_shooter = SidewaysShooter(self)
        self.stones = pygame.sprite.Group()

    def run_game(self):
        while True:
            self._check_events()
            self.sideways_shooter.update()
            self._stones_update()
            self._update_screen()

    def _stones_update(self):
        self.stones.update()
        for stone in self.stones.copy():
            if stone.rect.left >= self.sideways_shooter.screen_rect.right:
                self.stones.remove(stone)

    def _check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)


    def _check_keydown_events(self, event):
        if event.key == pygame.K_UP:
            self.sideways_shooter.moving_up = True
        elif event.key == pygame.K_DOWN:
            self.sideways_shooter.moving_down = True
        elif event.key == pygame.K_q:
            sys.exit()
        elif event.key == pygame.K_SPACE:
            self._throw_stones()

    def _check_keyup_events(self,event):
        if event.key == pygame.K_UP:
            self.sideways_shooter.moving_up = False
        elif event.key == pygame.K_DOWN:
            self.sideways_shooter.moving_down = False

    def _throw_stones(self):
        if len(self.stones) < self.settings.stones_allowed:
            new_stone = Stone(self)
            self.stones.add(new_stone)

    def _update_screen(self):
        self.screen.fill(self.settings.bg_color)
        self.sideways_shooter.blitme()
        for stone in self.stones:
            stone.draw_stone()

        pygame.display.flip()

if __name__ == '__main__':
    ss_game = SidewaysShooterGame()
    ss_game.run_game()

The below are the classes and methods that contains assets and attributes for the main code project.

sideway_shooter file

import pygame
class SidewaysShooter:
    def __init__(self, ss_game):
        self.screen = ss_game.screen
        self.settings = ss_game.settings
        self.screen_rect = ss_game.screen.get_rect()
        self.image = pygame.image.load('images/resized_shooter_bmp_file.bmp')
        self.rect = self.image.get_rect()

        self.rect.midleft = self.screen_rect.midleft

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

        self.moving_up = False
        self.moving_down = False

    def update(self):
        if self.moving_up and self.rect.top > 0:
            self.y -= self.settings.speed
        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.y += self.settings.speed

        self.rect.y = self.y

    def blitme(self):
        self.screen.blit(self.image, self.rect)

stone file

import pygame
from pygame.sprite import Sprite
class Stone(Sprite):

    def __init__(self, ss_game):
        super().__init__()
        self.screen = ss_game.screen
        self.settings = ss_game.settings
        self.color = self.settings.stone_color

        self.rect = pygame.Rect(0, 0, self.settings.stone_width, 
            self.settings.stone_height)
        self.rect.midright = ss_game.sideways_shooter.rect.midright

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

    def upate(self):
        self.x += self.settings.stone_speed
        self.rect.x = self.x

    def draw_stone(self):
        pygame.draw.rect(self.screen, self.color, self.rect)

settings file

class Settings:
    def __init__(self):
        self.screen_width = 1400
        self.screen_height = 700
        self.bg_color = (255, 255, 255)
        self.speed = 1.5
        self.stone_color = (60, 60, 60)
        self.stone_width = 15
        self.stone_height = 3
        self.stone_speed = 1.0
        self.stones_allowed = 3
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Guzal920
  • 21
  • 3
  • 1
    It is a typo. `def upate(self):` has to be `def update(self):` in class `Stone`. (the `d` is missing) – Rabbid76 Jan 18 '20 at 17:57
  • 1
    another typo mistake, it's a good idea to learn how to create a spelling mistake finder program using python. I really thank you for taking the time to refactor my code. – Guzal920 Jan 18 '20 at 18:51

0 Answers0