Nie moge poruszyc zdjecia w pygame

0

Chce za pomoca przyciskow poruszyc zdjecie w pygame ale dostaje blad. Program sie kompiluje ale po wcisnieciu przycisku ...
"UnboundLocalError: local variable 'plx' referenced before assignment"

W setting.py mam dwie zmienne
plx=10
ply=10

A chce je modyfikowac w main.py

  def events(self):
       for event in pg.event.get():
           if event.type == pg.KEYDOWN:
               if event.key == pg.K_ESCAPE:
                   self.quit()
               if event.key == pygame.K_LEFT:
                   plx = plx -10
               elif event.key == pygame.K_RIGHT:
                   pass
               elif event.key == pygame.K_UP:
                   pass
               elif event.key == pygame.K_DOWN:
                   pass

Jak to innaczej rozwiazac?

0

Jeśli plx i ply to zmienne globalne w setting.py to możesz zrobić

import setting

def funkcja(argumenty):
    setting.plx = setting.plx - 10

albo

from setting import plx
from setting import ply

def funkcja(argumenty):
    plx = plx - 10

albo

from setting import * # wtedy zaimportujesz wszystko z modułu setting

def funkcja(argumenty):
    plx = plx - 10

Poczytaj o zasięgach zmiennych w Pythonie

0

Robiłem juz tak i caly czas tak samo

0

Settings.py

WIDTH = 800
HEIGHT = 1200
TILE = "Gra 2d"
PATH = "gfx\\"
KRATKA = 30
tablica_mapy = []
ilosc_W_mapie =0
dl_x = 20
dl_y = 20

plx = 10
ply = 10
plimg = PATH+"120.jpg

game.py

import pygame
import pygame as pg
from Settings import *

class GAME:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((HEIGHT,WIDTH))
        pg.display.set_caption(TILE)
        self.clock = pg.time.Clock()
        self.load_data()
    def load_data(self):
        f= open("map0","r")
        for linia in f:
            tablica_mapy.append(linia.strip())
        print(tablica_mapy)
        ilosc_W_mapie=(len(tablica_mapy))
        print(ilosc_W_mapie)
        f.close()
    def quit(self):
        pg.QUIT()
    def events(self):
        for event in pg.event.get():
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    self.quit()
                if event.key == pygame.K_LEFT:
                    plx = plx - 10
                elif event.key == pygame.K_RIGHT:
                    pass
                elif event.key == pygame.K_UP:
                    pass
                elif event.key == pygame.K_DOWN:
                    pass
    def play(self):
        self.playing = True
        while self.playing:
            self.draw()
            self.events()
    def gracz(self,x,y):
        image = pygame.image.load(plimg)
        self.screen.blit(image,(x*KRATKA,y*KRATKA))
        #pg.display.update()
    def draw(self):
        x = dl_x
        y = dl_y
        a=0
        for i in range(x):
            for j in range(y):
                if a ==400:
                    a=0
                sciezka = PATH+tablica_mapy[a]+".jpg"
                image = pygame.image.load(sciezka)
                print(sciezka)
                self.screen.blit(image,(i*KRATKA,j*KRATKA))
                a=a+1
                print(a)

        self.gracz(plx,ply)
        pg.display.update()
0

Dopisz global

def funkcja(argumenty):
    global plx
    plx -= 10
0

Dodałem zadziałalo. Dzieki

0

Ustawienia to powinna byc klasa

1 użytkowników online, w tym zalogowanych: 0, gości: 1