4
\$\begingroup\$

Your Task

Write a program that displays a dot that moves toward the position of your mouse at 4 pixels per second. The dot must be 100 pixels big and the window must fill the computer screen.

Input

None

Output

A window showing an animated dot moving towards the mouse at a fixed, non-changing speed.

Scoring

This is , so the shortest code in bytes wins.

\$\endgroup\$
7
  • \$\begingroup\$ Edits that invalidate existing answers are not advised. Just sayin \$\endgroup\$
    – Gryphon
    Jun 12, 2017 at 0:11
  • 5
    \$\begingroup\$ What is the formula for speed? Is Manhattan distance allowed? \$\endgroup\$
    – ASCII-only
    Jun 12, 2017 at 0:11
  • \$\begingroup\$ Please leave it at "the window must fill the computer screen"; it's more objective anyway. \$\endgroup\$
    – MD XF
    Jun 12, 2017 at 0:11
  • \$\begingroup\$ I assume ruffle means roughly; however, this is also subjective, and we like 100% objective challenges here. \$\endgroup\$
    – MD XF
    Jun 12, 2017 at 0:15
  • \$\begingroup\$ thanks MD XF did not relies that things need to be 100% \$\endgroup\$
    – Foxy
    Jun 12, 2017 at 0:19

4 Answers 4

5
\$\begingroup\$

HTML + JavaScript, 140 bytes

Infinite speed

t=c.getContext`2d`
c.onmousemove=e=>(x=e.clientX,y=e.clientY)
setInterval(_=>t.clearRect(0,0,1e5,1e5)||t.fillRect(x,y,10,10),9)
<canvas id=c>

Non-cheaty, 188 bytes

x=y=0,t=c.getContext`2d`
c.onmousemove=e=>(z=e.pageX,w=e.pageY)
f=_=>t.clearRect(0,0,1e5,1e5)||(m=((a=z-x)**2+(b=w-y)**2)**.5)|t.fillRect(x+=a/m,y+=b/m,10,10)
setInterval(f,9)
<canvas id=c>

Larger and faster:

x=y=0,s=3,t=c.getContext`2d`
c.onmousemove=e=>(z=e.pageX,w=e.pageY)
f=_=>t.clearRect(0,0,1e5,1e5)||(m=((a=z-x)**2+(b=w-y)**2)**.5)|t.fillRect(x+=s*a/m,y+=s*b/m,10,10)
setInterval(f,9)
<canvas id=c height=999 width=999>

\$\endgroup\$
3
  • \$\begingroup\$ did you see my edit \$\endgroup\$
    – Foxy
    Jun 12, 2017 at 0:07
  • \$\begingroup\$ I think there is a problem with you second code ASCII-only it dos'nt follow my mose it just flouts above it \$\endgroup\$
    – Foxy
    Jun 12, 2017 at 0:16
  • \$\begingroup\$ never mind it works now \$\endgroup\$
    – Foxy
    Jun 12, 2017 at 0:22
5
\$\begingroup\$

Python 3 + Tkinter, 264 262 259 254 bytes

import tkinter as t,numpy as n
f=t.Tk()
p=n.array([1,1])
q=n.array([0.]*2)
c=t.Canvas(f)
c.pack()
def s(e):p[:]=e.x,e.y
c.bind("<Motion>",s)
z=c.create_oval(0,0,2,2)
def r():d=p-q;u=(d*d.T+.1)**-0.5;c.move(z,*u*d);q[:]+=u*d;c.after(9,r)
r()
f.mainloop()

Thanks to @Gryphon for saving 2 bytes, to @Jonathan Allan for another one, and to @Challenger5 for five. Changed 1e-9 to .1 for another two.

\$\endgroup\$
6
  • \$\begingroup\$ You can remove the space before the * to remove 2 bytes. \$\endgroup\$
    – Gryphon
    Jun 12, 2017 at 0:13
  • \$\begingroup\$ Welcome to PPCG! If I am reading it correctly (and the only references used are tkinter.Tk and two numpy.arrays) then import tkinter,numpy as n...f=tkinter.Tk()...p=n.array([1,1])...q=n.array([0.]*2) saves two. \$\endgroup\$ Jun 12, 2017 at 3:47
  • \$\begingroup\$ There is Canvas from tkinter as well, so it only saves a byte, but thanks! \$\endgroup\$
    – nore
    Jun 12, 2017 at 4:09
  • \$\begingroup\$ I don't have enough reputation to comment on the corresponding answer yet, but @Gryphon could save a few bytes by writing 4*(x>b)-2 in their solution. \$\endgroup\$
    – nore
    Jun 12, 2017 at 4:14
  • \$\begingroup\$ You can put the body of r in a single line to save indentation: def r():d=p-q;u=(d*d.T+.1)**-0.5;c.move(z,*u*d);q[:]+=u*d;c.after(9,r) \$\endgroup\$ Jun 12, 2017 at 5:42
3
\$\begingroup\$

Mathematica, 89 bytes

Graphics[Disk[Dynamic[MousePosition["Graphics",{0,0}]],.1],PlotRange->10,ImageSize->1000]

enter image description here

\$\endgroup\$
3
\$\begingroup\$

Python 2 + Pygame, 178 Bytes

-2 bytes thanks to @ASCII-only, because I was being an idiot

-12 bytes thanks to @officialaimm, also because I was being an idiot

-11 bytes thanks to @JonathanAllan, for telling me the default window size was the screen size

-2 bytes thanks to @WheatWizard, also because I was being an idiot

-18 bytes thanks to @nore, because I didn't have to use the if/else statements

-24 bytes thanks to @nore, who reminded me to put the whole loop on one line.

-42 bytes thanks to @WheatWizard

-3 bytes thanks to @ASCII-only

from pygame.locals import*
from pygame import*
init()
a=display.set_mode()
b=c=0
while 1:a.fill((0,0,0));x,y=mouse.get_pos();draw.rect(a,(9,9,9),
(b,c,b+10,c+10));display.update()

This is definitely no longer the best way to do this, so I may come out with a version that's better suited to the new requirements of the question. The dot is 10 pixels by 10 pixels. The screen size is the size of the computer screen. If you can see the dot, I'm impressed, as it is almost the exact same colour as the background, to save on byte counts for RGB colours.

\$\endgroup\$
12
  • \$\begingroup\$ Noncompeting status is reserved for languages that were made after the challenge; however, I rolled back the OP's useless edits. \$\endgroup\$
    – MD XF
    Jun 12, 2017 at 0:15
  • \$\begingroup\$ I'm assuming 100 pixels means 10x10 \$\endgroup\$
    – ASCII-only
    Jun 12, 2017 at 0:43
  • \$\begingroup\$ You can strip off few bytes by using: b+=2if x>b else-2 for updating b & c both. \$\endgroup\$ Jun 12, 2017 at 1:07
  • \$\begingroup\$ @officialaimm, Ummm, where? I'm looking and I can't see anywhere where I can do that, but I'm sure I'm just being an idiot again. \$\endgroup\$
    – Gryphon
    Jun 12, 2017 at 1:08
  • 1
    \$\begingroup\$ BTW you should be able to change while True to while 1 \$\endgroup\$
    – ASCII-only
    Jun 19, 2017 at 11:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.