在<10行简单代码中可以做的最酷的事情是什么? 帮助我激发初学者!
我正在寻找几行简单代码中最酷的东西。 我敢肯定,你可以在15行的Haskell中写一个Mandelbrot集,但是很难跟踪。
我的目标是激发学生编程很酷 。
我们知道编程很酷,因为您可以创build任何您想象的东西 – 这是最终的创意出路。 我想激励这些初学者,并尽可能多地学习早期的学习。
现在,我的理由是自私的。 我正在教授“ 计算入门”课程,课程包括60个半工半读的专业; 所有新生。 他们是来自贫困高中的学生。 从我以往的经验来看,这个小组大体上分为:几个摇滚明星 ,一些非常努力的人,一些很努力, 很less拿的人,还有一些不在乎的人。 我想尽可能多地接触到这些团体。 下面是一个如何使用计算机程序教学的例子:
下面是我正在寻找的一个例子:1行VBS脚本让您的计算机与您交谈:
CreateObject("sapi.spvoice").Speak InputBox("Enter your text","Talk it")
我可以用这个来演示操作顺序。 我会显示代码,让他们玩,然后解释说,有很多事情在这一行,但计算机可以理解它,因为它知道规则。 然后我会向他们展示这样的事情:
4(5*5) / 10 + 9(.25 + .75)
你可以看到,首先我需要做的是(5 * 5)。 然后我可以乘以4.现在我已经创build了对象。 除以10与调用Speak相同 – 在有对象之前我不会说话,而在100之前我不能分开。然后在另一边,我首先创build一个InputBox,其中包含一些如何显示它的指令。 当我在input框中input时,它会评估或“返回”我input的内容。 (提示:'oooooo'发出一个有趣的声音)所以当我说话的时候,右边是说什么。 我从InputBox中获取。
所以当你在一行上做了几件事情,比如:
x = 14 + y;
你需要了解事物的顺序。 首先我们添加14和y。 然后我们把结果(它所评估的或返回的)放到x中。
这是我的目标,有一堆这些很酷的例子来展示和教课,而他们玩得很开心。 我在室友身上试过这个例子,虽然我可能不会把这个作为第一课,但是她喜欢它并学到了一些东西。
一些很酷的mathematica程序,使美丽的graphics或易于理解的形状将是好主意,我会去看看那些。 这里有一些复杂的动作例子,但这有点太先进,我不能教Flash。 你还有什么其他想法?
我用一个快速的VB脚本来处理一个微软代理angular色,从我的孩子那里得到了很好的回应。 对于那些不熟悉MS Agent的人来说,它是一系列可以通过COM接口操作的animation屏幕上的字符。 您可以在Microsoft Agent下载页面下载代码和字符。
下面几行将使Merlinangular色出现在屏幕上,飞来飞去,敲击屏幕以引起你的注意,并打招呼。
agentName = "Merlin" agentPath = "c:\windows\msagent\chars\" & agentName & ".acs" Set agent = CreateObject("Agent.Control.2") agent.Connected = TRUE agent.Characters.Load agentName, agentPath Set character = agent.Characters.Character(agentName) character.Show character.MoveTo 500, 400 character.Play "GetAttention" character.Speak "Hello, how are you?" Wscript.Sleep 15000 character.Stop character.Play "Hide"
有很多其他的命令可以使用。 查看http://www.microsoft.com/technet/scriptcenter/funzone/agent.mspx了解更多信息。;
编辑2011-09-02我最近发现,微软代理不是本地安装在Windows 7上。但它是作为一个单独的下载在这里提供 。 我没有testing过,所以无法validation它是否运行。
在地址栏中input此代码(在浏览器中),然后按Enter键。 那么你可以编辑网页的所有内容!
javascript:document.body.contentEditable='true'; document.designMode='on'; void 0
这是我知道的最酷的“单线”=)
当我第一次写这个。
10 PRINT "What is your name?" 20 INPUT A$ 30 PRINT "Hello " A$ 40 GOTO 30
它吹走了人们! 电脑记得他们的名字!
编辑:只需添加到此。 如果你能说服一个新的程序员,这是他们能做的最酷的事情,他们将成为优秀的程序员。 现在,你可以用一行代码来做任何你想做的事情来运行别人写的一个库。 我个人绝对没有这样做的满意,并没有看到教学的好处。
PHP – Sierpinski垫圈又名Triforce
好吧,这是15行代码,但结果是真棒! 那是当我还是个孩子的时候让我发疯的那种东西。 这是从PHP手册:
$x = 200; $y = 200; $gd = imagecreatetruecolor($x, $y); $corners[0] = array('x' => 100, 'y' => 10); $corners[1] = array('x' => 0, 'y' => 190); $corners[2] = array('x' => 200, 'y' => 190); $red = imagecolorallocate($gd, 255, 0, 0); for ($i = 0; $i < 100000; $i++) { imagesetpixel($gd, round($x),round($y), $red); $a = rand(0, 2); $x = ($x + $corners[$a]['x']) / 2; $y = ($y + $corners[$a]['y']) / 2; } header('Content-Type: image/png'); imagepng($gd);
微软已经推出Small Basic ,一款面向“小孩”的IDE。
pic = Flickr.GetRandomPicture("beach") Desktop.SetWallpaper(pic)
它是专门devise来显示如何酷编程。
我倾向于认为人们对于他们可以涉及的或与他们的生活有关的东西印象深刻。 我会试着把我的10行代码放在他们知道并理解的东西的周围。 以Twitter和它的API为例。 为什么不使用这个API来构build一个很酷的东西。 以下10行代码将从Twitter返回“公共时间线”,并将其显示在控制台应用程序中。
using (var xmlr = XmlReader.Create("http://twitter.com/statuses/public_timeline.rss")) { SyndicationFeed .Load(xmlr) .GetRss20Formatter() .Feed .Items .ToList() .ForEach( x => Console.WriteLine(x.Title.Text)); }
我的代码示例可能不是最适合您的学生的。 它是用C#编写的,使用.NET 3.5。 所以,如果你要教他们PHP,Java或C ++,这将是没有用的。 然而,我的观点是,通过将10行代码与“对学生来说很酷,有趣且相关的东西”联系起来,您的示例也变得很酷,有趣且相关。
祝你好运!
[是的,我知道我已经错过了使用语句和Main方法的几行,但是我猜测10行不需要10行]
这是一个Python远程login服务器,会询问用户名并向他们打招呼。 这看起来很酷,因为您正通过networking与另一台计算机上的程序进行通信。
from socket import * s=socket(AF_INET, SOCK_STREAM) s.bind(("", 3333)) s.listen(5) while 1: (c, a) = s.accept() c.send("What is your name? ") name = c.recv(100) c.send("Hello "+name) c.close()
我觉得现在很难成为电脑教育工作者。 我是。 我们面临着越来越陡峭的艰苦战斗。 我们的学生是令人难以置信的复杂用户 ,需要很多才能打动他们。 他们有这么多的工具可以让他们做出令人惊叹的事情。
一个简单的计算器在10行代码? 为什么? 我有一个TI-86的。
将特殊效果应用于图像的脚本? 这是Photoshop的用途。 Photoshop可以吹掉10行以上的任何东西。
如何翻录CD并将文件转换为MP3? 呃,我已经从BitTorrent得到了5万首歌。 他们已经在MP3格式。 我用我的iPhone玩他们。 谁买CD?
要介绍精明的用户编程,你将不得不find的东西是:
a)适用于他们觉得有趣和酷的东西,b)做一些他们不能做的事情。
假设您的学生已经可以访问最昂贵的软件。 他们中的许多人确实有完整版本的Adobe CS5.5(零售价:2,600美元;实际价格:免费),并且可以轻松地获得任何通常会打破部门预算的应用程序。
但绝大多数人不知道这些“电脑东西”究竟是如何工作的。
他们是一个令人难以置信的创意群体:他们喜欢创造的东西。 他们只是希望能够做或做出他们的朋友不能做的事情。 他们想要吹嘘什么。
以下是我发现与学生共鸣的一些事情:
- HTML和CSS。 从那些他们了解如何MySpace主题工作,并可以自定义它们。
- 混搭。 他们都看过他们,但不知道如何去创造他们。 检查雅虎! pipe道 。 有很多可教的时刻,比如RSS,XML,文本过滤,映射和可视化。 完成的mashup小部件可以embedded到网页中。
- 艺术品。 看看上下文无关的艺术 。 recursion和随机化是制作漂亮图片的关键。
- 评书。 通过使用像爱丽丝这样的易于使用的3D编程环境,只需使用拖放function即可轻松创build高质量,引人入胜的故事。
这些都不涉及传统意义上的编程。 但他们确实利用强大的图书馆。 我认为它们是一种不同的编程方式。
我发现最喜欢的(在GWBASIC)是:
10 input "What is your name ";N$ 20 i = int(rnd * 2) 30 if i = 0 print "Hello ";N$;". You are a <fill in insult number 1>" 40 if i = 1 print "Hello ";N$;". You are a <fill in insult number 2>"
我发现刚开始的学生有一些需要修正的概念。
- 电脑不读你的思想。
- 电脑一次只做一件事,即使它们做得太快,它们似乎一次就能做完。
- 电脑只是愚蠢的机器,只做他们所说的。
- 电脑只识别某些东西,而这些就像积木。
- 一个关键的概念是variables是包含一个值,它的名字是不同的值。
- 编辑程序的时间和运行时间之间的区别。
祝你的class级好运。 我相信你会做得很好。
PS我相信你明白,除了材料和技巧之外,你也在教导一种态度,这一点同样重要。
这个C代码可能被混淆了,但我发现它非常强大
#include <unistd.h> float o=0.075,h=1.5,T,r,O,l,I;int _,L=80,s=3200;main(){for(;s%L|| (h-=o,T= -2),s;4 -(r=O*O)<(l=I*I)|++ _==L&&write(1,(--s%L?_<L?--_ %6:6:7)+"World! \n",1)&&(O=I=l=_=r=0,T+=o /2))O=I*2*O+h,I=l+Tr;}
这里是结果…只有3行…一种分形Hello World
…
WWWWWWWWWWWWWWWWooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo WWWWWWWWWWWWWWooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo WWWWWWWWWWWWWooooooooooooooooorrrrrrrrrrrrrrrrrrrrroooooooooooooooooooooooooooo WWWWWWWWWWWoooooooooooorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrooooooooooooooooooooo WWWWWWWWWWooooooooorrrrrrrrrrrrrrrrrrrrrrrllllld!!ddllllrrrrrrooooooooooooooooo WWWWWWWWoooooooorrrrrrrrrrrrrrrrrrrrrrllllllldd!oWW!!dllllllrrrrroooooooooooooo WWWWWWWoooooorrrrrrrrrrrrrrrrrrrrrrlllllllldddd!orro!o!dllllllrrrrrrooooooooooo WWWWWWooooorrrrrrrrrrrrrrrrrrrrrllllllllldddd!WorddddoW!ddllllllrrrrrrooooooooo WWWWWoooorrrrrrrrrrrrrrrrrrrrrlllllllllddd!!!o!!! !dWW!ddddllllrrrrrrrooooooo WWWWooorrrrrrrrrrrrrrrrrrrrllllllllldd!!!!WWWoo WloW!!!ddddllrrrrrrrrooooo WWWWoorrrrrrrrrrrrrrrrrrrlllllllddddWldolrrlo!Wl r!dlooWWWoW!dllrrrrrrroooo WWWoorrrrrrrrrrrrrrrrrlllllddddddd!!Wdo l! rdo!l!r!dlrrrrrrrrooo WWoorrrrrrrrrrrrrrrlllddddddddd!!!!oolWW lW!ddlrrrrrrrroo WWorrrrrrrrrrrrllld!!!!!dddd!!!!WWrd ! rlW!ddllrrrrrrrro Worrrrrrrllllllddd!oooWWWoloWWWWoodr drrWdlllrrrrrrrr Worrrlllllllldddd!WolWrr!!dWWWlrrldr ro!dlllrrrrrrrr Wrrllllllllddddd!WWolWr oWoo r!dllllrrrrrrr Wlllllllldddd!!odrrdW o lWddllllrrrrrrr Wlddddd!!!!!WWordlWrd oW!ddllllrrrrrrr olddddd!!!!!WWordlWrd oW!ddllllrrrrrrr Wlllllllldddd!!odrrdW o lWddllllrrrrrrr Wrrllllllllddddd!WWolWr oWoo r!dllllrrrrrrr Worrrlllllllldddd!WolWrr!!dWWWlrrldr ro!dlllrrrrrrrr Worrrrrrrllllllddd!oooWWWoloWWWWoodr droWdlllrrrrrrrr WWorrrrrrrrrrrrllld!!!!!dddd!!!!WWrd ! rlW!ddllrrrrrrrro WWoorrrrrrrrrrrrrrrlllddddddddd!!!!oolWW lW!ddlrrrrrrrroo WWWoorrrrrrrrrrrrrrrrrlllllddddddd!!Wdo l! rdo!l!r!dlrrrrrrrrooo WWWWoorrrrrrrrrrrrrrrrrrrlllllllddddWldolrrlo!Wl r!dlooWWWoW!dllrrrrrrroooo WWWWooorrrrrrrrrrrrrrrrrrrrllllllllldd!!!!WWWoo WloW!!!ddddllrrrrrrrrooooo WWWWWoooorrrrrrrrrrrrrrrrrrrrrlllllllllddd!!!o!!! WdWW!ddddllllrrrrrrrooooooo WWWWWWooooorrrrrrrrrrrrrrrrrrrrrllllllllldddd!WorddddoW!ddllllllrrrrrrooooooooo WWWWWWWoooooorrrrrrrrrrrrrrrrrrrrrrlllllllldddd!orro!o!dllllllrrrrrrooooooooooo WWWWWWWWoooooooorrrrrrrrrrrrrrrrrrrrrrllllllldd!oWW!!dllllllrrrrroooooooooooooo WWWWWWWWWWooooooooorrrrrrrrrrrrrrrrrrrrrrrllllld!!ddllllrrrrrrooooooooooooooooo WWWWWWWWWWWoooooooooooorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrooooooooooooooooooooo WWWWWWWWWWWWWooooooooooooooooorrrrrrrrrrrrrrrrrrrrroooooooooooooooooooooooooooo WWWWWWWWWWWWWWooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo WWWWWWWWWWWWWWWWooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo WWWWWWWWWWWWWWWWWWWoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo WWWWWWWWWWWWWWWWWWWWWoooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
如何显示您可以使用任何networking浏览器并在地址栏中inputJavaScript并获取代码执行?
编辑:转到有很多图像的页面,并尝试在地址栏中:
javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=document.images; DIL=DI.length; function A(){for(i=0; i<DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=Math.sin(R*x1+i*x2+x3)*x4+x5; DIS.top=Math.cos(R*y1+i*y2+y3)*y4+y5}R++ }setInterval('A()',5); void(0)
你可以创build一个随机数的应用程序。 你必须猜测它。 如果你错了,说:更高或更低。 如果你猜,这是一个很好的信息。
为学生打球很酷。
简单的Python版本没有适当的错误检查:
import random while input('Want to play higher/lower? ').lower().startswith('y'): n = random.randint(1, 100) g = int(input('Guess: ')) while g != n: print(' %ser!' % (g > n and 'low' or 'high')) g = int(input('Guess: ')) print(' Correct! Congratulations!')
Erikbuild议计算机应该猜出这个数字。 这可以在10行代码中完成(虽然现在缺less适当的错误检查更为严重:范围之外的有效数字导致无限循环):
while input('Want to let the pc play higher/lower? ').lower().startswith('y'): n = int(input('Give a number between 1 and 100: ')) lo, hi, guess, tries = 1, 100, 50, 1 while guess != n: tries += 1 lo, hi = (guess + 1, hi) if guess < n else (lo, guess - 1) guess = (lo + hi) // 2 print('Computer guessed number in %d tries' % tries)
回到高中的计算机课,我和一些朋友教授class级如何使用Delphi进行编程。 这个课程主要集中在Pascal编程上,所以Delphi是一个很好的下一步。 我们展示了Delphi的事件驱动特性及其RADfunction。 在课程结束时,我们向class级展示了一个示例应用程序,并要求他们复制它。 应用程序询问“ 你喝醉了吗? ”有两个button是和否 。 …我想你知道接下来会发生什么… Nobutton改变了鼠标位置, 几乎不可能点击。
学生和老师得到了很好的解决。
该程序只需要几行用户编写的代码,用一个简单的公式来计算button的移动位置。 我想其他的学生都没有想到,但是其中有一些接近。
当我第一次想到这个东西时,我觉得它真的很好。 如此简单,但它可以做到的整洁:
:(){ :|:& };:
这是作弊,甚至不是那么简单,但我曾经使用Allegrographics库在20行C ++中写了一个shoot'em。 没有一个真正的标准,但它是一个前一个,而它是纯粹的乐趣。 它甚至有粗糙的声音效果。
以下是它的样子:
20行http://img227.imageshack.us/img227/8770/20linesxx0.png
这里的代码( 应该编译):
bool inside(int x, int y, int x2, int y2) { return (x>x2&&x<x2+20&&y>y2&&y<y2+10); } int main() { BITMAP* buffer; float px,shotx,shoty,monstars[8],first,rnd,pressed,points = 0, maxp = 0; unsigned char midi[5] = {0xC0,127,0x90,25,0x54}, plgfx[] = {0,0,0,10,3,10,3,5,6,5,6,10,8,12,10,10,10,5,13,5,13,10,16,10,16,0,13,0,13,2,3,2,3,0,0,0}, mongfx[] = {0,0, 10,5, 20,0, 17,8, 15,6, 10,16, 5,6, 3,8, 0,0}; allegro_init(), set_color_depth(32), set_gfx_mode(GFX_AUTODETECT_WINDOWED,320,240,0,0), install_timer(), install_keyboard(), install_mouse(), buffer = create_bitmap(320,240),srand(time(NULL)),install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT,""),clear_to_color(buffer,makecol32(100,100,255)); while ((pressed=(!key[KEY_Z]&&pressed)?0:pressed)?1:1&&(((shoty=key[KEY_Z]&&shoty<0&&pressed==0?(pressed=1?200:200):first==0?-1:shoty)==200?shotx=px+9:0)==9999?1:1) && 1+(px += key[KEY_LEFT]?-0.1:0 + key[KEY_RIGHT]?0.1:0) && 1+int(px=(px<0?0:(px>228?228:px))) && !key[KEY_ESC]) { rectfill(buffer,0,0,244,240,makecol32(0,0,0)); for(int i=0;i<8;i++) if (inside(shotx,shoty,i*32,monstars[i])) midi_out(midi,5); for (int i=0; i<8; monstars[i] += first++>8?(monstars[i]==-100?0:0.02):-100, points = monstars[i]>240?points-1:points, monstars[i]=monstars[i]>240?-100:monstars[i], points = inside(shotx,shoty,i*32,monstars[i])?points+1:points, (monstars[i] = inside(shotx,shoty,i*32,monstars[i])?shoty=-1?-100:-100:monstars[i]), maxp = maxp>points?maxp:points, i++) for (int j=1; j<9; j++) line(buffer,i*32+mongfx[j*2 - 2],monstars[i]+mongfx[j*2-1],i*32+mongfx[j*2],monstars[i]+mongfx[j*2+1],makecol32(255,0,0)); if (int(first)%2000 == 0 && int(rnd=float(rand()%8))) monstars[int(rnd)] = monstars[int(rnd)]==-100?-20:monstars[int(rnd)]; // randomowe pojawianie potworkow if (shoty>0) rectfill(buffer,shotx,shoty-=0.1,shotx+2,shoty+2,makecol32(0,255,255)); // rysowanie strzalu for (int i=1; i<18; i++) line(buffer,px+plgfx[i*2 - 2],200-plgfx[i*2-1],px+plgfx[i*2],200-plgfx[i*2+1],makecol32(255,255,0)); textprintf_ex(buffer,font,250,10,makecol32(255,255,255),makecol32(100,100,255),"$: %i ",int(points)*10); textprintf_ex(buffer,font,250,20,makecol32(255,255,255),makecol32(100,100,255),"$$ %i ",int(maxp)*10); blit(buffer, screen, 0, 0, 0, 0, 320,240); } } END_OF_MAIN()
In this day and age, JavaScript is an excellent way to show how you can program using some really basic tools eg notepad.
jQuery effects are great starting point for anyone wanting to wow their friends!
In this one, just click the white space of the page.
<html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script> $(document.body).click(function () { if ($("#pic").is(":hidden")) { $("#pic").slideDown("slow"); } else { $("#pic").slideUp(); } }); </script> </head> <body><img id="pic" src="http://www.smidgy.com/smidgyhttp://img.dovov.com2007/07/26/lol_cat_icanhascheezburger.jpg"/> </body> </html>
One thing you might consider is something like Robocode , in which a lot of coding is abstracted away and you basically just tell a robot what to do. A simple 10-line function can make the robot do a great deal, and has a very visual and easy-to-follow result.
Perhaps Robocode itself isn't suited to the task, but this kind of thing is a good way to relate written code to visual actions on the computer, plus it's fun to watch for when you need to give examples.
public class MyFirstJuniorRobot extends JuniorRobot { public void run() { setColors(green, black, blue); // Seesaw forever while (true) { ahead(100); // Move ahead 100 turnGunRight(360); // Spin gun around back(100); // Move back 100 turnGunRight(360); // Spin gun around } } public void onScannedRobot() { turnGunTo(scannedAngle); fire(1); } public void onHitByBullet() { turnAheadLeft(100, 90 - hitByBulletBearing); } }
So one day, I decided that I'd had enough. I would learn piano. Seeing people like Elton John command such mastery of the keyboard assured me that this was what I wanted to do.
Actually learning piano was a huge letdown. Even after completing eight grades of piano lessons, I was still not impressed with how my mental image of playing piano was so different from my original vision of enjoying the activity.
However, what I thoroughly enjoyed was my mere three grades of rudiments of music theory. I learned about the construction of music. I was finally able to step from the world of performing written music to writing my own music. Subsequently, I was able to start playing what I wanted to play.
Don't try to dazzle new programmers, especially young programmers. The whole notion of "less than ten lines of simple code" seems to elicit a mood of "Show me something clever ".
You can show a new programmer something clever. You can then teach that same programmer how to replicate this "performance". But this is not what gets them hooked on programming. Teach them the rudiments, and let them synthesize their own clever ten lines of code.
I would show a new programmer the following Python code:
input = open("input.txt", "r") output = open("output.txt", "w") for line in input: edited_line = line edited_line = edited_line.replace("EDTA", "ethylenediaminetetraacetic acid") edited_line = edited_line.replace("ATP", "adenosine triphosphate") output.write(edited_line)
I realize that I don't need to assign line
to edited_line
. However, that's just to keep things clear, and to show that I'm not editing the original document.
In less than ten lines, I've verbosified a document. Of course, also be sure to show the new programmer all the string methods that are available. More importantly, I've showed three fundamentally interesting things I can do: variable assignment, a loop, file IO, and use of the standard library.
I think you'll agree that this code doesn't dazzle. In fact, it's a little boring. No – actually, it's very boring. But show that code to a new programmer and see if that programmer can't repurpose every part of that script to something much more interesting within the week, if not the day. Sure, it'll be distasteful to you (maybe using this script to make a simple HTML parser), but everything else just takes time and experience.
Like most of the other commenters, I started out writing code to solve math problems (or to create graphics for really terrible games that I would design — things like Indiana Jones versus Zombies).
What really started me (on both math and programming) was going from text based, choose your own adventure style games…to more graphics-based games. I started out coloring graph paper and plotting pixels, until I got into geometry…and discovered how to use equations to plot curves and lines, boxes, etc.
My point is, I could have really gotten into something like processing ( http://processing.org/ ) where a typical program looks something like this:
void setup() { size(200, 200); noStroke(); rectMode(CENTER); } void draw() { background(51); fill(255, 204); rect(mouseX, height/2, mouseY/2+10, mouseY/2+10); fill(255, 204); int inverseX = width-mouseX; int inverseY = height-mouseY; rect(inverseX, height/2, (inverseY/2)+10, (inverseY/2)+10); }
To me, this is the "Logo" of the future.
There are easy "hello world" examples that can quickly get someone drawing and changing code and seeing how things break and what weird "accidents" can be created…all the way to more advanced interaction and fractal creation…
You could use a script written with AutoIt , which blurs the line between using a traditional application and programming.
Eg a script which opens notepad and makes their own computer insult them in it and via a message box, and then leaves no trace of its actions:
Run("notepad.exe") WinWaitActive("Untitled - Notepad") Send("You smell of human.") Sleep(10000) MsgBox(0, "Humans smell bad", "Yuck!") WinClose("Untitled - Notepad") WinWaitActive("Notepad", "Do you want to save") Send("!n")
I remember when I first started coding loops always impressed me. You write 5 – 10 lines of code (or less) and hundreds (or however many you specify) lines print out. (I learned first in PHP and Java).
for( int i = 0; i < 200; i++ ) { System.out.println( i ); }
I think a good place for a student to get started could be Greasemonkey . There are thousands of example scripts on userscripts.org, very good reading material, some of which are very small. Greasemonkey scripts affect web-pages, which the students will already be familiar with using, if not manipulating. Greasemonkey itself offers a very easy way to edit and enable/disable scripts while testing.
As an example, here is the "Google Two Columns" script:
result2 = '<table width="100%" align="center" cellpadding="10" style="font-size:12px">'; gEntry = document.evaluate("//li[@class='g'] | //div[@class='g'] | //li[@class='g w0'] | //li[@class='gs w0']",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null); for (var i = 0; i < gEntry.snapshotLength; i++) { if (i==0) { var sDiv = gEntry.snapshotItem(i).parentNode.parentNode; } if(i%2 == 0) { result2 += '<tr><td width="50%" valign="top">'+gEntry.snapshotItem(i).innerHTML+'</td>'; } if(i%2 == 1) { result2 += '<td width="50%" valign="top">'+gEntry.snapshotItem(i).innerHTML+'</td></tr>'; } } sDiv.innerHTML = result2+'</table>'; if (document.getElementById('mbEnd') !== null) { document.getElementById('mbEnd').style.display = 'none'; }
This is a very rudimentary text-based c# program that simulates the spinning action of a slot machine. It doesn't include different odds of winning or cash payouts, but that could be a nice exercise for the students.
Sorry that it is more than 10 lines.
string[] symbols = new[] { "#", "?", "~" }; // The symbols on the reel Random rand = new Random(); do { string a="",b="",c=""; for( int i = 0; i < 20; i++ ) { Thread.Sleep( 50 + 25 * i ); // slow down more the longer the loop runs if( i < 10 ) a = symbols[rand.Next( 0, symbols.Length )]; if( i < 15 ) b = symbols[rand.Next( 0, symbols.Length )]; c = symbols[rand.Next( 0, symbols.Length )]; Console.Clear(); Console.WriteLine( "Spin: " + a + b + c ); } if( a == b && b == c ) Console.WriteLine( "You win. Press enter to play again or type \"exit\" to exit" ); else Console.WriteLine( "You lose. Press enter to play again or type \"exit\" to exit" ); } while( Console.ReadLine() != "exit" );
With Tcl you have a simple text editor with a save button in about 12 lines of code (but no open, that would take another 8 lines). It works across all standard platforms:
pack [frame .toolbar] -side top -fill x pack [button .save -text save -command save] -in .toolbar -side left pack [scrollbar .vsb -orient vertical -command [list .text yview]] -side right -fill y pack [text .text -wrap word -yscrollcommand [list .vsb set]] -side left -fill both -expand true proc save {} { set filename [tk_getSaveFile] if {$filename ne ""} { set f [open $filename w] puts $f [.text get 1.0 end-1c] close $f } }
I realize the goal was 10 lines, so if you want the to stick to 10 lines or less, a simple text editor without load or save is only two lines. That's not too shabby.
pack [scrollbar .vsb -orient vertical -command [list .text yview]] -side left -fill y pack [text .text -wrap word -yscrollcommand [list .vsb set]] -side left -fill both -expand true
Execute either of the above blocks of code with "wish filename " on the platform of your choice. Wish comes with most *nix's and the mac but you'll have to install it manually for windows.
To go a step further, that two line script can also be written in python, though it takes eight lines, still under the 10 line goal:
from Tkinter import * root=Tk() text = Text(wrap="word") sb = Scrollbar(orient="vertical", command=text.yview) text.configure(yscrollcommand=sb.set) sb.pack(side="right", fill="y") text.pack(side="left", fill="both", expand=True) root.mainloop()
How about a bookmarklet? It would show them how to manipulate something that they use every day (the Internet) without requiring any development tools.
If you can afford the hardware, using an Arduino board + processing will produce some pretty cool things, though it may be a little advanced for people that may not be interested at all in programming.
I wrote about this recently in an article "The Shortest, most useful program I have ever written."
Summary: I wrote a 3 line VB6 app back in 1996 that I still use every single day. Once the exe is dropped in the "Send-to" folder. It lets you right click on a file in explorer and send the full path of that file to the clipboard.
Public Sub Main() Clipboard.SetText Command$ End Sub
It's interesting that you mention the Mandelbrot set, as creating fractals with GW-BASIC is what sparked my love of programming back in high school (around 1993). Before we started learning about fractals, we wrote boring standard deviation applications and I still planned to go into journalism.
But once I saw that long, difficult-to-write BASIC program generate "fractal terrain," I was hooked and I never looked back. It changed the way I thought about math, science, computers, and the way I learn.
I hope you find the program that has the same affect on your students.
wxPython First Steps
import wx app = wx.App() wx.Frame(None, -1, 'simple.py').Show() app.MainLoop()
simple.py frame http://zetcode.com/wxpythonhttp://img.dovov.comsimple.jpg
I'm sure it'd turn into more than 10 lines of code, but have you considered a form based app where pressing the buttons does things like changing the colour of the background or changes the size of the text? This would show them how interactive programs work. It would also show them that they, as programmer, are in complete control of what the computer (program) does.
Hopefully it would lead them to make suggestions for other things they could change and then onto other things they might want to do.