How open a GIF image in with turtle? - python

I tried to animate my GIF image but it does not work, this is my code :
import turtle
screen = turtle.Screen()
t1 = turtle.Turtle()
screen.addshape('black.gif')
t1.shape('black.gif')
turtle.done()

Make sure that your python code and your gif is in the same folder and then try this code:
import turtle
screen = turtle.Screen()
screen.setup(500, 500) # You can change the dimensions(500, 500) to any number
t = turtle.Turlte()
screen.addshape("name_of_the_gif.gif") #don't forget they have to be in the same file
t.shape("name_of_the_gif.gif")# make the same gif as the screen.addshape

Related

Turtle Text Flashing

When I attempt to put text over a turtle in the python turtle module, it flashes. Any solutions?
import turtle
s = turtle.Screen()
s.setup(width=500,height=600)
c = turtle.Turtle()
c.shapesize(stretch_len=5,stretch_wid=5)
c.goto(0,0)
c.shape("square")
pen = turtle.Turtle()
pen.hideturtle()
pen.goto(0,0)
pen.color("red")
while True:
pen.write("hello!")
s.update()
Although I don't see the flashing on my screen, my guess is your problem is related to this bad idiom:
while True:
# ...
s.update()
We need neither the while True: (which has no place in an event-driven environment like turtle) nor the call to update() (which is not needed given no previous call to tracer()). Let's rewrite this as turtle code:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=500, height=600)
turtle = Turtle()
turtle.hideturtle()
turtle.shapesize(5)
turtle.shape('square')
turtle.stamp() # stamp a square so we can reuse turtle
pen = Turtle()
pen.hideturtle()
pen.color("red")
pen.write("Hello!", align='center', font=('Arial', 16, 'normal'))
screen.exitonclick()
Does that solve your problem?

Why my python kept crashing when it tried to run the turtle.tracer() method in turtle module?

I was coding a Snake Game using turtle module but when I add this line into my code the turtle screen and python crashed:
turtle.tracer(0)
can somebody help me so I can complete the game? Thanks a lot
my code:
from turtle import Turtle, Screen, colormode
screen = Screen()
screen.bgcolor("black")
screen.setup(width=600, height=600)
screen.title("My Snake Game")
screen.tracer(0)
x = 0
segments = []
for turtle in range(3):
turtle = Turtle("square")
turtle.color("white")
turtle.penup()
turtle.goto(0-x, 0)
x += 20
segments.append(turtle)
game_is_on = True
screen.update()
while game_is_on:
for segment in segments:
segment.forward(20)
screen.exitonclick()
I think we need to know more about what you mean by 'crashed'. If you mean everything froze, that's the code you wrote. Once you introduce tracer() you need to provide an update() for every change you want the user to see. But you don't have any update() calls in your loop so everything visually remains as it was before the loop. If you want to see the segments move, you need to do something like:
from turtle import Turtle, Screen
screen = Screen()
screen.bgcolor('black')
screen.setup(width=600, height=600)
screen.title("My Snake Game")
screen.tracer(0)
x = 0
segments = []
for turtle in range(3):
turtle = Turtle('square')
turtle.color('white')
turtle.penup()
turtle.setx(x)
x -= 20
segments.append(turtle)
screen.update()
game_is_on = True
while game_is_on:
for segment in segments:
segment.forward(20)
screen.update()
screen.exitonclick() # never reached
If you mean by 'crashed' that Python quit back to the operating system, then you need to describe the environment under which you're running this code.

Python turtle object wont show in window

