Re: Tkinter App, Turtle ein JPG Skin zuweisen
Verfasst: Sonntag 17. Juni 2018, 21:04
@ blackjack und deswegen soll ich get(x,y) verwenden?
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Code: Alles auswählen
def border_check():
xpos, ypos = turtle.position()
bg_img_xpos = int(xpos + bg_width_offset)
bg_img_ypos = int(bg_image_height - bg_width_offset - ypos)
print("Border Check:", bg_img_xpos, bg_img_ypos)
pixel_color = bg_image.get(bg_img_xpos, bg_img_ypos)
print("Farbe:", pixel_color)
Code: Alles auswählen
bg_image = tk.PhotoImage(file="labyrinth.png")
bg_image_width = bg_image.width()
bg_width_offset = bg_image_width / 2
bg_image_height = bg_image.height()
bg_height_offset = bg_image_height / 2
Code: Alles auswählen
import turtle
from turtle import register_shape, Shape
import tkinter as tk
from PIL import Image, ImageTk
NORTH = 90
SOUTH = 270
EAST = 0
WEST = 180
MOVE_STEP = 10
ANGLE_STEP = 90
def forward():
turtle.setheading(90)
def backward():
turtle.setheading(270)
def left():
turtle.setheading(180)
def right():
turtle.setheading(0)
screen = turtle.Screen()
#img = ImageTk.PhotoImage(Image.open("Turtle.png"))
#register_shape("Bild", Shape("image", img))
# turtle.shape("Bild")
bg_image = tk.PhotoImage(file="Labyrinth.png")
bg_image_width = bg_image.width()
bg_image_height = bg_image.height()
bg_image = tk.PhotoImage(file="labyrinth.png")
bg_image_width = bg_image.width()
bg_width_offset = bg_image_width / 2
bg_image_height = bg_image.height()
bg_height_offset = bg_image_height / 2
screen.setup(bg_image_width, bg_image_height)
canvas = screen.getcanvas()
canvas.create_image(0, 0, image=bg_image)
def border_check():
xpos, ypos = turtle.position()
bg_img_xpos = int(xpos + bg_width_offset)
bg_img_ypos = int(bg_image_height - bg_width_offset - ypos)
#print("Border Check:", bg_img_xpos, bg_img_ypos)
pixel_color = bg_image.get(bg_img_xpos, bg_img_ypos)
#print("Farbe:", pixel_color)
if pixel_color == (0, 0, 0):
turtle.goto(0, 0)
def coordinates():
xpos, ypos = turtle.position()
bg_img_xpos = int(xpos + bg_width_offset)
bg_img_ypos = int(bg_image_height - bg_width_offset - ypos)
pixel_color = bg_image.get(bg_img_xpos, bg_img_ypos)
print("Farbe:", pixel_color)
turtle.penup()
turtle.speed(0)
turtle.goto(-500, 500)
turtle.setheading(EAST)
turtle.showturtle()
# Keyboard actions
screen.listen()
screen.onkey(forward, "w")
screen.onkey(backward, "s")
screen.onkey(left, "a")
screen.onkey(right, "d")
screen.onkey(coordinates, "c")
fd = 1
x = turtle.xcor()
y = turtle.ycor()
ende = False
while not ende:
turtle.forward(fd)
border_check()
screen.listen()
turtle.mainloop()
#0.0 203.0
#999.0 0.0
#turtle.forward(fd)
#x = turtle.xcor()#+500
#y = turtle.ycor()#+500
#a = bg_image.get(x, y)
#if a == (0, 0, 0, 255):
#print("Schwarz")
#else:
#print("weiß")
#if a == (0, 0, 0, 0) or a == (0, 0, 0, 255):
#turtle.goto(0, 0)
#print(a)
Code: Alles auswählen
turtle.penup()
turtle.speed(0)
turtle.goto(-500, 500)
turtle.setheading(EAST)
turtle.showturtle()
Code: Alles auswählen
def nord():
turtle.setheading(NORTH)
def south():
turtle.setheading(SOUTH)
def west():
turtle.setheading(WEST)
def east():
turtle.setheading(EAST)
Code: Alles auswählen
def forward():
turtle.forward(MOVE_STEP)
def backward():
turtle.backward(MOVE_STEP)
Code: Alles auswählen
fd = 1
x = turtle.xcor()
y = turtle.ycor()
ende = False
while not ende:
turtle.forward(fd)
border_check()
Code: Alles auswählen
import turtle
from turtle import register_shape, Shape
import tkinter as tk
from PIL import Image, ImageTk
NORTH = 90
SOUTH = 270
EAST = 0
WEST = 180
MOVE_STEP = 10
ANGLE_STEP = 90
def forward():
turtle.forward(MOVE_STEP)
def backward():
turtle.backward(MOVE_STEP)
def nord():
turtle.setheading(NORTH)
def south():
turtle.setheading(SOUTH)
def west():
turtle.setheading(WEST)
def east():
turtle.setheading(EAST)
screen = turtle.Screen()
#img = ImageTk.PhotoImage(Image.open("Turtle.png"))
#register_shape("Bild", Shape("image", img))
# turtle.shape("Bild")
bg_image = tk.PhotoImage(file="labyrinth.png")
bg_image_width = bg_image.width()
bg_image_height = bg_image.height()
bg_image = tk.PhotoImage(file="labyrinth.png")
bg_image_width = bg_image.width()
bg_width_offset = bg_image_width / 2
bg_image_height = bg_image.height()
bg_height_offset = bg_image_height / 2
screen.setup(bg_image_width, bg_image_height)
canvas = screen.getcanvas()
canvas.create_image(0, 0, image=bg_image)
def border_check():
xpos, ypos = turtle.position()
bg_img_xpos = int(xpos + bg_width_offset)
bg_img_ypos = int(bg_image_height - bg_width_offset - ypos)
#print("Border Check:", bg_img_xpos, bg_img_ypos)
pixel_color = bg_image.get(bg_img_xpos, bg_img_ypos)
#print("Farbe:", pixel_color)
if pixel_color == (0, 0, 0):
turtle.goto(0, 0)
def coordinates():
xpos, ypos = turtle.position()
bg_img_xpos = int(xpos + bg_width_offset)
bg_img_ypos = int(bg_image_height - bg_width_offset - ypos)
pixel_color = bg_image.get(bg_img_xpos, bg_img_ypos)
print("Farbe:", pixel_color)
turtle.penup()
turtle.speed(0)
turtle.goto(0, 0)
#turtle.goto(-500, 500)
turtle.setheading(EAST)
turtle.showturtle()
# Keyboard actions
#screen.listen()
#screen.onkey(forward, "w")
#screen.onkey(backward, "s")
#screen.onkey(left, "a")
#screen.onkey(right, "d")
#screen.onkey(coordinates, "c")
screen.listen()
# Tastatureingabe für die Richtung
screen.onkey(nord, "w")
screen.onkey(south, "s")
screen.onkey(west, "a")
screen.onkey(east, "d")
screen.onkey(coordinates, "c")
# Tastatureingabe für die Bewegung
screen.onkey(forward, "Right")
screen.onkey(backward, "Left")
#fd = 1
#x = turtle.xcor()
#y = turtle.ycor()
#ende = False
#while not ende:
#turtle.forward(fd)
#border_check()
screen.listen()
turtle.mainloop()
#0.0 203.0
#999.0 0.0
#turtle.forward(fd)
#x = turtle.xcor()#+500
#y = turtle.ycor()#+500
#a = bg_image.get(x, y)
#if a == (0, 0, 0, 255):
#print("Schwarz")
#else:
#print("weiß")
#if a == (0, 0, 0, 0) or a == (0, 0, 0, 255):
#turtle.goto(0, 0)
#print(a)
Code: Alles auswählen
import turtle
from turtle import register_shape, Shape
import tkinter as tk
from PIL import Image, ImageTk
NORTH = 90
SOUTH = 270
EAST = 0
WEST = 180
MOVE_STEP = 10
ANGLE_STEP = 90
def forward():
turtle.forward(MOVE_STEP)
def backward():
turtle.backward(MOVE_STEP)
def nord():
turtle.setheading(NORTH)
def south():
turtle.setheading(SOUTH)
def west():
turtle.setheading(WEST)
def east():
turtle.setheading(EAST)
screen = turtle.Screen()
#img = ImageTk.PhotoImage(Image.open("Turtle.png"))
#register_shape("Bild", Shape("image", img))
# turtle.shape("Bild")
bg_image = tk.PhotoImage(file="labyrinth.png")
bg_image_width = bg_image.width()
bg_image_height = bg_image.height()
bg_image = tk.PhotoImage(file="labyrinth.png")
bg_image_width = bg_image.width()
bg_width_offset = bg_image_width / 2
bg_image_height = bg_image.height()
bg_height_offset = bg_image_height / 2
screen.setup(bg_image_width, bg_image_height)
canvas = screen.getcanvas()
canvas.create_image(0, 0, image=bg_image)
def border_check():
xpos, ypos = turtle.position()
bg_img_xpos = int(xpos + bg_width_offset)
bg_img_ypos = int(bg_image_height - bg_width_offset - ypos)
#print("Border Check:", bg_img_xpos, bg_img_ypos)
pixel_color = bg_image.get(bg_img_xpos, bg_img_ypos)
#print("Farbe:", pixel_color)
if pixel_color == (0, 0, 0):
turtle.goto(0, 0)
def coordinates():
xpos, ypos = turtle.position()
bg_img_xpos = int(xpos + bg_width_offset)
bg_img_ypos = int(bg_image_height - bg_width_offset - ypos)
pixel_color = bg_image.get(bg_img_xpos, bg_img_ypos)
print("Farbe:", pixel_color)
def turtle_automove_forward():
print('Hi')
forward()
canvas.after(500, turtle_automove_forward)
turtle.penup()
turtle.speed(0)
turtle.goto(0, 0)
#turtle.goto(-500, 500)
turtle.setheading(EAST)
turtle.showturtle()
# Keyboard actions
#screen.listen()
#screen.onkey(forward, "w")
#screen.onkey(backward, "s")
#screen.onkey(left, "a")
#screen.onkey(right, "d")
#screen.onkey(coordinates, "c")
screen.listen()
# Tastatureingabe für die Richtung
screen.onkey(nord, "w")
screen.onkey(south, "s")
screen.onkey(west, "a")
screen.onkey(east, "d")
screen.onkey(coordinates, "c")
# Tastatureingabe für die Bewegung
screen.onkey(forward, "Right")
screen.onkey(backward, "Left")
#fd = 1
#x = turtle.xcor()
#y = turtle.ycor()
#ende = True
#while not ende:
#forward()
##border_check()
turtle_automove_forward()
screen.listen()
turtle.mainloop()
#0.0 203.0
#999.0 0.0
#turtle.forward(fd)
#x = turtle.xcor()#+500
#y = turtle.ycor()#+500
#a = bg_image.get(x, y)
#if a == (0, 0, 0, 255):
#print("Schwarz")
#else:
#print("weiß")
#if a == (0, 0, 0, 0) or a == (0, 0, 0, 255):
#turtle.goto(0, 0)
#print(a)
Diese kleine Erweiterung löst es:Ok Danke aber hier funbktioniert border_check nicht.
Code: Alles auswählen
def turtle_automove_forward():
forward()
border_check()
canvas.after(500, turtle_automove_forward)
Code: Alles auswählen
import turtle
from turtle import register_shape, Shape
import tkinter as tk
from PIL import Image, ImageTk
def up():
turtle.setheading(90)
def down():
turtle.setheading(270)
def left():
turtle.setheading(180)
def right():
turtle.setheading(0)
screen = turtle.Screen()
bg_image = tk.PhotoImage(file="Labyrinth.png")
bg_image_width = bg_image.width()
bg_image_height = bg_image.height()
bg_image = tk.PhotoImage(file="labyrinth.png")
bg_image_width = bg_image.width()
bg_width_offset = bg_image_width / 2
bg_image_height = bg_image.height()
bg_height_offset = bg_image_height / 2
screen.setup(bg_image_width, bg_image_height)
canvas = screen.getcanvas()
canvas.create_image(0, 0, image=bg_image)
fd = 1
ende = False
def border_check():
xpos, ypos = turtle.position()
bg_img_xpos = int(xpos + bg_width_offset)
bg_img_ypos = int(bg_image_height - bg_width_offset - ypos)
#print("Border Check:", bg_img_xpos, bg_img_ypos)
pixel_color = bg_image.get(bg_img_xpos, bg_img_ypos)
print("Farbe:", pixel_color)
if pixel_color == (0, 0, 0):
turtle.goto(-499, 499)
down()
if pixel_color == (0, 255, 18):
ende = True
turtle.pu()
turtle.goto(-499, 499)
down()
# Keyboard actions
screen.listen()
screen.onkey(up, "w")
screen.onkey(down, "s")
screen.onkey(left, "a")
screen.onkey(right, "d")
while not ende:
turtle.forward(fd)
border_check()
Code: Alles auswählen
while not border_check():
turtle.forward(fd)
Code: Alles auswählen
import turtle
import tkinter as tk
NORTH = 90
SOUTH = 270
EAST = 0
WEST = 180
MOVE_STEP = 10
ANGLE_STEP = 90
MOVE_STEP_TIME = 500 # Milliseconds
def forward():
turtle.forward(MOVE_STEP)
border_touch = border_check()
if border_touch:
backward()
def backward():
turtle.backward(MOVE_STEP)
def nord():
turtle.shape("turtle_north.gif")
turtle.setheading(NORTH)
def south():
turtle.shape("turtle_south.gif")
turtle.setheading(SOUTH)
def west():
turtle.shape("turtle_west.gif")
turtle.setheading(WEST)
def east():
turtle.shape("turtle_east.gif")
turtle.setheading(EAST)
screen = turtle.Screen()
bg_image = tk.PhotoImage(file="labyrinth.png")
bg_image_width = bg_image.width()
bg_image_height = bg_image.height()
bg_image = tk.PhotoImage(file="labyrinth.png")
bg_image_width = bg_image.width()
bg_width_offset = bg_image_width / 2
bg_image_height = bg_image.height()
bg_height_offset = bg_image_height / 2
screen.setup(bg_image_width, bg_image_height)
canvas = screen.getcanvas()
canvas.create_image(0, 0, image=bg_image)
def border_check():
xpos, ypos = turtle.position()
bg_img_xpos = int(xpos + bg_width_offset)
bg_img_ypos = int(bg_image_height - bg_width_offset - ypos)
#print("Border Check:", bg_img_xpos, bg_img_ypos)
pixel_color = bg_image.get(bg_img_xpos, bg_img_ypos)
#print("Farbe:", pixel_color)
if pixel_color == (0, 0, 0):
#turtle.goto(0, 0)
border_touch = True
return border_touch
else:
border_touch = False
return border_touch
def coordinates():
xpos, ypos = turtle.position()
bg_img_xpos = int(xpos + bg_width_offset)
bg_img_ypos = int(bg_image_height - bg_width_offset - ypos)
pixel_color = bg_image.get(bg_img_xpos, bg_img_ypos)
print("Farbe:", pixel_color)
def turtle_automove_forward():
forward()
canvas.after(MOVE_STEP_TIME, turtle_automove_forward)
screen.register_shape("turtle_east.gif")
screen.register_shape("turtle_north.gif")
screen.register_shape("turtle_west.gif")
screen.register_shape("turtle_south.gif")
turtle.shape("turtle_east.gif")
turtle.penup()
turtle.speed(0)
turtle.goto(0, 0)
#turtle.goto(-500, 500)
turtle.setheading(EAST)
screen.listen()
# Tastatureingabe für die Richtung
screen.onkey(nord, "w")
screen.onkey(south, "s")
screen.onkey(west, "a")
screen.onkey(east, "d")
screen.onkey(coordinates, "c")
# Tastatureingabe für die Bewegung
screen.onkey(forward, "Right")
screen.onkey(backward, "Left")
turtle_automove_forward()
screen.listen()
turtle.mainloop()