Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • udwxd/pinball-project
  • uxowp/pinball-project
2 results
Show changes
Commits on Source (1)
import pygame
from pathlib import Path
# Initialize PyGame
pygame.init()
# Initial window size
s_width = 600
s_height = 800
# Define spacetime
GRAVITY_X = 0.0
GRAVITY_Y = 0.3
DT = 1 # ms (discretization of time)
# Making display screen
screen = pygame.display.set_mode((s_width, s_height), pygame.RESIZABLE)
bg_orig=pygame.image.load(Path(__file__).parents[0] / Path("bkg.jpg")).convert()
clock = pygame.time.Clock()
# Setup
running = True
# You could declare components (the initial ball, the other items, ...) here
ball_x = 100
ball_y = 150
ball_vx = 0
ball_vy = 0
ball_radius = 30
ball2_x = 200
ball2_y = 150
ball2_vx = 0
ball2_vy = 0
ball2_radius = 40
# Main event loop
while running:
for event in pygame.event.get():
# Get's all the user action (keyboard, mouse, joysticks, ...)
continue
# Adjust screen
s_width, s_height = screen.get_width(), screen.get_height()
bg = pygame.transform.scale(bg_orig, (s_width, s_height))
screen.blit(bg, (0, 0)) # redraws background image
# Here the action could take place
# s = s0 + v0*t + 1/2a*t**2
ball_vy = ball_vy + GRAVITY_Y*DT
ball2_vy = ball2_vy + GRAVITY_Y*DT
if ball_y >= screen.get_height():
ball_vy = ball_vy * (-1)
if ball2_y >= screen.get_height():
ball2_vy = ball2_vy * (-1)
ball_y = ball_y + ball_vy*DT + 0.5 * GRAVITY_Y*DT**2
ball2_y = ball2_y + ball2_vy*DT + 0.5 * GRAVITY_Y*DT**2
pygame.draw.circle(screen, (35, 161, 224), [ball_x,ball_y] , ball_radius)
pygame.draw.circle(screen, (35, 161, 224), [ball2_x,ball2_y] , ball2_radius)
pygame.display.flip() # Update the display of the full screen
clock.tick(60) # 60 frames per second
def apply_gravity(vy, gravity, dt):
return vy + gravity * dt
# collision.py
def check_wall_collision(y, vy, screen_height):
if y >= screen_height:
return -vy
return vy
# abortion.py
def check_abortion_condition(y, hole_radius, screen_height):
return y >= screen_height + hole_radius
# elements_collision.py
def check_circle_collision(x, y, vx, vy, radius, circle_x, circle_y, circle_radius, gravity, dt):
# Implementieren Sie die Logik für Kollisionen mit einem Kreis
# Passen Sie die Ballbewegung entsprechend an
pass
def check_rectangle_collision(x, y, vx, vy, radius, rectangle_x, rectangle_y, rectangle_width, rectangle_height, gravity, dt):
# Implementieren Sie die Logik für Kollisionen mit einem Rechteck
pass
def check_moving_element_collision(x, y, vx, vy, radius, moving_element_x, moving_element_y, moving_element_speed, gravity, dt):
# Implementieren Sie die Logik für Kollisionen mit einem sich bewegenden Element
pass
# flipper.py
class Flipper:
def __init__(self):
# Implementieren Sie die Logik für die Steuerung des Flippers
pass
# ball_spawning.py
def spawn_new_ball(game, event):
# Implementieren Sie die Funktion zum Erzeugen eines neuen Balls bei bestimmten Ereignissen
pass
# points_highscore.py
class Game:
def __init__(self):
self.points = 0
self.highscore = 0
def update_points(self, points):
# Implementieren Sie die Logik zur Aktualisierung der Punkte
self.points += points
def update_highscore(self):
# Implementieren Sie die Logik zur Aktualisierung des Highscores
if self.points > self.highscore:
self.highscore = self.points
# main.py
import pygame
from pathlib import Path
from gravitation import apply_gravity
from collision import check_wall_collision
from abortion import check_abortion_condition
from elements_collision import (
check_circle_collision,
check_rectangle_collision,
check_moving_element_collision,
)
from flipper import Flipper
from ball_spawning import spawn_new_ball
from points_highscore import Game
# Restlicher Code bleibt unverändert
# Main event loop
while running:
for event in pygame.event.get():
# Get's all the user action (keyboard, mouse, joysticks, ...)
continue
# Adjust screen
s_width, s_height = screen.get_width(), screen.get_height()
bg = pygame.transform.scale(bg_orig, (s_width, s_height))
screen.blit(bg, (0, 0)) # redraws background image
# Hier könnte die Action stattfinden
ball_vy = apply_gravity(ball_vy, GRAVITY_Y, DT)
ball2_vy = apply_gravity(ball2_vy, GRAVITY_Y, DT)
ball_vy = check_wall_collision(ball_y, ball_vy, s_height)
ball2_vy = check_wall_collision(ball2_y, ball2_vy, s_height)
if check_abortion_condition(ball_y, ball_radius, s_height):
ball_vy = -ball_vy
if check_abortion_condition(ball2_y, ball2_radius, s_height):
ball2_vy = -ball2_vy
# Weitere Kollisionsüberprüfungen hier...
pygame.draw.circle(screen, (35, 161, 224), [ball_x, ball_y], ball_radius)
pygame.draw.circle(screen, (35, 161, 224), [ball2_x, ball2_y], ball2_radius)
pygame.display.flip() # Update the display of the full screen
clock.tick(60) # 60 frames per second
# Done! Time to quit.