Python Forum
Can't change Right to left id spawning/ movement to top to bottom
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't change Right to left id spawning/ movement to top to bottom
#1
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)
Reply
#2
def create_yblock():
   #x = WIDTH + GAP
    x = randint(0, WIDTH)
    ...

def move_yblocks():
    for i in range(len(yblock_id)):
       #c.move(yblock_id[i], -yblock_speed[i], 0)
        c.move(yblock_id[i], 0, yblock_speed[i])
Reply
#3
Is there a reason you're using tk for a game?  Eventually, you're going to find something that it simply cannot handle, so why not use a library that was designed for games, like http://pygame.org/news ?
Reply
#4
Quote:Eventually, you're going to find something that it simply cannot handle
I'd change Eventually to Soon
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x-axis text move bottom upward jacklee26 3 928 May-31-2023, 04:28 AM
Last Post: jacklee26
  how to make the movement humanized isv 4 1,502 Jul-29-2022, 07:14 PM
Last Post: XavierPlatinum
  python-docx regex : Browse the found words in turn from top to bottom Tmagpy 0 1,489 Jun-27-2022, 08:45 AM
Last Post: Tmagpy
  PyQT5 - align left frohr 7 3,835 May-07-2022, 09:56 PM
Last Post: deanhystad
  How did one column get left-justified? Mark17 6 1,877 Feb-26-2022, 11:55 PM
Last Post: deanhystad
  How to get continuous movement whilst using State Machine cjoe1993 2 1,787 Dec-10-2020, 06:36 AM
Last Post: cjoe1993
  Explanation of the left side of this statement please rascalsailor 3 2,451 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  How to left align logging messages Mekala 3 6,698 Jun-28-2020, 04:04 PM
Last Post: bowlofred
  Moving mouse so that games can detect movement SheeppOSU 2 1,898 Jun-09-2020, 07:23 PM
Last Post: SheeppOSU
  Spawning a new process that is not attached to python cman234 3 1,841 Apr-25-2020, 05:24 PM
Last Post: cman234

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020