The dot is chasing me!

4

1

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.

Foxy

Posted 2017-06-11T23:44:39.357

Reputation: 249

Question was closed 2017-06-12T12:19:13.793

Edits that invalidate existing answers are not advised. Just sayin – Gryphon – 2017-06-12T00:11:09.267

5What is the formula for speed? Is Manhattan distance allowed? – ASCII-only – 2017-06-12T00:11:29.577

Please leave it at "the window must fill the computer screen"; it's more objective anyway. – MD XF – 2017-06-12T00:11:31.447

I assume ruffle means roughly; however, this is also subjective, and we like 100% objective challenges here. – MD XF – 2017-06-12T00:15:53.923

thanks MD XF did not relies that things need to be 100% – Foxy – 2017-06-12T00:19:22.880

IMO this question is really clear right now. What else is missing? Unfortunately, the answers dont match the question anymore. I like the concept behind this question. Maybe re-asking might be an idea? Any opinions on that? – phil294 – 2017-06-13T11:15:29.897

@Blauhirn Note that no edits were made between when I cast the final close-vote and all of these reopen votes. First of all, "100 pixels big" for a "dot" is very vague; is it a circle, a square, a star, or what? Secondly, should the dot trace the mouse's movement, or on each update move towards the mouse (real-time updating)? – HyperNeutrino – 2017-06-18T02:40:48.160

Answers

3

Mathematica, 89 bytes

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

enter image description here

J42161217

Posted 2017-06-11T23:44:39.357

Reputation: 15 931

5

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>

ASCII-only

Posted 2017-06-11T23:44:39.357

Reputation: 4 687

did you see my edit – Foxy – 2017-06-12T00:07:49.473

I think there is a problem with you second code ASCII-only it dos'nt follow my mose it just flouts above it – Foxy – 2017-06-12T00:16:57.510

never mind it works now – Foxy – 2017-06-12T00:22:27.870

5

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.

nore

Posted 2017-06-11T23:44:39.357

Reputation: 436

You can remove the space before the * to remove 2 bytes. – Gryphon – 2017-06-12T00:13:02.790

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. – Jonathan Allan – 2017-06-12T03:47:58.010

There is Canvas from tkinter as well, so it only saves a byte, but thanks! – nore – 2017-06-12T04:09:22.333

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. – nore – 2017-06-12T04:14:12.437

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) – Esolanging Fruit – 2017-06-12T05:42:29.937

Upvote for *u* – sagiksp – 2017-06-13T04:41:24.520

3

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.

Gryphon

Posted 2017-06-11T23:44:39.357

Reputation: 6 697

Noncompeting status is reserved for languages that were made after the challenge; however, I rolled back the OP's useless edits. – MD XF – 2017-06-12T00:15:11.443

I'm assuming 100 pixels means 10x10 – ASCII-only – 2017-06-12T00:43:22.463

You can strip off few bytes by using: b+=2if x>b else-2 for updating b & c both. – officialaimm – 2017-06-12T01:07:25.550

@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. – Gryphon – 2017-06-12T01:08:42.133

3rd and 4th lines within the while loop – officialaimm – 2017-06-12T01:14:55.620

I don't have pygame installed but the docs for pygame.display.set_mode say "If no resolution is passed or is set to (0, 0) and pygame uses SDL version 1.2.10 or above, the created Surface will have the same size as the current screen resolution.". Maybe an 11 byte save? (otherwise could they be floats like 1e5 saving two?) – Jonathan Allan – 2017-06-12T03:57:58.530

b,c=0,0 can be b=c=0 for a few bytes off. – Post Rock Garf Hunter – 2017-06-12T14:17:18.313

You can also use 4*(x>b)-2 (and same with y and c) to strip a few more bytes off. You can also probably put the whole loop body on a single line to save the indentation. – nore – 2017-06-13T04:34:49.633

Thanks, @nore. You saved me a total of 24 bytes! You also put me into third place, as of now. – Gryphon – 2017-06-13T10:17:58.983

Whats the deal with 4*(x>b)-2;4*(y>c)-2? As far as I can tell it doesn't do anything. Both of these lines just create value but neither of these values are used. – Post Rock Garf Hunter – 2017-06-19T03:37:09.110

You can also be far smarter with your imports, Here is a version with you inputs fixed up and my last change suggested.

– Post Rock Garf Hunter – 2017-06-19T03:44:57.430

1BTW you should be able to change while True to while 1 – ASCII-only – 2017-06-19T11:56:17.873