1

Hi Im getting this error : "SyntaxError: Invalid Syntax"


& C:/Users/unhackerguard/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/unhackerguard/Documents/Alien invasion/alien_invasion.py"
File "<stdin>", line 1
& C:/Users/unhackerguard/AppData/Local/Programs/Python/Python39/python.exe
"c:/Users/unhackerguard/Documents/Alien invasion/alien_invasion.py"

(there is a carrot under the &) when Y am trying to my code, me and friend have to debug with no solution in site and tried to google to the best of my ability.

import sys
import pygame

# file imports
from settings import Settings
from ship import Ship

class AlienInvasion:
    """Overall class to manage game assets and behavior"""
    
    def __init__(self):
        """Initialize the game, and create game resoures"""
        pygame.init()
        self.settings= Settings()

        self.screen= pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        self.ship= Ship(self)

        # Set Background color.
        self.bg_color= (230,230,230)

    def run_game(self):
        """Start the main loop for the game"""
        while True:
            self._check_events
            self.ship.update
            self._update_screen


    def _check_events(self):
            # responds to keypresses and mouse events.
        for event in pygame.event.get():
            if event.type== pygame.QUIT:
                sys.exit()
            elif event.type== pygame.KEYDOWN:
                if event.key== pygame.K_RIGHT:
                    self.ship.moving_right= True
                elif event.key== pygame.K_LEFT:
                    self.ship.moving_left= True
            elif event.type== pygame.KEYUP:
                if event.key== pygame.K_RIGHT:
                    self.ship.moving_right= False
                elif event.type== pygame.K_LEFT:
                    self.ship.moving_left= False
            print(event)   
            
    def _update_screen(self):
         # Redraw the screen during each pass though the loop
        self.screen.fill(self.bg_color)
        self.ship.blitme()
        """update images on the screen, and flip to the new screen"""
        pygame.display.flip()

if __name__ == '__main__':
    # Make a game instance, and run the game.
    ai = AlienInvasion()
    ai.run_game()```

```import pygame

class Ship:
    """ a class to manage ship."""

    def __init__(self, ai_game):
        """initialize the ship and set its starting positition."""
        self.screen= ai_game.screen
        self.screen_rect= ai_game.screen.get_rect()

        #load the ship image and get its rect.
        self.image= pygame.image.load('C:/Users/unhackerguard/AppData/Local/Programs/Python/Python39/Lib/site-packages/pygame/examples/data/ship.bmp')
        self.rect= self.image.get_rect()

        # start each new ship at the bottom center of the screen.
        self.rect.midbottom= self.screen_rect.midbottom

        #movent flag
        self.moving_right= False
        self.moving_left= False

    def update(self):
        """update the ships posotion based on the movent flag"""
        if self.moving_right:
            self.rect.x+= 1
        
        if self.moving_left:
            self.rect.x-= 1

    def blitme(self):
        """draw the ship at its current location"""
        self.screen.blit(self.image, self.rect)```

```class Settings:
    """ A class to store all the settings for alien invasion"""

    def __init__(self):
        """Initialize the game settings"""
        # Screen Settings
        self.screen_width= 1200
        self.screen_height= 800
        self.bg_color= (230,230,230)
        ```
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Share the full traceback please. –  Jun 08 '21 at 02:06
  • 1
    Are you trying to run the `python.exe` executable while you're already in a Python REPL? It looks like you've got the interpreter trying to interpret the shell command as a line of Python code, based on the limited info you've shown – Silvio Mayolo Jun 08 '21 at 02:43
  • Are you using an IDE? That command line looks more like what an IDE might do than what a human would be likely to write. You may have misconfigured your IDE. – user2357112 Jun 09 '21 at 00:49
  • im using VS code and it shows the error, when I run the file its self, the the program becomes unresponsive immediately – Unhackerguard Jun 09 '21 at 01:10
  • Does this answer your question? [Invalid Syntax error when running python from inside Visual Studio Code](https://stackoverflow.com/questions/51540391/invalid-syntax-error-when-running-python-from-inside-visual-studio-code) – Tomerikoo Jun 27 '21 at 07:47
  • The line starting with '&' is not a valid way to run your code. That is why you are receiving this error, not because of a problem in your Python file. You first need to get your code to run at all, then troubleshoot it. You could have VS Code create a new terminal pane, then run the command above, but without the '&', and maybe with backslashes ``\`` instead of forward slashes `/`. Or try clicking the triangle button at the top of the code window to run your code in a terminal. Then see how your code runs. – Matthias Fripp Jun 27 '21 at 07:51

0 Answers0