1

I'm trying to modify the control of the game in the Book "Python Crash Course". I want to control the picture of the spaceship (move it just horizontal). Here's what I found on stackoverflow:

import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
IMG = pg.Surface((120, 90))
IMG.fill((0, 120, 200))
x = 200
y = 300
done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.MOUSEMOTION:
            if event.buttons[0]:  # Left mouse button pressed.
                x += event.rel[0]
                y += event.rel[1]

    screen.fill(BG_COLOR)
    screen.blit(IMG, (x, y))
    pg.display.flip()
    clock.tick(60)
pg.quit()

Here's my attempt to realize it to my programming: What I'm doing wrong? Alien_Invasion.py

import pygame
from settings import Settings
from ship import Ship
import game_functions as gf

def run_game():
    # Initialisiert das Spiel + erstellt ein screen-Objekt
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")
    # Erstellt das Schiff
    ship = Ship(ai_settings,screen)
    # Startet die Hauptschleife des Spiels.
    while gf.check_events(ship):
        ship.update()
        gf.update_screen(ai_settings, screen, ship)
run_game()

game_functions.py

import pygame,time 
import pygame.locals as pyl
clock = pygame.time.Clock()
x=0

def check_events(ship):
    # Lauscht auf Tastaturereignisse.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            return False
        elif event.type == pyl.MOUSEMOTION:
            x = event.rel[0]
            if x>0:
                ship.moving_right=True
            if x<0:
                ship.moving_left=True
    clock.tick(60)
    return True     

def update_screen(ai_settings, screen, ship):
    #Zeichnet den Bildschirm bei jedem Schleifendurchlauf neu
    screen.fill(ai_settings.bg_color)
    ship.blitme()
    # Macht den als letztes gezeichneten Bi1dschirm sichtbar
    pygame.display.flip()

ship.py

import pygame
class Ship():
    def __init__(self,ai_settings,screen):
        self.screen = screen
        self.ai_settings = ai_settings
        # lädt das Bi1d des Schiffes und ruft dessen umgebendes Rechteck ab
        self.image=pygame.image.load('images/ship.bmp')
        self.rect=self.image.get_rect()  
        self.screen_rect= screen.get_rect()
        # Platziert jedes neue Schiff mittig am unteren Bildschirmrand
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
        # speichert einen Kommawert für den Schiffsmittelpunkt
        self.center=float(self.rect.centerx)

        self.moving_right=False
        self.moving_left=False
        
    def update(self):
        #Aktualisiert den Wert für den Mittelpunkt des Schiffs, nicht des Rechtecks
        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
        #Aktualisiert das rect-Objekt auf der Grundlage von self.center
        self.rect.centerx=self.center

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

settings.py

class Settings():
    def __init__(self):
        # Bildschirmeinstellungen
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (80,80,255)
        self.ship_speed_factor = 1.5

Please be so kind an help me to get the mousemovement recognition working... Thanks...

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
jörg
  • 21
  • 2

1 Answers1

1

If you move the mouse left and right, ship.moving_left and ship.moving_right will be set to True. Therefore, the ship moves right and left. In the end, the ship is in its original position.
You must reset the attributes:

x = event.rel[0]
if x > 0:
    ship.moving_right = True
    ship.moving_left = False
if x < 0:
    ship.moving_right = False
    ship.moving_left = True
Rabbid76
  • 202,892
  • 27
  • 131
  • 174