I'm using Turtle Graphics, and i want to draw a pixel at specific x,y position
something like :
pixel = turtle.Turtle()
pixel.draw(x, y)
Is it possible ?
The goto, setpos and setposition methods of the turtle module can be
used to set the position to a given x, y for the turtle.
Then, the dot method can be used to draw a pixel at the point.
# STEP-1: GOING TO (X, Y)
# Any one of these methods can be used
# pixel.goto(x, y) # or
# pixel.setpos(x, y) # or
pixel.setposition(x, y)
# STEP-2: DRAWING A PIXEL
pixel.dot(1, "black") # drawing the pixel.
Further, a function can be defined incase the same has to be used multiple times like so -:
def draw_pixel(turtle, x, y, color) :
# Draws a pixel of given color using given turtle at (x, y)
# Any one of these methods can be used
# turtle.goto(x, y) # or
# turtle.setpos(x, y) # or
turtle.setposition(x, y)
turtle.dot(1, color) # drawing the pixel.
return
Related
I am working with pygame, to display the score I have images from 0 to 9 and need to place them centralized, but i can't just put it in the middle because the size of the score can change, the only solution I found until now is to hardcode it for scores up to 9, then up to 99 etc like in the code below
if len(str(self.value)) == 1:
win.blit(self.imgs[self.value],
((x - self.imgs[self.value].get_width(), y)))
elif len(str(self.value)) == 2:
win.blit(self.imgs[int(str(self.value)[0])],
(x - self.imgs[int(str(self.value)[0])].get_width(), y))
win.blit(self.imgs[int(str(self.value)[1])],
(x, y))
Is there a generic way to do display n numbers centralized looping for all the numbers of the score?
Get a pygame.Rect object with the size of the image (pygame.Surface) by get_rect(). Set the center of the rectangle by an keyword attribute (center = (x, y)) and use the rectangle to blit the image:
centered_rect = self.imgs[self.value].get_rect(center = (x, y))
win.blit(self.imgs[self.value], centered_rect)
So, I'm randomly generating a "world" and drawing it with pygame. That part worked perfectly fine until I decided to add something above what I already drew.
The code is as follows. What each thing is is of no consequence, but DISPLAY is the surface I'm working on, y.colour is a size 3 Tuple, y.coord is a (x,y) Tuple
for x in W_Map:
for y in x:
DISPLAY.fill(y.colour, pygame.Rect(y.coord[0]-tile_size,
y.coord[1]-tile_size,
y.coord[0]+tile_size,
y.coord[1]+tile_size))
DISPLAY.fill(lime, pygame.Rect(300,300,310,310))
According to the game above, this should create a lime coloured 10x10 square centered on 305x305. The result, however, is the following picture:
As you can see, the first part of the code draws the terrain perfectly, but when creating the lime square on top of what's already drawn, it goes completely crazy. The whole function is:
pygame.init()
DISPLAY = pygame.display.set_mode(
(shape[0]*2*tile_size, shape[1]*2*tile_size))
DISPLAY.fill((0,0,0))
#Make and draw the Rects
for x in W_Map:
for y in x:
DISPLAY.fill(y.colour, pygame.Rect(y.coord[0]-tile_size,
y.coord[1]-tile_size,
y.coord[0]+tile_size,
y.coord[1]+tile_size))
DISPLAY.fill(lime, pygame.Rect(300,300,310,310))
Pygame's Rect takes four arguments: x, y, width, and height, where x and y are relative to the top left of the viewport. Your lime rectangle is created with pygame.Rect(300,300,310,310), meaning a width and height of 310 pixels and a location of (300, 300).
To create a 10x10 rectangle centered at (305, 305) you'll need to use pygame.Rect(300, 300, 10, 10). You can also create a helper function to translate size and center point to the necessary rectangle parameters:
def center_rect(x, y, width, height):
return pygame.Rect(x - width / 2, y - height / 2, width, height)
Then you could use this helper function like so:
DISPLAY.fill(lime, center_rect(305, 305, 10, 10))
I need to make a circle by adjusting conditions on the heights, this program using a lot of random circles but I am unsure where to go from here? I am trying to use the following equation d = (sqrt)((x1 –x2)^2 +(y1 – y2)^2). Right now the program draws many random circles, so adjusting the formula i should be able to manipulate it so that certain circles are red in the centre (like the japan flag).
# using the SimpleGraphics library
from SimpleGraphics import *
# use the random library to generate random numbers
import random
diameter = 15
##
# returns a valid colour based on the input coordinates
#
# #param x is an x-coordinate
# #param y is a y-coordinate
# #return a colour based on the input x,y values for the given flag
##
def define_colour(x,y):
##
if y < (((2.5 - 0)**2) + ((-0.5 - 0)**2)**(1/2)):
c = 'red'
else:
c = 'white'
return c
return None
# repeat until window is closed
while not closed():
# generate random x and y values
x = random.randint(0, getWidth())
y = random.randint(0, getHeight())
# set colour for current circle
setFill( define_colour(x,y) )
# draw the current circle
ellipse(x, y, diameter, diameter)
Here's some code that endlessly draws circles. Circles that are close to the centre of the screen will be drawn in red, all other circles will be drawn in white. Eventually, this will create an image similar to the flag of Japan, although the edge of the inner red "circle" will not be smooth.
I have not tested this code because I don't have the SimpleGraphics module, and I'm not having much success locating it via Google or pip.
from SimpleGraphics import *
import random
diameter = 15
width, height = getWidth(), getHeight()
cx, cy = width // 2, height // 2
# Adjust the multiplier (10) to control the size of the central red portion
min_dist_squared = (10 * diameter) ** 2
def define_colour(x, y):
#Calculate distance squared from screen centre
r2 = (x - cx) ** 2 + (y - cy) ** 2
if r2 <= min_dist_squared:
return 'red'
else:
return 'white'
# repeat until window is closed
while not closed():
# generate random x and y values
x = random.randrange(0, width)
y = random.randrange(0, height)
# set colour for current circle
setFill(define_colour(x, y))
# draw the current circle
ellipse(x, y, diameter, diameter)
I am attempting to overlay 2 concentric circles (with different radii) over a picture. The problem I am encountering is that, after the first circle, the following circles don't show up. I assumed the algorithm would perform the functions in the order they're written which would overlay the white circle over the red one however it doesn't. The X and Y values are set up so that regardless of the dimensions of the picture, the outer circle would touch the edges of the longer side.
def concentricCircles(pic):
X = getWidth(pic)
Y = getHeight(pic)
if(X >= Y):
addOvalFilled(pic,(X/2) - (Y/2),0, Y, Y, green)
if(X < Y):
addOvalFilled(pic, 0 , (Y/2) - (X/2), X, X, green)
if(X >= Y):
addOvalFilled(pic,((X/2) - (Y/3)),0+(Y/6), 2/3*Y, 2/3*Y, blue)
if(X < Y):
addOvalFilled(pic,((Y/2) - (X/3)),0+(X/6), 2/3*X, 2/3*X, blue)
show(pic)
Do I have to establish a for loop within the if statement to perform what I'm looking for? Can I embed an if statement within another or combine the addOvalFilled functions within the same if statement? Would it be more feasible to copy the image onto a canvas before applying the concentric circles? If so how would I go about doing that in this case?
Thanks.
Is it possible to set the limits of the how far axhspan spand the x axis using coordinates rather than a value of 0-1? Usually the command takes:
axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs)
I know that you can calculate the the 0-1 value for xmin and xmaxfrom the coordinates, but it just seem a long winded way to o it?
Example:
I would like the blue shading to go from 0-100, white 100-200, blue 200-400.
Is the only way to do this either by converting to value of 0-1 or just adding the rectangle shape s as opposed to using axhspan()?
Use matplotlib.patches.Rectangle with matplotlib.axes.Axes.add_patch.
For example:
from pylab import *
from matplotlib.patches import Rectangle
plot(arange(0, 500), arange(0, 500))
gca().add_patch(Rectangle((100, 100), 200, 200)) # (x, y), width, height
show()
NOTE the 2nd, 3rd parameters of Rectangle constructor are width, height (not x, y position).