I am currently trying to make a copy of Atari's breakout using python and turtle. Previously I have created a pong copy which does not use OOP and it worked perfectly fine. However due to the bricks in breakout I decided to use OOP and create an object class for the bricks. Once I run the program, it wont display the brick. Any ideas to why?
import turtle
window = turtle.Screen()
window.title('Atari Breakout')
window.bgcolor('black')
window.setup(width=800, height=600)
window.tracer(0)
class Brick(Turtle):
def __init__(self):
super().__init__(shape='square', visible=True)
self.myturtle = turtle.Turtle()
self.color = 'white'
self.shapesize(stretch_wid=10, stretch_len=4)
self.pendown()
self.goto(-350, 200)
board1 = Brick()
window.update()
There are a few problems in this code - firstly, you need turtle.Turtle not just Turtle as otherwise this causes an error. Secondly, the line self.myturtle = turtle.Turtle() is unnecessary as super().__init__(shape='square', visible=True) already creates a turtle, and thirdly self.color = 'white' should be changed to self.color('white'). Also, I'm pretty sure you meant self.penup() not self.pendown() to stop the brick from drawing a line from the center to its position.
Completed code:
import turtle
window = turtle.Screen()
window.title('Atari Breakout')
window.bgcolor('black')
window.setup(width=800, height=600)
window.tracer(0)
class Brick(turtle.Turtle):
def __init__(self):
super().__init__(shape='square', visible=True)
self.color('white')
self.shapesize(stretch_wid=10, stretch_len=4)
self.penup()
self.goto(-350, 200)
board1 = Brick()
window.update()

I am using Turtle and when I open a window with Turtle, it does not respond

I am using Turtle and when I open a window with Turtle, it does not respond. I haven't finished the code yet. I am still a beginner and I am trying to make a simple pong game. Thanks!
import turtle
# Paddles
pa = turtle.Turtle()
pa.speed(0)
pa.shape("square")
pa.color("white")
pa.penup()
pa.goto(-350, 0)
pa.shapesize(stretch_wid=5, stretch_len=1)
pb = turtle.Turtle()
pb.speed(0)
pb.shape("square")
pb.color("white")
pb.penup()
pb.goto(350, 0)
pb.shapesize(stretch_wid=5, stretch_len=1)
# ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)
# Screen
win = turtle.Screen()
win.title("Stankity Stank Stank Stank")
win.bgcolor("black")
win.setup(width=800, height=600)
win.tracer(0)
while True:
win.update
# Controls
def paup():
y = pa.ycor()
y += 20
pa.sety(y)
win.listen()
win.onkeypress(paup, "w")
Stack Overflow said my post was mostly code, so here's some extra text.
You did not ask a question. I presume it is "Why don't I see anything?" Answers:
A white pen on a white background is invisible.
A pen that is up does not draw.
Fix those two things and the following draws a black line with a square at the end.
import turtle
pb = turtle.Turtle()
pb.speed(0)
pb.shape("square")
pb.color("black")
pb.pendown()
pb.goto(350, 0)
I recommend that you look at the turtle doc, starting with the sample on the upper right, and the turtledemo module (python -m turtledemo or import turtledemo). Also, get a few lines right before writing so much.

Image import issue with pygame

So this program is supposed to just put a sprite on screen. But the image is not importing right. I made sure the image has no background, but in the program, part of the background turns black while the other half stays transparent. Its really weird.
heres my code:
from Tkinter import *
import pygame
from livewires import games
#Creating screen window
games.init(screen_width = 700, screen_height = 650, fps = 50)
#creating background image for the screen
screen_background = games.load_image('resized_stars background.png', transparent = False)
games.screen.background = screen_background
#Creating a sprite
spaceship_image = games.load_image('8-bit_Spaceship.png')
spaceship = games.Sprite(image = spaceship_image, x=350, y=235)
games.screen.add(spaceship)
#main loop at the end of the program just as in tkinter
games.screen.mainloop()
Why will it not show up on screen properly?
Here's what I use, and it works just fine:
First, create the screen:
screen = pygame.display.set_mode((screen width, screen height))
Then:
spaceship = pygame.image.load("direct it to the image")
An example of directing it to the image would be "C:/PythonFolder/spaceship.png"
I see you just put the name of the file.
Then, when you're ready to blit (append) it to the screen, use
screen.blit(spaceship, (x location, y location))
Then update it:
pygame.display.update()
or:
pygame.display.flip()

Categories