I wrote a small football game. It has a player (circle) controlled with the arrows, a ball (also circle) and two goals on the left and on the right side (rects). Until now it was made if I go over the screen the player and the ball appear on the other side. I now need to set a window border: If the ball (and the player go near the border they can't escape the window. For the ball a collision is a possibility too. I tried different things, but they didn't work.
here's some of the things I tried:
def checkballborder():
global ballX
global ballY
if ballX > windowsize[0]:
ballX *= -1
if ballX < windowsize[0]:
ballX *= -1
if ballY > windowsize[1]:
ballY *= -1
if ballY < windowsize[0]:
ballY *= -1
and:
#borders
if ballX < ballradius:
ballX = ballradius
if ballX > windowsize[0] - ballradius:
ballX = windowsize[0] - ballradius
if ballY > ballradius:
ballY = ballradius
if ballY > windowsize[1] - ballradius:
ballY = windowsize[1] - ballradius
here's my whole code:
import pygame, time, sys, math, random
pygame.init()
#Farben bestimmen
red = pygame.color.Color("red")
blue = pygame.color.Color("blue")
white = pygame.color.Color("white")
black = pygame.color.Color("black")
green = pygame.color.Color("green")
beige = pygame.color.Color("#F5F5DC")
#Fenster
width = 200
height = 200
windowsize = [width, height]
fps = 60
window = pygame.display.set_mode (windowsize)
#Uhr
clock = pygame.time.Clock()
#Position Spieler
playerX = random.randrange (0, windowsize[0])
playerY = random.randrange (0, windowsize[1])
#Position Ball
ballX = random.randrange(0, windowsize[0])
ballY = random.randrange(0, windowsize[1])
ballradius = 6
#Mittellinie
midlineA = int (windowsize [0] / 2)
midlineB = int (windowsize [0] / 2)
#Anstoßkreiß
midcircleX = int (windowsize[0] / 2)
midcircleY = int (windowsize[1] / 2)
#Position Tor links
lgoalX = 0
lgoalY = int (windowsize [1] / 2 - 10)
lgoalW = 15
lgoalH = 25
#Position Tor rechts
rgoalX = int (windowsize [0] - 15)
rgoalY = int (windowsize [1] / 2 - 10)
rgoalW = 15
rgoalH = 25
def checkOffWindowX(playerX) :
return playerX % windowsize[0]
def checkOffWindowY(playerY):
return playerY % windowsize[1]
def checkTouching():
global playerX
global ballX
global playerY
global ballY
if -10 < playerY - ballY < 10 and -10 < playerX - ballX < 10:
xDiff = playerX - ballX
yDiff = playerY - ballY
#Spielball verschieben
ballX -= xDiff *2
ballY -= yDiff *2
#Falls der Spielball in einer Ecke sein sollte
if ballY == 0:
xDiff -= 5
elif ballX == windowsize[0]:
xDiff += 5
if ballY == windowsize[1]:
yDiff += 5
elif ballY == windowsize[1]:
yDiff +=5
done = False
while not done:
window.fill(green)
#Bewegung Spieler
keys = pygame.key.get_pressed()
if keys [pygame.K_UP]:
playerY -= 1
if keys [pygame.K_DOWN]:
playerY += 1
if keys [pygame.K_LEFT]:
playerX -= 1
if keys[pygame.K_RIGHT]:
playerX += 1
#Positionsbestimmung
playerX = checkOffWindowX(playerX)
playerY = checkOffWindowY(playerY)
#Auf Berührung testen
checkTouching()
#borders
if ballX < ballradius:
ballX = ballradius
if ballX > windowsize[0] - ballradius:
ballX = windowsize[0] - ballradius
if ballY > ballradius:
ballY = ballradius
if ballY > windowsize[1] - ballradius:
ballY = windowsize[1] - ballradius
#Anstoßkreis zeichnen
pygame.draw.circle (window, white, [midcircleX,midcircleY], 20, 3)
#Mittelpunkt zeichnen
pygame.draw.circle (window, white, [midcircleX,midcircleY], 5)
#Mittellinie zeichnen
pygame.draw.line (window, white, (midlineA , 0), (midlineB,200),4)
#Spieler zeichnen
pygame.draw.circle (window, red, [playerX, playerY], 8)
#Spielball zeichnen
pygame.draw.circle (window, blue, [ballX,ballY], ballradius)
#####~Toren zeichnen und Torerkennung~#######################################
#
#Tor links zeichnen #
pygame.draw.rect(window, white, (lgoalX, lgoalY, lgoalW, lgoalH)) #
#Tor rechts zeichnen #
pygame.draw.rect(window, white, (rgoalX, rgoalY, rgoalW, rgoalH)) #
#
#
#Rechte Tor #
distancer = int(math.hypot(ballX - rgoalX, ballY - rgoalY)) #
#Linke Tor #
distancel = int(math.hypot(ballX - lgoalX, ballY - lgoalY)) #
#
if distancer < 20 >= 0: #
done = True #
if distancel < 20 >= 0: #
done = True #
#
#if ballX < ballradius: #
# ballX = ballradius #
#
#if ballY < ballradius: #
# ballY = ballradius #
#
#############################################################################
pygame.display.update()
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.quit()
the comments are in german, but if you need something, I can translate it to you
Use a pygame.Rect
object for the bounding box of the object and clamp_ip()
to keep the object in bounds:
For instance:
def ClampToRect(x, y, radius, border_rect):
# define a rectangle by the center of the object and the radius
object_rect = pygame.Rect(x-radius, y-radius, radius*2, radius*2)
# "clamp" to border
# move the rectangle inside the border rectangle
# if the rectangle is not completely inside
object_rect.clamp_ip(border_rect)
# return the new center point of the rectnagle
return object_rect.center
window = pygame.display.set_mode(windowsize)
# [...]
done = False
while not done:
# [...]
# playerX = checkOffWindowX(playerX)
# playerY = checkOffWindowY(playerY)
player_radius = 8
playerX, playerY = ClampToRect(playerX, playerY, player_radius, window.get_rect())
# [...]
Do the same for the ball
def checkTouching():
global playerX
global ballX
global playerY
global ballY
if -10 < playerY - ballY < 10 and -10 < playerX - ballX < 10:
xDiff = playerX - ballX
yDiff = playerY - ballY
#Spielball verschieben
ballX -= xDiff *2
ballY -= yDiff *2
ballX, ballY = ClampToRect(ballX, ballY, ballradius, window.get_rect())