Note that this function must be called after the screen.fill (backgroundcolour) command and before the flip () command. # x-y-value-pairs of where dashes start (and on next, will end)ĭash_knots = np.array(, end_pos, dash_amount) for i in range(2)]). (screen, (0,0,255), (150, 50), 15, 1) This draws a blue ( (0,0,255) is blue in RGB notation) circle centred at (150, 50), with radius 15 and thickness of 1 pixel. # get amount of pieces that line will be split up in (half of it are amount of dashes) Length = np.linalg.norm(end_pos - start_pos) # get euclidian distance between start_pos and end_pos import pygame as pgĭef draw_line_dashed(surface, color, start_pos, end_pos, width = 1, dash_length = 10, exclude_corners = True): You could make this function way shorter, by using all of NumPy's features.
#Pygame draw dashed line code
Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed. We are looking for answers that provide insightful observations about the code in the question. (surf, color, start.get(), end.get(), width) Thanks here is my code:you'C:\\Users\\user\\Desktop\\Python Programs\\Snake\\textures\\you.bmp'import pygame, sys, randomfrom pygame.locals import pygame. When drawing of an object (rectangle, circle, etc.) is done, as indicated by a MOUSEBUTTONUP event, we create a rectangle and append it to the rectangle list: elif event.type MOUSEBUTTONUP: end event.pos size end0-start0, end1-start1 rect pygame.Rect(start, size) rectlist.append(rect) drawing False. Start = origin + (slope * index * dash_length)Įnd = origin + (slope * (index + 1) * dash_length) Hello there is a strange dotted line following the 'player' I made. # get back values in original tuple formatįor index in range(0, length/dash_length, 2): Return int(math.sqrt(self.x**2 + self.y**2)) Return Point((self.x/scalar, self.y/scalar)) Return Point((self.x*scalar, self.y*scalar)) Return Point((self.x - other.x, self.y - other.y)) Return Point((self.x + other.x, self.y + other.y)) Instead of storing all the values you're trying to draw, it might be more efficient to just get the slope of the drawn line, and compute dashes at draw time.Ĭonsidering that there's a lot of mirrored code here, consider using a point class to handle both the x and the y values at the same time! class Point: Line a = abs(x2 - x1) to line dy = dl * b / c calculates the amount x and y change for dl (dash_length).īecause dx and dy are floats, I had to use numpy.arange (built-in range() doesn't allow floating-point). (Apr-24-2017, 01:59 PM) prathack Wrote: great bro I'm really enjoying reading sorce code :) Thank you :) If there is anything I should have done more the Python way please let me know. This function takes two coordinates and draws a colored dashed line from the first to the second coordinate.
#Pygame draw dashed line windows
Last_coords = list(zip(xcoords, ycoords))įor (x1, y1), (x2, y2) in zip(next_coords, last_coords): You must do the following to get started with Pygame: import pygame import sys pygame.init() Strictly speaking, import sys is not needed for PyGame, but as we'll see later, to be able to use the 'close window' button on Windows or Mac, we'll need to use sys.exit(), so it is helpful. Next_coords = list(zip(xcoords, ycoords))
#Pygame draw dashed line how to
How to get column names in Pandas dataframe.Adding new column to existing DataFrame in Pandas.Top 40 Python Interview Questions & Answers python drawing a circle pygmae pygame draw rect with colour pg.draw how to get a rect color in pygame pygamedraw rect lines pygame how to deaw a circle on a pygame screen fill rectangle pygame how to make a cross in pygame make a ring follow another ring pygame how to draw using.