How can i render non rectangular part of an image? - python

I'm learning how to create a multiplayer game with pygame by recreating Among-us.
I'm currently trying to recreate the vision system.
To make sure that the vision is blocked by walls, i've seen that a simple method is to place points at each corner of walls and to only draw the inner part of the triangles made between those points and the player.
(I'm aware that i'll have to stop the purple lines when they hit a wall.)
But my issue now is how can i draw the background (its an image of the Among_us map) only on those triangles and shade the rest of the image ?
PS: If you know how to integrate the shape bewteen the triangle and the and of the vision circle(like on the right of the picture) it would help to.
At first i thought about using the area parameter of blit but it only work with rectangles.
Maybe i could draw each pixel one by one but i don't think that it's a great idea.
I hope there is a better way.

To make sure that the vision is blocked by walls, i've seen that a simple method is to place points at each corner of walls and to only draw the inner part of the triangles made between those points and the player.
That's the way to go. Maybe you'll find this tutorial helpful.
But my issue now is how can i draw the background ... only on those triangles and shade the rest of the image ?
Create a black surface, "cut out" the visible area (for example, drawing a polygon with a color key will do), and just draw it on top; like Woodford already said in a comment.
PS: If you know how to integrate the shape bewteen the triangle and the and of the vision circle(like on the right of the picture) it would help to.
Here you can use the same idea. Create a black surface, draw a transparent circle with center = postion of the player and radius = 'max visible distance', and slap it on top.
Here I quickly ported the tutorial I linked to python/pygame:
# A port of https://ncase.me/sight-and-light/ to pygame
import pygame, math
from itertools import cycle,islice
from collections import namedtuple
FPS = 60
SCREEN_SIZE = 800, 600
SCREEN_RECT = pygame.Rect(0, 0, *SCREEN_SIZE)
COLOR_KEY = (12, 23, 34)
Point = namedtuple("Point", "x y")
Line = namedtuple("Ray", "a b")
Intersection = namedtuple("Intersection", "x y param angle", defaults=[0])
class Obstacle(pygame.sprite.Sprite):
def __init__(self, points, *args):
super().__init__(*args)
self.image = pygame.Surface(SCREEN_RECT.size)
self.image.set_colorkey(COLOR_KEY)
self.image.fill(COLOR_KEY)
self.rect = self.image.get_rect()
self.points = points
pygame.draw.polygon(self.image, (30, 255, 30), self.points, 4)
def __iter__(self):
return iter(self.points)
class Actor(pygame.sprite.Sprite):
def __init__(self, *args):
super().__init__(*args)
self.image = pygame.Surface((20, 20))
self.image.fill((255, 30, 30))
self.rect = self.image.get_rect(center=SCREEN_RECT.center)
def get_intersection(ray, segment):
"""Find intersection of RAY & SEGMENT"""
# RAY in parametric: Point + Delta*T1
r_px = ray.a.x
r_py = ray.a.y
r_dx = ray.b.x-ray.a.x
r_dy = ray.b.y-ray.a.y
# SEGMENT in parametric: Point + Delta*T2
s_px = segment.a.x
s_py = segment.a.y
s_dx = segment.b.x-segment.a.x
s_dy = segment.b.y-segment.a.y
# Are they parallel? If so, no intersect
r_mag = math.sqrt(r_dx*r_dx+r_dy*r_dy);
s_mag = math.sqrt(s_dx*s_dx+s_dy*s_dy);
if r_dx/r_mag==s_dx/s_mag and r_dy/r_mag==s_dy/s_mag:
return
try:
# SOLVE FOR T1 & T2
# r_px+r_dx*T1 = s_px+s_dx*T2 && r_py+r_dy*T1 = s_py+s_dy*T2
# ==> T1 = (s_px+s_dx*T2-r_px)/r_dx = (s_py+s_dy*T2-r_py)/r_dy
# ==> s_px*r_dy + s_dx*T2*r_dy - r_px*r_dy = s_py*r_dx + s_dy*T2*r_dx - r_py*r_dx
# ==> T2 = (r_dx*(s_py-r_py) + r_dy*(r_px-s_px))/(s_dx*r_dy - s_dy*r_dx)
T2 = (r_dx*(s_py-r_py) + r_dy*(r_px-s_px))/(s_dx*r_dy - s_dy*r_dx)
T1 = (s_px+s_dx*T2-r_px)/r_dx
# Must be within parametic whatevers for RAY/SEGMENT
if T1<0: return
if T2<0 or T2>1: return
# Return the POINT OF INTERSECTION
return Intersection(r_px+r_dx*T1, r_py+r_dy*T1, T1)
except ZeroDivisionError:
pass
def get_all_angles_from_points(start, points):
angles = []
for p in points:
angle = math.atan2(p[1]-start[1], p[0]-start[0])
angles.append(angle-0.0001)
angles.append(angle)
angles.append(angle+0.0001)
return angles
def get_blocker_surface(intersects):
intersects = sorted(intersects, key=lambda x: x.angle)
as_points = list(map(lambda r: (r.x, r.y), intersects))
blocker_surface = pygame.Surface(SCREEN_SIZE)
blocker_surface.set_colorkey(COLOR_KEY)
pygame.draw.polygon(blocker_surface, COLOR_KEY, as_points)
return blocker_surface
def fov(screen, start, obstacles):
points = set(p for o in obstacles for p in o)
angles = get_all_angles_from_points(start, points)
intersects = []
for angle in angles:
dx = math.cos(angle)
dy = math.sin(angle)
ray = Line(Point(start.x, start.y), Point(start.x + dx, start.y + dy))
closest_intersection = None
for o in obstacles:
connected_points = zip(o.points, islice(cycle(o.points), 1, None))
for segment in connected_points:
intersection = get_intersection(ray, Line._make(segment))
if not intersection: continue
if not closest_intersection or intersection.param < closest_intersection.param:
closest_intersection = intersection
if closest_intersection:
intersects.append(closest_intersection._replace(angle=angle))
return intersects
def main():
pygame.init()
pygame.display.set_caption("FOV demo")
clock = pygame.time.Clock()
screen = pygame.display.set_mode(SCREEN_SIZE)
sprites = pygame.sprite.Group()
obstacle_sprites = pygame.sprite.Group()
font = pygame.font.SysFont("Consolas", 20)
tmprect = SCREEN_RECT.inflate(-2, -2)
data = [
[tmprect.topleft, tmprect.topright, tmprect.bottomright, tmprect.bottomleft],
[(229, 211), (336, 294), (271, 406), (126, 323)],
[(386, 170), (487, 167), (393, 74), (309, 186)],
[(631, 502), (576, 457), (690, 389), (764, 493)],
[(519, 308), (489, 378), (589, 381), (532, 292)],
[(349, 552), (347, 480), (441, 484), (475, 566)],
[(637, 93), (592, 228), (726, 278), (661, 153)],
[(186, 494), (88, 431), (29, 533), (174, 566)],
[(97, 138), (32, 242), (150, 178), (161, 74)]
]
obstacles = [Obstacle([Point._make(t) for t in o], sprites, obstacle_sprites) for o in data]
player = Actor(sprites)
def stay_on_mouse(this):
this.rect.center = pygame.mouse.get_pos()
player.update = lambda *args: stay_on_mouse(player, *args)
do_fov = True
do_debug = False
do_draw_obstacles_on_top = True
fov_alpha = 255
while True:
evs = pygame.event.get()
for e in evs:
if e.type == pygame.QUIT:
return
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_d: do_debug = not do_debug
if e.key == pygame.K_o: do_draw_obstacles_on_top = not do_draw_obstacles_on_top
if e.key == pygame.K_SPACE: do_fov = not do_fov
if e.key == pygame.K_ESCAPE: return
sprites.update()
screen.fill((50, 50, 50))
# draw regular stuff
sprites.draw(screen)
# hide stuff due to FOV
intersects = fov(screen, Point._make(player.rect.center), obstacles)
blocker_surface = get_blocker_surface(intersects)
fov_alpha = min(fov_alpha+5, 255) if do_fov else max(fov_alpha-5, 0)
blocker_surface.set_alpha(fov_alpha)
screen.blit(blocker_surface, (0, 0))
if do_debug:
for i in intersects:
pygame.draw.line(screen, (100, 100, 100), (i.x, i.y), player.rect.center)
if do_draw_obstacles_on_top:
obstacle_sprites.draw(screen)
y = 24
for line in ["[SPACE] - Toggle FOV", "[D] - Draw debug lines", "[O] - Draw obstascles on top"]:
screen.blit(font.render(line, True, (0, 0, 0)), (22, y+2))
screen.blit(font.render(line, True, (255, 255, 255)), (20, y))
y+= 24
pygame.display.flip()
clock.tick(FPS)
main()
And it looks like this:
That should get you started.

