Nov-07-2017, 03:58 AM
Ive been trying to change the way my yblocks in my game move, they go (right to left) at this point but I want them to move (top to bottom) but I can't figure out how, please help me change this code so the yblocks flow or move from (top to bottom) name of block id:(yblock) my code (so far):
from tkinter import *
from random import randint
from time import sleep, time
from math import sqrt
HEIGHT = 1000
WIDTH = 900
window = Tk()
window.title('Strange Game')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='black')
c.pack()
block_id = c.create_rectangle(0, 0, 40, 40, outline='red', fill='red')
BLOCK_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(block_id, MID_X, MID_Y)
BLOCK_SPD = 15
yblock_id = list()
yblock_r = list()
yblock_speed = list()
MIN_YBLOCK_R = 5
MAX_YBLOCK_R = 10
MAX_YBLOCK_SPD = 2
GAP = 100
YBLOCK_CHANCE = 100
def move_block(event):
if event.keysym == 'Up':
c.move(block_id, 0, -BLOCK_SPD)
elif event.keysym == 'Down':
c.move(block_id, 0, BLOCK_SPD)
elif event.keysym == 'Left':
c.move(block_id, -BLOCK_SPD, 0)
elif event.keysym == 'Right':
c.move(block_id, BLOCK_SPD, 0)
c.bind_all('<Key>', move_block)
def create_yblock():
x = WIDTH + GAP
y = randint(0, HEIGHT)
r = randint(MIN_YBLOCK_R, MAX_YBLOCK_R)
id1 = c.create_rectangle(x - r, y - r, x + r, y + r, outline='yellow')
yblock_id.append(id1)
yblock_r.append®
yblock_speed.append(randint(1, MAX_YBLOCK_SPD))
def move_yblocks():
for i in range(len(yblock_id)):
c.move(yblock_id[i], -yblock_speed[i], 0)
def get_coords(id_num):
pos = c.coords(id_num)
x = (pos[0] + pos[2])/2
y = (pos[1] + pos[3])/2
return x, y
def del_yblock(i):
del yblock_r[i]
del yblock_speed[i]
c.delete(yblock_id[i])
del yblock_id[i]
def distance(id1, id2):
x1, y1 = get_coords(id1)
x2, y2 = get_coords(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def clean_up_yblocks():
for i in range(len(yblock_id)-1, -1, -1):
x, y = get_coords(yblock_id[i])
if x < -GAP:
del_yblock(i)
#MAIN GAME LOOP
while True:
if randint(1, YBLOCK_CHANCE) == 1:
create_yblock()
move_yblocks()
window.update()
sleep(0.01)
from tkinter import *
from random import randint
from time import sleep, time
from math import sqrt
HEIGHT = 1000
WIDTH = 900
window = Tk()
window.title('Strange Game')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='black')
c.pack()
block_id = c.create_rectangle(0, 0, 40, 40, outline='red', fill='red')
BLOCK_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(block_id, MID_X, MID_Y)
BLOCK_SPD = 15
yblock_id = list()
yblock_r = list()
yblock_speed = list()
MIN_YBLOCK_R = 5
MAX_YBLOCK_R = 10
MAX_YBLOCK_SPD = 2
GAP = 100
YBLOCK_CHANCE = 100
def move_block(event):
if event.keysym == 'Up':
c.move(block_id, 0, -BLOCK_SPD)
elif event.keysym == 'Down':
c.move(block_id, 0, BLOCK_SPD)
elif event.keysym == 'Left':
c.move(block_id, -BLOCK_SPD, 0)
elif event.keysym == 'Right':
c.move(block_id, BLOCK_SPD, 0)
c.bind_all('<Key>', move_block)
def create_yblock():
x = WIDTH + GAP
y = randint(0, HEIGHT)
r = randint(MIN_YBLOCK_R, MAX_YBLOCK_R)
id1 = c.create_rectangle(x - r, y - r, x + r, y + r, outline='yellow')
yblock_id.append(id1)
yblock_r.append®
yblock_speed.append(randint(1, MAX_YBLOCK_SPD))
def move_yblocks():
for i in range(len(yblock_id)):
c.move(yblock_id[i], -yblock_speed[i], 0)
def get_coords(id_num):
pos = c.coords(id_num)
x = (pos[0] + pos[2])/2
y = (pos[1] + pos[3])/2
return x, y
def del_yblock(i):
del yblock_r[i]
del yblock_speed[i]
c.delete(yblock_id[i])
del yblock_id[i]
def distance(id1, id2):
x1, y1 = get_coords(id1)
x2, y2 = get_coords(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def clean_up_yblocks():
for i in range(len(yblock_id)-1, -1, -1):
x, y = get_coords(yblock_id[i])
if x < -GAP:
del_yblock(i)
#MAIN GAME LOOP
while True:
if randint(1, YBLOCK_CHANCE) == 1:
create_yblock()
move_yblocks()
window.update()
sleep(0.01)