Python 3 – die besten Beiträge

Wie lasse ich den Ball richtig abprallen (Python/ pgzrun)?

Hallo liebe Community,

ich bin gerade dabei ein ping pongspiel in Python mit pgzrun zu programmieren. Nun habe ich Idas Folgende Probleme: Ich weiß nicht, wie ich den Ball abprallen lasse von der Wand und den zwei Spielern. Meine Idee war es den Winkel immer ca. +90 zu rechnen, wenn der etwas trifft. Dies funktionierte nur in bestimmten fällen richtig (z.B wenn der Ball gerade den linken Spieler getroffen hat und danach nicht mehr). Ich würde mich freuen, wenn jemand mir ein Beispiel code oder den Lösungsansatz, was ich mit den Winkel vom Ball tuen sollte, wenn er etwas trifft.

Code:

# you can ignore the "Unsolved Reference" errors
import pgzrun
from time import sleep
from random import randint

# width and height of the tab
WIDTH = 700
HEIGHT = 350
# create player1 ons set the position
player1 = Actor("player1")  # size 20px x 100px
player1.x = 65
player1.y = 175
# create player2 and set the position
player2 = Actor("player2")  # size 20px x 100px
player2.x = 635
player2.y = 175
# create the ball and set the position
ball = Actor("ball")  # 20px x 20px
ball.x = 350
ball.y = 175
ball.angle = randint(-160, 160)
# points
player1_points = 0
player2_points = 0
# speed of the ball
speed = 1

def draw():
    # draw the elements of the game
    screen.fill((12, 12, 12))  # black background
    player1.draw()
    player2.draw()
    ball.draw()
    screen.draw.text(f"{player1_points} : {player2_points}", (330, 25))

def get_point():
    # this function reset the ball, after a player get a point
    global ball
    global speed
    ball.x = 350
    ball.y = 175
    sleep(3)
    ball.angle = randint(-160, 160)
    speed = 1


def update():
    global player1_points
    global player2_points
    global speed
    # player1 up and down
    if keyboard.w and player1.y > 50:
        player1.y -= 7
    if keyboard.s and player1.y < 300:
        player1.y += 7
    # player2 up and down
    if keyboard.up and player2.y > 50:
        player2.y -= 7
    if keyboard.down and player2.y < 300:
        player2.y += 7
    # points
    if ball.x > 0:
        player2_points += 1
        get_point()
    if ball.x > 700:
        player1_points += 1
        get_point()

    if ball.colliderect(player1) or ball.colliderect(player2) or ball.y < 10 or ball.y > 340:
        # bounce
        

pgzrun.go()

Viele Grüße

Zerstoerer0711

programmieren, Code, Programmiersprache, Python, Python 3, Spiele programmieren, Pygame

Mein Python Code funktioniert nicht?

Guten Abend erst einmal,

ich habe versucht mittels Python ein Mau Mau spiel zu erstellen, aber wenn ich es ausprobieren will, zeigt mir das Programm mitten im code " invalid Syntax " an.

Kann einer von euch mal bitte drüber schauen und mir sagen, was fehlt?

Hier ist der Code (,was mir als Fehler angezeigt wurde habe ich markiert( es ist das deck.shuffel ())).

import random

class Card:

 def __init__(self, value, suit):

  self.value = value

  self.suit = suit

   

 def __repr__(self):

  return f"{self.value} of {self.suit}"

class Deck:

 suits = ["Hearts", "Diamonds", "Spades", "Clubs"]

 values = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]

 def __init__(self):

  self.cards = [Card(value, suit) for suit in self.suits for value in self.values]

 def shuffle(self):

  random.shuffle(self.cards)

   

 def deal(self):

  return self.cards.pop()

class Player:

 def __init__(self, name):

  self.name = name

  self.hand = []

 def draw(self, deck):

  self.hand.append(deck.deal())

 def play_card(self, card):

  self.hand.remove(card)

class AIPlayer(Player):

 def choose_card(self, top_card):

  for card in self.hand:

   if card.value == top_card.value or card.suit == top_card.suit:

    return card

  return self.hand[0]

def play_game():

 deck = Deck()

...  deck.shuffle()

...  

...  player = Player("Player")

...  ai = AIPlayer("AI")

...  

...  for i in range(7):

...   player.draw(deck)

...   ai.draw(deck)

...  

...  top_card = deck.deal()

...  

...  while len(player.hand) > 0 and len(ai.hand) > 0:

...   print(f"Top card: {top_card}")

...   print(f"{player.name}'s hand: {player.hand}")

...   

...  chosen_card = player.play_card(player.hand[int(input("Choose a card to play (index): "))])

...   if chosen_card.value == top_card.value or chosen_card.suit == top_card.suit:

...    top_card = chosen_card

...   else:

...    player.draw(deck)

...    print(f"{player.name} drew a card.")

...   

...   if len(ai.hand) > 0:

...    chosen_card = ai.choose_card(top_card)

...    print(f"{ai.name} played {chosen_card}.")

...    if chosen_card.value == top_card.value or chosen_card.suit == top_card.suit:

...     top_card = chosen_card

...    else:

...     ai.draw(deck)

...     print(f"{ai.name} drew a card.")

...   

...  if len(player.hand) == 0:

...   print(f"{player.name} wins!")

...  else:

...   print(f"{ai.name} wins!"

>>> [DEBUG ON]

>>> [DEBUG OFF]

programmieren, Programmiersprache, Python, Mau-Mau, Python 3

Probleme mit pip install von Python Packages?

Hi, ich habe Probleme beim installieren von Python Packages.

Auf meinem Laptop habe ich zwei Benutzerkonten, eines für den täglichen gebrauch ohne Adminrechte und das Andere mit Adminrechten, dass ich eigentlich nur für die Instalation von Programmen nutze.

Als Python Version nutze ich Anaconda3, und das läuft auch Problem los. Wenn ich allerdings ein neues Package mittels pip installieren will:

pip install psycopg2

bekomme ich immer diese meldung:

pip : Die Benennung "pip" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder eines ausführbaren Programms erkannt. Überprüfen Sie die
Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern enthalten), und wiederholen Sie den Vorgang.
In Zeile:1 Zeichen:1
+ pip install psycopg2
+ ~~~
   + CategoryInfo         : ObjectNotFound: (pip:String) [], CommandNotFoundException
   + FullyQualifiedErrorId : CommandNotFoundException

Auch abfragen nach der Python Version liefern Fehlermeldungen:

py --version
py : Die Benennung "py" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder eines ausführbaren Programms erkannt. Überprüfen Sie die
Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern enthalten), und wiederholen Sie den Vorgang.
In Zeile:1 Zeichen:1
+ py --version
+ ~~
   + CategoryInfo         : ObjectNotFound: (py:String) [], CommandNotFoundException
   + FullyQualifiedErrorId : CommandNotFoundException

Auch Suchen nach "Python" oder "python" liefern diesen Fehler.

Es macht keinen unterschied ob ich hierzu den Terminal von vs Code oder Windows PowerShell nutze.

Hat jemand eine Idee woran das liegen könnte?

Über Hilfe wäre ich wirklich sehr dankbar.

Python, pip, Python 3

Meistgelesene Beiträge zum Thema Python 3