Related

How to give a warning when a moving object deviates from a path by a specific margin?

In my pygame-code, I have a drone that is supposed to follow a flight path.
I used pygame.draw.lines to draw lines between specified points. Now, I have a flight path with 10 points where after each point the path angle changes (a bit like a zigzag). The player can move the drone by pressing the keys.
My goal is to print a warning once the drone deviates from the path, e.g. by +/-30. I have been racking my brain for two days but can't come up with a condition to detect a deviation. I just can't figure out how to approach this.
I can determine the drone's x-coordinate at any time but how do I determine the offset from the path? I have attached an image to visualize my problem.
Edit:
As I am a beginner my code is a mess but when copy-pasting it, I guess only the lines 35-91 are interesting. Thank you for any kind of advice in advance!!
import pygame
import pygame.gfxdraw
import random
import sys
import math
pygame.init()
# Define some colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
red_transp = (255,0,0, 150)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
X = 0
Y = 250
#Display
display_width, display_height = 1200, 700
h_width, h_height = display_width/2, display_height/2
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Game Display')
#Drone Sprite Image Load Function
droneImg_interim = pygame.image.load('drone.png')
droneImg = pygame.transform.scale(droneImg_interim, [50,50])
drone_width, drone_height = droneImg.get_rect().size
#Create 11 Waypoints with the same coordinates
p1=[X, Y]
p2=[X, Y]
p3=[X, Y]
p4=[X, Y]
p5=[X, Y]
p6=[X, Y]
p7=[X, Y]
p8=[X, Y]
p9=[X, Y]
p10=[X, Y]
p11=[X, Y]
pointlist = [p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11]
x_min=drone_width
x_max=100
#Setting new x-coordinate for each point
for i in pointlist:
i[0] = random.randrange(x_min, x_max)
x_min+=250
x_max+=250
#Setting new y-coordinate for each point
for i in range(len(pointlist)):
if i == 0:
pointlist[i][1] = random.randrange(200, 400)
else:
prev = pointlist[i-1][1]
pointlist[i][1] = random.randrange(200, prev+100)
#Plotting pointlist on gameDisplay and connecting dots
def flightpath(pointlist):
pygame.draw.lines(gameDisplay, (255, 0, 0), False, pointlist, 2)
def margin(x):
for i in range(len(pointlist)-1):
p1_x = pointlist[i][0]
p2_x = pointlist[i+1][0]
p1_y = pointlist[i][1]
p2_y = pointlist[i+1][1]
distance_x = p2_x - p1_x
distance = math.sqrt((p2_x-p1_x)**2+(p2_y-p1_y)**2)
halfwaypoint_x = math.sqrt((p2_x - p1_x)**2)/2 + p1_x
halfwaypoint_y = math.sqrt((p2_y - p1_y)**2)/2 + p1_y
if p2_y < p1_y:
angle_rad = math.acos(distance_x/distance)
elif p2_y > p1_y:
angle_rad = 0 - math.acos(distance_x/distance)
angle_deg = math.degrees(angle_rad)
rect_width = distance
rect_height = 60
"""
This part of the code is meant for displaying the margins (the rectangles) around the flight path on the display.
marginSize = (rect_width, rect_height)
surface = pygame.Surface(marginSize, pygame.SRCALPHA)
surface.fill((255,0,0,25))
rotated_surface = pygame.transform.rotate(surface, angle_deg)
#new_rect = rotated_surface.get_rect(center = surface.get_rect(center = ((pointlist[i][0], pointlist[i][1]))).center)
new_rect = rotated_surface.get_rect(center = surface.get_rect(center = ((halfwaypoint_x, halfwaypoint_y))).center)
#gameDisplay.blit(rotated_surface, new_rect)
"""
#Placing drone on the screen
def drone(x,y):
rect = droneImg.get_rect ()
rect.center=(x, y)
gameDisplay.blit(droneImg,rect)
def displayMSG(value,ttext,posx,posy):
myFont = pygame.font.SysFont("Verdana", 12)
Label = myFont.render(ttext, 1, black)
Value = myFont.render(str(value), 1, black)
gameDisplay.blit(Label, (posx, posy))
gameDisplay.blit(Value, (posx + 100, posy))
#Main Loop Object
def game_loop():
global X, Y, FThrustX, FThrustY, FDragY, Time
FThrustY = 0
gameExit = False
while not gameExit:
#Event Checker (Keyboard, Mouse, etc.)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed() #checking pressed keys
if keys[pygame.K_LEFT]:
X -= 1
if keys[pygame.K_RIGHT]:
X +=1
if keys[pygame.K_DOWN]:
Y += 1
if keys[pygame.K_UP]:
Y -=1
#Display Background Fill
gameDisplay.fill(white)
#Plot flightpath
flightpath(pointlist)
#YS: Determine the position of the mouse
current_pos_x, current_pos_y = pygame.mouse.get_pos()
displayMSG(current_pos_x,'x:',20,665)
displayMSG(current_pos_y,'y:',20,680)
#Plot margin
margin(5)
#Move Drone Object
drone(X,Y)
#Determine the position of the mouse
current_pos_x, current_pos_y = pygame.mouse.get_pos()
#No exceeding of display edge
if X > display_width - drone_width: X = display_width - drone_width
if Y > display_height - drone_height: Y = display_height - drone_height
if X < drone_width: X = drone_width
if Y < drone_height: Y = drone_height
pygame.display.update()
#MAIN
game_loop()
pygame.quit()
sys.exit()
One approach is to find the minimum distance between the center of the drone and the line.
Write the function that calculates the minimum distance from a point to a line segment. To do this, use pygame.math.Vector2 and the Dot product:
def distance_point_linesegment(pt, l1, l2):
LV = pygame.math.Vector2(l2[0] - l1[0], l2[1] - l1[1])
PV = pygame.math.Vector2(pt[0] - l1[0], pt[1]- l1[1])
dotLP = LV.dot(PV)
if dotLP < 0:
return PV.length()
if dotLP > LV.length_squared():
return pygame.math.Vector2(pt[0] - l2[0], pt[1]- l2[1]).length()
NV = pygame.math.Vector2(l1[1] - l2[1], l2[0] - l1[0])
return abs(NV.normalize().dot(PV))
Find the line segment with the shortest distance in a loop:
def minimum_distance(pt, pointlist):
min_dist = -1
for i in range(len(pointlist)-1):
dist = distance_point_linesegment(pt, pointlist[i], pointlist[i+1])
if i == 0 or dist < min_dist:
min_dist = dist
return min_dist
Create an alert when the distance exceeds a certain threshold:
def game_loop():
# [...]
while not gameExit:
# [...]
dist_to_path = minimum_distance((X, Y), pointlist)
if dist_to_path > 25:
pygame.draw.circle(gameDisplay, (255, 0, 0), (X, Y), 25, 4)
drone(X,Y
# [...]
Another possible solution is to use pygame.Rect.clipline and detect the collision of the line segments and the rectangle surrounding the drone:
def intersect_rect(rect, pointlist):
for i in range(len(pointlist)-1):
if rect.clipline(pointlist[i], pointlist[i+1]):
return True
return False
def game_loop():
# [...]
while not gameExit:
# [...]
if not intersect_rect(droneImg.get_rect(center = (X, Y)), pointlist):
pygame.draw.circle(gameDisplay, (255, 0, 0), (X, Y), 25, 4)
drone(X,Y
# [...]
The interesting part of the question is of course finding the nearest point on the desired path to the actual position; distance is easy. The hard part of that is in turn identifying the nearest element (line segment) of the path; projecting onto it is also straightforward.
If the path is simple enough (in particular, if it doesn’t branch and it’s impossible/disallowed to skip sections at a self-intersection), you can finesse that part by just maintaining that current element in a variable and updating it to the previous or next element when the projection onto one of them is closer than the projection onto the current one. This is a typical algorithm used by racing games to determine the instantaneous order of racers.

Python code problem displaying a polygon in PyGame using a polygon modeled in PyMunk

I am trying to model physics in pymunk and display using pygame and it works well so far with circles and a simple line. The problem I have is that I am trying to model a rectangle.
In pymunk that is a ploygon and I can create it using:-
def create_rect(space, pos):
body = pymunk.Body(1,100,body_type= pymunk.Body.DYNAMIC)
body.position = pos
poly_dims = [(100,10),(200,10),(200,15),(100,15)]
shape = pymunk.Poly(body,poly_dims)
space.add(body,shape)
return shape
I can get the position of the body which starts off falling and print using pygame with:-
pos_x = int(board.body.position.x)
pos_y = int(board.body.position.y)
pygame.draw.circle (screen,(0,0,0),(pos_x,pos_y),10)
But I cannot seem to obtain the moving co-ordinates of the Polygon edges to then print using pygame.
v = board.get_vertices()
print(v)
pygame.draw.polygon(screen,(0,0,0),v,1)
Vertices
[Vec2d(100.0, 10.0), Vec2d(200.0, 10.0), Vec2d(200.0, 15.0), Vec2d(100.0, 15.0)]
Which looks good, however the co-ordinates never change.
Then I tried:-
pos = board.body.position
print('Position')
print(pos)
which outputs
Position
Vec2d(530.5760282186911, 545.8604346347887)
And the position does change, but you cannot print the polygon with a 2D vector, you need all the vertices to do it. And presumably when the polygon hits a surface it will rotate etc. I just want to model a rectangle and I am stuck on this one point !
If you need the vertices in world coordinates then you have to transform the vertices. Follow the example in the Pymunk documentation (see get_vertices()) and write a function that draws a polygon from the transformed vertices:
def drawShape(surf, color, shape):
pts = []
for v in shape.get_vertices():
x, y = v.rotated(shape.body.angle) + shape.body.position
pts.append((round(x), round(surf.get_width() - y)))
pygame.draw.polygon(surf, color, pts, True)
Minimal example
import pygame, math
import pymunk
pygame.init()
screen = pygame.display.set_mode((200, 200))
def create_rect(space, pos, angle):
body = pymunk.Body(1, 100, body_type= pymunk.Body.DYNAMIC)
body.position = pos
body.angle = angle
poly_dims = [(-50, 0), (50, 0), (50, 5), (-50, 5)]
shape = pymunk.Poly(body,poly_dims)
space.add(body,shape)
return shape
def drawShape(surf, color, shape):
pts = []
for v in shape.get_vertices():
x, y = v.rotated(shape.body.angle) + shape.body.position
pts.append((round(x), round(surf.get_width() - y)))
pygame.draw.polygon(surf, color, pts, True)
space = pymunk.Space()
space.gravity = (0, 0)
board = create_rect(space, (100, 100), math.pi / 4)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill((255, 255, 255))
drawShape(screen, (255, 0, 0), board)
pygame.display.flip()
pygame.quit()
Alternatively you can use the pymunk.pygame_util Module. Minimal example:
import pygame, math
import pymunk
import pymunk.pygame_util
pygame.init()
screen = pygame.display.set_mode((200, 200))
draw_options = pymunk.pygame_util.DrawOptions(screen)
def create_rect(space, pos, angle):
body = pymunk.Body(1, 100, body_type= pymunk.Body.DYNAMIC)
body.position = pos
body.angle = angle
poly_dims = [(-50, 0), (50, 0), (50, 5), (-50, 5)]
shape = pymunk.Poly(body,poly_dims)
space.add(body,shape)
return shape
space = pymunk.Space()
space.gravity = (0, 0)
board = create_rect(space, (100, 100), math.pi / 4)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill((255, 255, 255))
space.debug_draw(draw_options)
pygame.display.flip()
pygame.quit()

what does draw.lines return?

from what I understand draw.lines joins each coordinate that is passed to it, so if we have [A, B, C]
will draw a line from coordinate A to coordinate B and from B to C and if closed is True it draws a line from coordinate A to coordinate C therefore it will always join the first coordinate with the last one its right?
what i don't understand is what the rect variable returns..
I think that every time I add a new coordinate it returns (starting_point, rectangle_size) where starting point is the first coordinate and the rectangle size is calculated by the distance of the first coordinate with the last one then draw the rectangle with draw.rect
but the reasoning I don't think is right because if I add a coordinate of this type to the list the rectangle remains unchanged
CODE:
"""Place a polygone line with the clicks of the mouse."""
import pygame
from pygame.locals import *
RED = (255, 0, 0)
GREEN = (0, 255, 0)
GRAY = (150, 150, 150)
pygame.init()
screen = pygame.display.set_mode((640, 240))
drawing = False
points = []
running = True
while running:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
points.append(event.pos)
drawing = True
elif event.type == MOUSEBUTTONUP:
drawing = False
elif event.type == MOUSEMOTION and drawing:
points[-1] = event.pos
screen.fill(GRAY)
if len(points)>1:
rect = pygame.draw.lines(screen, RED, True, points, 3)
pygame.draw.rect(screen, GREEN, rect, 1)
pygame.display.update()
pygame.quit()
pygame.draw.lines() returns a pygame.Rect object that encloses all the points of the line:
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the position of the first point in the points parameter (float values will be truncated) and its width and height will be 0
The rectangle does not start at any particular point on the line, but is just large enough to enclose all the points along the line.
It returns the same as:
list_x, list_y = zip(*points)
min_x, max_x = min(list_x), max(list_x)
min_y, max_y = min(list_y), max(list_y)
rect = pygame.Rect(min_x, min_y, max_x-min_x, max_y-min_y)
According to the docs:
Returns:
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the position of the first point in the points parameter (float values will be truncated) and its width and height will be 0
this is the reasoning I made:
import pygame
def minore(lista_coordinate, asse):
dimensione = lista_coordinate[0][asse]
for coordinata in lista_coordinate:
if coordinata[asse] < dimensione:
dimensione = coordinata[asse]
return dimensione
def maggiore(lista_coordinate, asse):
dimensione = lista_coordinate[0][asse]
for coordinata in lista_coordinate:
if coordinata[asse] > dimensione:
dimensione = coordinata[asse]
return dimensione
pygame.init()
schermo = pygame.display.set_mode((500, 400))
punti = [(100, 100), (200, 100), (200, 50), (150, 80)]
larghezza_minore = minore(punti, 0)
larghezza_maggiore = maggiore(punti, 0)
larghezza_rettangolo = larghezza_maggiore - larghezza_minore
altezza_minore = minore(punti, 1)
altezza_maggiore = maggiore(punti, 1)
altezza_rettangolo = altezza_maggiore - altezza_minore
dimensioni_rettangolo = (larghezza_rettangolo, altezza_rettangolo)
inizio = (larghezza_minore, altezza_minore)
pygame.draw.lines(schermo, (255, 0, 0), True, punti, 3)
pygame.draw.rect(schermo, (0, 255, 0), (inizio, dimensioni_rettangolo), 1)
pygame.display.update()

How to make ball bounce off triangle in pygame?

Hello I'm a pretty new programmer and I'm trying to make a ball bounce off a 45 degree triangle. Here is my code:
This program makes the ball bounce when it hits the sides of the window, but I don't know how to make it bounce off a triangle.
import pygame # importing the pygame
import sys # importing the system libraries
import time # importing timer
import random
from pygame.locals import * # importing the locals functions from the pygame library set
pygame.init() # the function from pygame that initializes all relevant variable
# setting length and width
width = 500
length = 300
# colour variables
WHITE = (255,255,255)
BLUE = (0,0,255)
# importing ball image
ball = pygame.image.load('ball.png')
ballRect = ball.get_rect()
ballRect.left = 300
ballRect.right = 300
# setting speed
x_speed = 2
y_speed = 2
# setting window size
WINDOW = pygame.display.set_mode((width, length))# setting the size of the window
pygame.display.update()
# loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
ballRect = ballRect.move(x_speed,y_speed)
WINDOW.fill(WHITE) # changing screen colour to white
WINDOW.blit(ball,ballRect) # printing the ball to screen
pygame.display.update()
pygame.display.flip()
time.sleep(0.002) # to slow down the speed of bouncing
pygame.display.update()
# if the left side of ballRect is in a position less than 0, or the right side of ballRect is greater than 500
if ballRect.left < 0 or ballRect.right > (width):
x_speed = x_speed * -1
# if the top of ballRect is in a position less than 0, or the bottom of ballRect is greater than the length
elif ballRect.top < 0 or ballRect.bottom > (length):
y_speed = y_speed * -1
pygame.display.update()
I haven't drawn in the triangle because I don't know where to, but I expect the ball to bounce off the triangle like it does when it hits the sides of the window. Any help would be great!
Interesting task. A triangle can be defined by a simple list:
triangle = [(250, 220), (400, 300), (100, 300)]
The triangle can be drawn by pygame.draw.polygon()
pygame.draw.polygon(WINDOW, RED, triangle, 0)
Use pygame.math.Vector2 to define the position and the motion vector of the ball:
ballvec = pygame.math.Vector2(1, 1)
ballpos = pygame.math.Vector2(150, 250)
balldiameter = 64
Create a function, which does the collision detection. The function has to detect if the ball hits a line. If the line is hit, then the motion vector of the ball is reflected on the line.
The line is represented by 2 points (lp0, lp1), which are pygame.math.Vector2 objects.
The position of the ball (pt) and the motion vector (dir) are pygame.math.Vector2 objects, too:
def isect(lp0, lp1, pt, dir, radius):
# direction vector of the line
l_dir = (lp1 - lp0).normalize()
# normal vector to the line
nv = pygame.math.Vector2(-l_dir[1], l_dir[0])
# distance to line
d = (lp0-pt).dot(nv)
# intersection point on endless line
ptX = pt + nv * d
# test if the ball hits the line
if abs(d) > radius or dir.dot(ptX-pt) <= 0:
return dir
if (ptX-lp0).dot(l_dir) < 0 or (ptX-lp1).dot(l_dir) > 0:
return dir
# reflect the direction vector on the line (like a billiard ball)
r_dir = dir.reflect(nv)
return r_dir
Append the window rectangle and the triangle to a list of lines. Ech line is represented by a tuple of 2 pygame.math.Vector2 objects:
# add screen rect
screen_rect = [(0, 0), (0, 300), (500, 300), (500, 0)]
for i in range(len(screen_rect)):
p0, p1 = screen_rect[i], screen_rect[(i+1) % len(screen_rect)]
line_list.append((pygame.math.Vector2(p0[0], p0[1]), pygame.math.Vector2(p1[0], p1[1])))
# add red trianlge
triangle = [(250, 220), (400, 300), (100, 300)]
for i in range(len(triangle)):
p0, p1 = triangle[i], triangle[(i+1) % len(triangle)]
line_list.append((pygame.math.Vector2(p0[0], p0[1]), pygame.math.Vector2(p1[0], p1[1])))
Do the collision detection in a loop, which traverse the lines. If the ball hits a line, then the motion vector is replaced by the reflected motion vector:
for line in line_list:
ballvec = isect(*line, ballpos, ballvec, balldiameter/2)
Finally update the position of the ball an the ball rectangle:
ballpos = ballpos + ballvec
ballRect.x, ballRect.y = ballpos[0]-ballRect.width/2, ballpos[1]-ballRect.height/2
See the example code, where I applied the suggested changes to your original code. My ball image has a size of 64x64. The ball diameter has to be set to this size (balldiameter = 64):
Minimal example
import pygame
pygame.init()
window = pygame.display.set_mode((500, 300))
try:
ball = pygame.image.load("Ball64.png")
except:
ball = pygame.Surface((64, 64), pygame.SRCALPHA)
pygame.draw.circle(ball, (255, 255, 0), (32, 32), 32)
ballvec = pygame.math.Vector2(1.5, 1.5)
ballpos = pygame.math.Vector2(150, 250)
balldiameter = ball.get_width()
def reflect_circle_on_line(lp0, lp1, pt, dir, radius):
l_dir = (lp1 - lp0).normalize() # direction vector of the line
nv = pygame.math.Vector2(-l_dir[1], l_dir[0]) # normal vector to the line
d = (lp0-pt).dot(nv) # distance to line
ptX = pt + nv * d # intersection point on endless line
if (abs(d) > radius or dir.dot(ptX-pt) <= 0 or # test if the ball hits the line
(ptX-lp0).dot(l_dir) < 0 or (ptX-lp1).dot(l_dir) > 0):
return dir
r_dir = dir.reflect(nv) # reflect the direction vector on the line (like a billiard ball)
return r_dir
triangle1 = [(250, 220), (400, 300), (100, 300)]
triangle2 = [(250, 80), (400, 0), (100, 0)]
screen_rect = [(0, 0), (0, window.get_height()), window.get_size(), (window.get_width(), 0)]
line_list = []
for p0, p1 in zip(triangle1, triangle1[1:] + triangle1[:1]):
line_list.append((pygame.math.Vector2(p0), pygame.math.Vector2(p1)))
for p0, p1 in zip(triangle2, triangle2[1:] + triangle2[:1]):
line_list.append((pygame.math.Vector2(p0), pygame.math.Vector2(p1)))
for p0, p1 in zip(screen_rect, screen_rect[1:] + screen_rect[:1]):
line_list.append((pygame.math.Vector2(p0), pygame.math.Vector2(p1)))
clock = pygame.time.Clock()
run = True
while run:
clock.tick(250)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for line in line_list:
ballvec = reflect_circle_on_line(*line, ballpos, ballvec, balldiameter/2)
ballpos = ballpos + ballvec
window.fill((64, 64, 64))
pygame.draw.polygon(window, (255, 0, 0), triangle1, 0)
pygame.draw.polygon(window, (0, 0, 255), triangle2, 0)
window.blit(ball, (round(ballpos[0]-balldiameter/2), round(ballpos[1]-balldiameter/2)))
pygame.display.flip()
pygame.quit()

How to get the coordinates of the place where the sprites are colliding?

is it possible to get the coordinates of the place where the sprites are colliding?, and if it more than one is it possible to get both?
Thanks a lot for the solvers
You can use the Pymunk physics library to get the contact points. Of course that means you'll have to familiarize yourself with this library first (it can be a bit difficult for beginners). You especially need to know how collision handlers, arbiters and callback functions work.
So you create a collision handler which checks if there were collisions between shapes of two specified collision types, e.g. handler = space.add_collision_handler(1, 1). When a collsion occurs the handler calls some callback functions (in the example I set handler.post_solve to the callback function) which receive an arbiter object as an argument that holds the collision data. Then you can extract the needed information from this arbiter, add it to a list or other data structure and use it in your main loop.
import sys
import random
import pygame as pg
import pymunk as pm
from pymunk import Vec2d
def flipy(p):
"""Convert chipmunk coordinates to pygame coordinates."""
return Vec2d(p[0], -p[1]+600)
class Ball(pg.sprite.Sprite):
def __init__(self, space, pos, mass=5, radius=30, elasticity=0.9):
super().__init__()
self.image = pg.Surface((60, 60), pg.SRCALPHA)
pg.draw.circle(self.image, pg.Color('royalblue'), (30, 30), radius)
self.rect = self.image.get_rect(center=pos)
# Set up the body and shape of this object and add them to the space.
inertia = pm.moment_for_circle(mass, 0, radius, (0, 0))
self.body = pm.Body(mass, inertia)
self.body.position = flipy(pos)
self.shape = pm.Circle(self.body, radius, (0, 0))
self.shape.elasticity = elasticity
# This type will be used by the collision handler.
self.shape.collision_type = 1
self.space = space
self.space.add(self.body, self.shape)
def update(self):
pos = flipy(self.body.position)
self.rect.center = pos
if pos.y > 600:
self.space.remove(self.body, self.shape)
self.kill()
def main():
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
done = False
contact_points = []
def contact_callback(arbiter, space, data):
"""Append the contact point to the contact_points list."""
if arbiter.is_first_contact:
for contact in arbiter.contact_point_set.points:
contact_points.append(contact.point_a)
# Pymunk stuff.
space = pm.Space()
space.gravity = Vec2d(0, -900)
# This collision handler will be used to get the contact points.
# It checks if shapes with `collision_type` 1 collide with others
# that also have type 1.
handler = space.add_collision_handler(1, 1)
# After a collision is solved, the callback funtion will be called
# which appends the contact point to the `contact_points` list.
handler.post_solve = contact_callback
# Create some static lines.
static_lines = [
pm.Segment(space.static_body, (170, 200), (0, 300), .1),
pm.Segment(space.static_body, (170, 200), (500, 200), .1),
pm.Segment(space.static_body, (500, 200), (600, 260), .1),
]
for line in static_lines:
line.elasticity = 0.9
space.add(static_lines)
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEBUTTONDOWN:
all_sprites.add(Ball(space, event.pos))
contact_points = []
space.step(1/60) # Update the physics space.
all_sprites.update()
screen.fill((60, 70, 80))
all_sprites.draw(screen) # Draw the sprite group.
# Draw static_lines.
for line in static_lines:
body = line.body
p1 = flipy(body.position + line.a.rotated(body.angle))
p2 = flipy(body.position + line.b.rotated(body.angle))
pg.draw.line(screen, pg.Color('gray68'), p1, p2, 5)
# Draw contact_points.
for point in contact_points:
x, y = flipy(point)
x, y = int(x), int(y)
pg.draw.circle(screen, pg.Color('orange'), (x, y), 8)
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
sys.exit()

Categories

Resources