Programmieren – die besten Beiträge

Wie kann ich mehrere Münzen in PyGame einsammeln?

Hallo,

ich habe eine Frage zu meinem Programm.

Nachdem ich die dritte Münze in meinem Spiel eingesammelt habe, wird komischerweise die zweite Münze anstatt der Dritten gespawnt. Wie kann ich das ändern? Bzw. hat jemand eine Idee, wie sich das beheben lässt?

Hier ist mein Code:

from random import randint

WIDTH = 600
HEIGHT = 600
score = 0
game_over = False

mario = Actor("mario.png")
mario.pos = 100, 100

coin = Actor("money.png")
coin.pos = 0, 0

def draw():
  screen.fill("green")
  mario.draw()
  coin.draw()
  screen.draw.text("Punkte: " + str(score), color="black", topleft=(10, 10))

  if game_over:
    screen.fill("pink")
    screen.draw.text("Endstand:" + str(score), topleft=(10, 10), fontsize = 60)

def place_coin():
  coin.x = 100
  coin.y = 200

def place_coin2():
  coin.x = 100
  coin.y = 300

def place_coin3():
  coin.x = 100
  coin.y = 400

def place_coin4():
  coin.x = 100
  coin.y = 500

def place_coin5():
  coin.x = 300
  coin.y = 100

def place_coin6():
  coin.x = 300
  coin.y = 300

def place_coin7():
  coin.x = 500
  coin.y = 200

def place_coin8():
  coin.x = 500
  coin.y = 300

def place_coin9():
  coin.x = 500
  coin.y = 400

def place_coin10():
  coin.x = 500
  coin.y = 500

def place_coin11():
  coin.x = 700
  coin.y = 100

def place_coin12():
  coin.x = 700
  coin.y = 300

def time_up():
  global game_over
  game_over = True

def update():
  global score

  if keyboard.left:
    mario.x = mario.x - 4
  elif keyboard.right:
    mario.x = mario.x + 4
  elif keyboard.up:
    mario.y = mario.y - 4
  elif keyboard.down:
    mario.y = mario.y + 4

  coin_collectet = mario.colliderect(coin)

  if coin_collectet:
    score = score + 10
    place_coin2()

  coin_collectet = mario.colliderect(coin)

  if coin_collectet:
    score = score + 10
    place_coin10()

  coin_collectet = mario.colliderect(coin)

  if coin_collectet:
    score = score + 10
    place_coin4()

  coin_collectet = mario.colliderect(coin)

  if coin_collectet:
    score = score + 10
    place_coin8()

clock.schedule(time_up, 20.0)
place_coin()
programmieren, Pygame

AttributeError: 'PhotoImage' object has no attribute 'shape'?

Ich bin derzeit dabei mit tkinter, opencv und mit Media Pipe Framework zu arbeiten. Dabei möchte ich Bilder Hochladen können und die hochgeladenen Bilder soll mithilfe von Mediapipe die Hand Landmarks erfassen. Mit Hand Landmarks meine ich alle 21 Positionen eines Hand zu erkennen (Hier findet ihr mehr Infos dazu: https://google.github.io/mediapipe/solutions/hands.html ). Leider bekomme ich mit meiner erstellten Implementierung folgende Fehlermeldung:

INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/bj/projects/pro1/pictureMp.py", line 31, in imageLandmarks
    height, width, _ = image.shape
AttributeError: 'PhotoImage' object has no attribute 'shape'

Was kann ich dagegen machen? Hier ist mein aktueller Code:

def imageLandmarks():
    global panelA
    with mpHands.Hands(static_image_mode=True, max_num_hands=2, min_detection_confidence=0.5) as hands:
        select_image.path = filedialog.askopenfilename()
        filename = os.path.basename(select_image.path)

        if len(select_image.path) > 0:
            image = cv2.imread(select_image.path)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(image)
            image = ImageTk.PhotoImage(image)

            height, width, _ = image.shape
            results = hands.process(image)

            num_cords = 21
            landmarks = ['class']
            for val in range(1, num_cords + 1):
                landmarks += ['x{}'.format(val), 'y{}'.format(val), 'z{}'.format(val)]

                if results.multi_hand_landmarks:
                    for num, hand in enumerate(results.multi_hand_landmarks):
                        mpDraw.draw_landmarks(image, hand, mpHands.HAND_CONNECTIONS)
programmieren, Python, opencv, Tkinter

Meistgelesene Beiträge zum Thema Programmieren