2017-01-21 9 views
0

Pythonでは、私は現在tkinterを使って 'ゲーム'を作っていますが、動作しないことに決めました。私がしようとしているのは、マウスで移動可能な1つの矩形と、何もしないで連続的に上下に動く他の矩形です。ここに私のコードです:形状を自動的に移動しますか? Python 3.5 Tkinter

from tkinter import * 
import time 
root = Tk() 
root.title("Game") 
root.geometry("800x800") 

def motion(): 
    canvas.delete(ALL) 
    a = canvas.create_rectangle(event.x-50, event.y-50, event.x+50, event.y+50, fill='red') 

def motion2(): 
    b = canvas.create_rectangle(10, 100, 100, 10, fill='blue') 
    y = -15 
    x = 0 
    time.sleep(0.025) 
    canvas.move(b, x, -y) 
    canvas.update() 

canvas = Canvas(root, width=1000, height=5000, bg='white') 
canvas.bind("<Motion>", motion) 
canvas.pack(pady=0) 

mainloop() 

私はこれがすぐに解決することを望みます。私はこの数日間、これに取り組んできましたが、まだ答えはありません。ありがとうございます:) - ジェイク

+1

使用 'root.after(ミリ秒、FUNCTION_NAME)'オブジェクトを自動的に移動します。 – furas

+1

オブジェクトを移動するには、オブジェクトを削除して再度作成する必要はありません。あなたは 'canvas.coords(object_id、...)'と 'canvas.move(object_id、...)'を持っています。より良いドキュメントを読む:すなわち。 http://effbot.org/tkinterbook/canvas.htm – furas

答えて

1

root.after(milliseconds, function_name)は定期的に機能を実行するために使用され、canvas.move(object_id, offset_x, offset_y)を使用するとオブジェクトを自動的に移動できます。

canvas.coords(object_id, x1, y1, x2, y2)を使用すると、マウスの位置を使用して新しい位置を設定できます。 bind exeecutesは1つの引数(オブジェクトevent)で機能するので、関数はこの引数を受け取る必要があります。

import tkinter as tk 

# --- functions --- 

def move_a(event): 
    canvas.coords(a, event.x-50, event.y-50, event.x+50, event.y+50) 

def move_b(): 
    canvas.move(b, 1, 0) 
    # move again after 25ms (0.025s) 
    root.after(25, move_b) 

# --- main --- 

# init 
root = tk.Tk() 

# create canvas 
canvas = tk.Canvas(root) 
canvas.pack() 

# create objects 
a = canvas.create_rectangle(0, 0, 100, 100, fill='red') 
b = canvas.create_rectangle(0, 0, 100, 100, fill='blue') 

# start moving `a` with mouse 
canvas.bind("<Motion>", move_a) 

# start moving `b` automatically 
move_b() 

# start program 
root.mainloop() 

EDIT:上に移動すると、ダウンあなたがそうあなたがspeedにagaint -speedspeedからそれを変更することができspeedで変数を使用する必要があります。そして、あなたはまた、現在の方向性を確認するためにmove_down = True/Falseを使用することができます(または、あなたは方向性を確認するためにspeedを使用することができますが、人々のために、より読みやすいb_move_downすることができます)

import tkinter as tk 

# --- functions --- 

def move_a(event): 
    canvas.coords(a, event.x-50, event.y-50, event.x+50, event.y+50) 

def move_b(): 
    # inform function to use external/global variable 
    # because we use `=` to change its value 
    global b_speed 
    global b_move_down 

    canvas.move(b, 0, b_speed) 

    # get current position   
    x1, y1, x2, y2 = canvas.coords(b) 

    # check if you have to change direction 
    #if b_speed > 0: 
    if b_move_down: 
     # if it reachs bottom 
     if y2 > 300: 
      # change direction 
      #b_move_down = False 
      b_move_down = not b_move_down 
      b_speed = -b_speed 
    else: 
     # if it reachs top 
     if y1 < 0: 
      # change direction 
      #b_move_down = True 
      b_move_down = not b_move_down 
      b_speed = -b_speed 

    # move again after 25 ms (0.025s) 
    root.after(25, move_b) 

# --- main --- 

# init 
root = tk.Tk() 

# create canvas 
canvas = tk.Canvas(root, width=500, height=300) 
canvas.pack() 

# create objects 
a = canvas.create_rectangle(0, 0, 100, 100, fill='red') 
b = canvas.create_rectangle(0, 0, 100, 100, fill='blue') 
# create global variables 
b_move_down = True 
b_speed = 5 

# start moving `a` with mouse 
canvas.bind("<Motion>", move_a) 

# start moving `b` automatically 
move_b() 

# start program 
root.mainloop() 

EDIT:キャンバスの周り移動

import tkinter as tk 

# --- functions --- 

def move_a(event): 
    canvas.coords(a, event.x-50, event.y-50, event.x+50, event.y+50) 

