Author Topic: Small planet system  (Read 1695 times)

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Small planet system
« on: April 22, 2013, 12:22:15 PM »
Just a some borrowed and changed code from QB64
Code: [Select]
screen (640,480,0,"Rotation")
setframetimer (120)

cls()

local v3=0
local v4=0

repeat

key = getkey()
for v=0,2*math.pi,0.008  do
    cls()
    newkey = getkey()
    v3 = v3+0.003
    v4 = v4 +0.01

    if v3 >= 2*math.pi then
       v3=0.003
    end
    if v4 >= 2*math.pi then
       v4=0.01
    end
    local x= cos (v3) * 180 + (cos(v) * -20 +320)
    local y= sin (v3) * 180 + (sin(v) * -20+240)
    local x2= cos (v3) * 180 + (cos(v) * 20 +320)
    local y2= sin (v3) * 180 + (sin(v) * 20+240)

    local x3= cos (v3) * 180 + (cos(v) * 0 +320)
    local y3= sin (v3) * 180 + (sin(v) * 0 +240)

    local x4 = cos (v) * 20+320
    local y4 = sin (v) * 20+240
    local x5 = cos (v4) * 80 + 320
    local y5 = sin (v4) * 80 +240

    colour (0,0,255)
    fillcircle (x3,y3,20)
    colour (0,0,0)
    line (x,y,x2,y2)
    colour (255,255,0)
    fillcircle (x4,y4,40)
    colour (200,200,200)
    fillcircle (x5,y5,10)
    if newkey==27 then
       os.exit()
    end
    sync()
end

until key==27

Tomaaz

  • Guest
Re: Small planet system
« Reply #1 on: April 22, 2013, 03:13:40 PM »
Just for curiosity - why do you use so many local keywords in your code? Shouldn't be better to write it like this?

Code: [Select]
screen (640,480,0,"Rotation")
setframetimer (120)

local v3, v4 = 0, 0
local x, y, x2, y2, x3, y3, x4, y4, x5, y5

while 1 do
for v=0,2*math.pi,0.008  do
cls()
key = getkey()
v3 = v3+0.003
v4 = v4 +0.01
if v3 >= 2*math.pi then
v3=0.003
end
if v4 >= 2*math.pi then
v4=0.01
end
x= cos (v3) * 180 + (cos(v) * -20 +320)
y= sin (v3) * 180 + (sin(v) * -20+240)
x2= cos (v3) * 180 + (cos(v) * 20 +320)
y2= sin (v3) * 180 + (sin(v) * 20+240)
x3= cos (v3) * 180 + (cos(v) * 0 +320)
y3= sin (v3) * 180 + (sin(v) * 0 +240)
x4 = cos (v) * 20+320
y4 = sin (v) * 20+240
x5 = cos (v4) * 80 + 320
y5 = sin (v4) * 80 +240
colour (0,0,255)
fillcircle (x3,y3,20)
colour (0,0,0)
line (x,y,x2,y2)
colour (255,255,0)
fillcircle (x4,y4,40)
colour (200,200,200)
fillcircle (x5,y5,10)
if key==27 then
os.exit()
end
sync()
end
end

EDIT: There is also no need to check key pressed twice.
« Last Edit: April 22, 2013, 03:23:57 PM by Tomaaz »