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
Wie kriegt man Berufserfahrung mit 15?

Hi. Also ich denke, was das Programmieren angeht, bin ich auf Fortgeschrittenen Niveau. Ich hab:

  • Einen HTTP-Server in Python programmiert (Nur mit Built-In Libraries, direkt mit Sockets. Und PHP ist auch implementiert, zumindest das meiste).
  • Meine eigene Wallpaper Engine in C# und Python geschrieben.
  • Viele Algorithmen auf EntwicklerHeld gemacht
  • Viele Landing Pages, Chat-Seiten, Foren und Mobile Apps Programmiert (Mobile Apps Backend immer mit PHP oder Python, Frontend mit HTML, CSS und JS)

Ich kenne und hab auch wissen, in den Folgenden Libraries (Python):

  • BeatifulSoup
  • Sockets
  • Threading/Multiprocessing
  • NumPy
  • pywin32 (Tiefe Kenntnisse in die WinAPI)
  • PyWebView
  • Tkinter
  • Pythonnet
  • PyAutoGUI
  • JSON
  • PIL
  • pystray
  • os/sys

Ich verstehe das HTTP-Protokoll und die REST-API, beide auch relativ gut. Ich hab Basic Wissen in C/C++ (beschäftige mich zurzeit damit aber noch), etwas Fortgeschrittenere Kenntnisse in C# und Fortgeschrittene Kenntnisse in HTML, CSS, JavaScript und Java. Java hab ich vor allem für die SpigotAPI oft verwendet. Auf GitHub hab ich lange nichts mehr hochgeladen, mein Syntax hat sich stark verändert. Aber paar Projekte hab ich dort trotzdem: https://github.com/Fidode07

Ich denke mein Wissen reicht, ob zumindest mal in ein paar Praktikums reinzuschnuppern. Also Geld verdienen, geht anscheinend nicht (Wegen meinem Alter .___.). Aber ich will wenigstens etwas Erfahrung sammeln. Ich weiß aber nicht, wo ich am besten Anfang soll zu suchen. Hat da jemand irgendwelche richtigen Tipps. Also nicht sowas wie "Ja, such dir halt Firmen in deiner Nähe". Das hilft nicht, ich will wissen WIE ich suche.

Am liebsten mach ich btw. Backend mit Python und C#. Danke im Vorraus und lg.

Arbeit, HTML, Webseite, Programmierer, Java, Jugendliche, JavaScript, Cplusplus, C Sharp, developer, Jungs, Programmiersprache, Python

Meistgelesene Beiträge zum Thema Python