def move_b(): 
    # inform function to use external/global variable 
    # because we use `=` to change its value 
    global b_speed_x 
    global b_speed_y 
    global b_direction 

    canvas.move(b, b_speed_x, b_speed_y) 

    # get current position   
    x1, y1, x2, y2 = canvas.coords(b) 

    if b_direction == 'down': 
     if y2 >= 300: 
      b_direction = 'right' 
      b_speed_x = 5 
      b_speed_y = 0 
    elif b_direction == 'up': 
     if y1 <= 0: 
      b_direction = 'left' 
      b_speed_x = -5 
      b_speed_y = 0 
    elif b_direction == 'right': 
     if x2 >= 500: 
      b_direction = 'up' 
      b_speed_x = 0 
      b_speed_y = -5 
    elif b_direction == 'left': 
     if x1 <= 0: 
      b_direction = 'down' 
      b_speed_x = 0 
      b_speed_y = 5 

    # move again after 25 ms (0.025s) 
    root.after(25, move_b) 

# --- main --- 

# init 
root = tk.Tk() 

# create canvas 
canvas = tk.Canvas(root, width=500, height=300) 
canvas.pack() 

# create objects 
a = canvas.create_rectangle(0, 0, 100, 100, fill='red') 
b = canvas.create_rectangle(0, 0, 100, 100, fill='blue') 
# create global variables 
b_direction = 'down' 
b_speed_x = 0 
b_speed_y = 5 

# start moving `a` with mouse 
canvas.bind("<Motion>", move_a) 

# start moving `b` automatically 
move_b() 

# start program 
root.mainloop() 

編集:最後の例 - k `)(`とtime.sleepを削除 - EY pポーズゲーム

#!/usr/bin/env python3 

import tkinter as tk 

# --- constants --- (UPPER_CASE names) 

DISPLAY_WIDHT = 800 
DISPLAY_HEIGHT = 600 

# --- classes --- (CamelCase names) 

#class Player(): 
# pass 

#class BlueEnemy(): 
# pass 

# --- functions --- (lower_case names) 

def move_a(event): 
    # don't move if gama paused 
    if not game_paused: 
     canvas.coords(a, event.x-50, event.y-50, event.x+50, event.y+50) 

def move_b(): 
    # inform function to use external/global variable 
    # because we use `=` to change its value 
    global b_speed_x 
    global b_speed_y 
    global b_direction 

    # don't move if gama paused 
    if not game_paused: 
     canvas.move(b, b_speed_x, b_speed_y) 

     # get current position 
     x1, y1, x2, y2 = canvas.coords(b) 

     if b_direction == 'down': 
      if y2 >= DISPLAY_HEIGHT: 
       b_direction = 'right' 
       b_speed_x = 5 
       b_speed_y = 0 
     elif b_direction == 'up': 
      if y1 <= 0: 
       b_direction = 'left' 
       b_speed_x = -5 
       b_speed_y = 0 
     elif b_direction == 'right': 
      if x2 >= DISPLAY_WIDHT: 
       b_direction = 'up' 
       b_speed_x = 0 
       b_speed_y = -5 
     elif b_direction == 'left': 
      if x1 <= 0: 
       b_direction = 'down' 
       b_speed_x = 0 
       b_speed_y = 5 

    # move again after 25 ms (0.025s) 
    root.after(25, move_b) 

def pause(event): 
    global game_paused 

    # change True/False 
    game_paused = not game_paused 

    if game_paused: 
     # center text on canvas 
     canvas.coords(text_pause, DISPLAY_WIDHT//2, DISPLAY_HEIGHT//2) 
    else: 
     # move text somewhere outside canvas 
     canvas.coords(text_pause, -1000, -1000) 

# --- main --- (lower_case names) 

# init 
root = tk.Tk() 

# key `p` pause game 
game_paused = False 
root.bind('p', pause) 

# create canvas 
canvas = tk.Canvas(root, width=DISPLAY_WIDHT, height=DISPLAY_HEIGHT) 
canvas.pack() 

# create objects 
a = canvas.create_rectangle(0, 0, 100, 100, fill='red') 
b = canvas.create_rectangle(0, 0, 100, 100, fill='blue') 
# create global variables 
b_direction = 'down' 
b_speed_x = 0 
b_speed_y = 5 

# start moving `a` with mouse 
canvas.bind("<Motion>", move_a) 

# start moving `b` automatically 
move_b() 

# create text somewhere outside canvas - so it will be "invisible" 
text_pause = canvas.create_text(-1000, -1000, text="PAUSED", font=(50,)) 

# start program 
root.mainloop() 
+0

私はそれを連続的に上下に動かして、それが決してそれをやめないことを意味しました。しかし、答えをありがとう。 – Jake

+0

それを止めたり方向を変えたりするには、 'move_up = True'のような変数と' if/else'のような変数を使って方向を変更する必要があります。 – furas

+0

これをどのようにして持続させるのですか? – Jake

関連する問題