Tetris [PyGame] - Let the shapes fall - python
I'm trying to clone the Tetris game and already have PyGame pick a random shape and display it. I drew an array-backed grid and 'told' PyGame to draw colored squares in certain cells in order to get the cells.
def iShape():
grid [0][5] = 3
grid [0][6] = 3
grid [0][7] = 3
grid [0][8] = 3
pygame.init()
this tells the system on which cell of the grid it will draw the square in order to get the shape.
def draw():
allShapes = ['''all Shapes that I defined''']
pick = random.choice (allShapes)
... #otherstuff
if pick == iShape:
if grid[row][column] == 3:
color = orange
#draw the squares
I have been trying to think of how I could let the shapes fall slowly so the player can move/rotate them before they hit the ground but none of my ideas work out. Does anyone have a suggestion?
try to create a def called clock or tick(anything) and have that control the drop speed. or you could youse the in built python timer by doin inport mathand there is a command to have times so you could have then drop a pice of the grid every second or something like that sry about the brightness
I've found a Tetris clone called Tetromino online. I can't quite say it will work as it probably uses a different code style, but you could get an idea from it. Its at the link https://inventwithpython.com/pygame/chapter7.html
Related
Moving the cursor in relation to a certain fix-point in Python
is there any possibility to move the cursor in Python with % starting from a certain coordinate like (1,1)? I am using pyautogui atm to automate and I thought it would be quite convenient if this is independent from the monitor size making it universal. Thanks for your help in advance!
It's possible indirectly. As detailed at the top of the Mouse Control Functions page of the documentation, you can get the screen size using the size() function (which returns a tuple of (X, Y)). You can then do the math to figure out how many screen pixels equal the percentage you're looking for, then call moveTo() to go there. # script to move mouse 50% to the right and down import pyautogui as pag percentage = 0.5 cur_X, cur_Y = pag.position() # current X and Y coordinates size_X, size_Y = pag.size() # screen size goto_X = (size_X - cur_X) * percentage + cur_X # current location plus half goto_Y = (size_Y - cur_Y) * percentage + cur_Y # the distance to the edge pag.moveTo(goto_X, goto_Y, 1) # move to new position, taking 1 second
Issue with Turtle in Python (AttributeError: 'Turtle' object has no attribute 'tracer')
I've been trying to follow this tutorial on creating pixel art in Turtle: https://www.101computing.net/pixel-art-in-python/ And I tried to copy over their code, but when I put it into VS Code, I get the following error: AttributeError: 'Turtle' object has no attribute 'tracer' Running their editor seems to work. Any ideas on what the issue could be? All I did was copy and paste the code (and fix some issues with indents). #Pixel Art - http://www.101computing.net/pixel-art-in-python/ import turtle myPen = turtle.Turtle() myPen.tracer(0) myPen.speed(0) myPen.color("#000000") # This function draws a box by drawing each side of the square and using the fill function def box(intDim): myPen.begin_fill() # 0 deg. myPen.forward(intDim) myPen.left(90) # 90 deg. myPen.forward(intDim) myPen.left(90) # 180 deg. myPen.forward(intDim) myPen.left(90) # 270 deg. myPen.forward(intDim) myPen.end_fill() myPen.setheading(0) boxSize = 10 #Position myPen in top left area of the screen myPen.penup() myPen.forward(-100) myPen.setheading(90) myPen.forward(100) myPen.setheading(0) ##Here is an example of how to draw a box #box(boxSize) ##Here are some instructions on how to move "myPen" around before drawing a box. #myPen.setheading(0) #point to the right, 90 to go up, 180 to go to the left 270 to go down #myPen.penup() #myPen.forward(boxSize) #myPen.pendown() #Here is how your PixelArt is stored (using a "list of lists") pixels = [[0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0]] pixels.append([0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0]) pixels.append([0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0]) pixels.append([0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0]) pixels.append([0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0]) pixels.append([1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1]) pixels.append([1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1]) pixels.append([1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1]) pixels.append([1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1]) pixels.append([1,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1]) pixels.append([1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1]) pixels.append([1,1,1,1,0,0,1,0,0,1,0,0,1,1,1,1]) pixels.append([0,1,1,0,0,0,1,0,0,1,0,0,0,1,1,0]) pixels.append([0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0]) pixels.append([0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0]) pixels.append([0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0]) for i in range (0,len(pixels)): for j in range (0,len(pixels[i])): if pixels[i][j]==1: box(boxSize) myPen.penup() myPen.forward(boxSize) myPen.pendown() myPen.setheading(270) myPen.penup() myPen.forward(boxSize) myPen.setheading(180) myPen.forward(boxSize*len(pixels[i])) myPen.setheading(0) myPen.pendown() myPen.getscreen().update()
From the docs Changes since Python 2.6 The methods Turtle.tracer(), Turtle.window_width() and Turtle.window_height() have been eliminated. Methods with these names and functionality are now available only as methods of Screen. The reason it works in their online IDE is that they have an older version of python. You can use turtle.tracer(0).
How can I get the color that is at a set of coordinates in python turtle?
With python, I'm trying to make a function that gets the colour that the turtle is currently on top of. I've found that turtle.pos() gives me coordinates of its location, how can I use those coordinates to get colours?
I think the best way to do this would be using the steps shown here: https://www.kite.com/python/answers/how-to-find-the-rgb-value-of-a-pixel-in-python
One way to go about this is to append every turtle you create into a list, then in your main game loop, loop through the list of turtle to see if the turtle.distance() is smaller than a certain value: import turtle wn = turtle.Screen() turtles = [] yertle = turtle.Turtle('turtle') while True: wn.tracer(0) for t in turtles: if yertle.distance(t) < 20 and yertle != t: print(f'Touched {t.color()}') wn.update() In the above code I used if yertle.distance(t) < 20 which is for when all the turtles are the default size: 20.
Manim: How to update function with defined time interval
I am currently learning manim and I am trying to replicate 3b1b's fourier drawing animations. (https://www.youtube.com/watch?v=-qgreAUpPwM) I've got everything set up and the basics of the animation is working. The only problem I've got is, that it doesnt draw the wanted figure. I am currently using valuetracker to update the function. To get the animation right, I think i need to have a constant "dt" between each updating, but I don't know how to implement that. I am not sure if valuetracker is the right way to do it. I've tried the using "dt" like in Theorem of Beethoven tutotial 8.3 (https://www.youtube.com/watch?v=J6qT8YZQeOw&t=506s). Wasn't able to make it work like that though. Here is the code that updates all mobjects in the animation: def update_fct(c): # Updating mobject for k in range(len(c)): c[k].become(c[k].start) for j in range(k): # nth Circles rotates around all circles before c[k].rotate(TAU * values[j, 2] * alpha.get_value(), about_point=c[j].get_center()) dots[k].move_to(circles[k].points[0]) # move dots to new position if k == N - 1: # Only track path of last dot old_path = paths[k].copy() old_path.append_vectorized_mobject(Line(old_path.points[-1], dots[k].get_center())) old_path.make_smooth() paths[k].become(old_path) lines[k].put_start_and_end_on(circles[k].get_center(), dots[k].get_center()) # Update Arrow circles.add_updater(update_fct) self.add(circles,lines,paths[N-1]) # Only add circles, arrows and last path self.play( alpha.set_value, 1, rate_func=linear, run_time=10 ) circles.clear_updaters()
Maya python and camera animation
Hey so in maya using python I am importing cameras that are locked with keyed animation on them and I just wanted the timeslider at the bottom to update to the length of animation of the imported camera. I can get the timeslider to adjust I am just having trouble finding the keyframe to adjust it to through python. (example: I import 4 cameras. 1 camera is 10 keyframes. 2 and 3 are 15 keyframes. and then 4th is 52. So I want the timeslider to be 1-52. I can get it to move but i dont know how to find the number 52) edit: sorry basically I import my camera cams = [cam1,cam2,cam3,cam4] for x of cams: cmds.file(x, i=True, dns=True, rnn = True) #i for import # this is how i change the timeslider but I want it to # be the number of keyframed animation on the camera cmds.playbackOptions(max=1000)
camera = 'Camera1' channel = 'translateX' keyframes = cmds.keyframe('{}.{}'.format(camera, channel), query=True) first, last = keyframes[0], keyframes[-1] cmds.playbackOptions(min=first, max=last, ast=first, aet=last) This works regardless of whether or not the channels are locked.