473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Live Coding in pygame

Here is a more visual example of the technique presented in Logan
Koester's article, "Live Coding in Python"
(http://www.logankoester.com/mt/2006/...python_1.html).

It's very quick-and-dirty in style, partly because half of the program
was written in a moment of inspiration, and the other half was written
and rewritten in real-time... :-D

I've only tested this on Linux, using python 2.4.3. Reports of working
or non-working on other platforms are very welcome. You will of course
need pygame installed. :-)

Paste the following snippets into live.py and livetest.py respectively.
Run python live.py. Edit livetest.py while the program is running,
and save livetest.py to see your results unfold.

Other fun misuses include:
(1) running multiple instances at once (slow!) and watching your
changes affect them all;
(2) nested live coding [live.py reloads live-prime.py, which in turn
reloads livetest.py on each iteration]; and
(3) having livetest.run() return data, and making live.py cache
non-null data received... and pass it back as an argument to
livetest.run(). You could then have reachable, usable data (say, a
bitmap picture) floating around in your program's running instance, yet
no longer mentioned in the program's source code. Ephemeral
programming!

Enjoy. :-)

Julian

################ live.py ####################
import pygame
from pygame.locals import *
from pygame import display

try:
import livetest
except ImportError:
livetest = None

pygame.init()

screen = display.set_mode((800, 600))
step = 0

while 1:
try:
reload(livetest)
except:
print "Failed to reload livetest.py"
livetest.run(screen, step)
step += 1
if step 2400: # just a nice, round number ;-)
step = 0
################ livetest.py ################
import pygame
from pygame.locals import *
from pygame import display

def run(screen, step):
background = 0x00, 0x00, 0x00
textcolor = 0x00, 0xcc, 0xff
text = "Live coding rocks!"
myfont = pygame.font.SysFont("Arial" , 50)
textimg = myfont.render(text, 1, textcolor)

x = step % 800
y = step % 600

screen.fill(background)
screen.blit(textimg, (x, y))
display.flip()

#########################################

Aug 26 '06 #1
0 1741

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Marian Aldenhövel | last post by:
Hi, I am trying to make pygame play music on windows. This simple program: import pygame,time pygame.init() print "Mixer settings", pygame.mixer.get_init() print "Mixer channels",...
3
by: Tim Knauf | last post by:
Hi everyone, I'm glad to have found this list. I've written a small script for my own use which, amongst other things, captures mouse click information from a window containing an image. I used...
2
by: Brent W. Hughes | last post by:
I'm just starting to learn pygame. I write what I think is just about the simplest program that should display a window and then quit. #----------------------------------------------- import sys...
1
by: kjm | last post by:
Hi everyone, I have recently acquired a Logitech Rumble pad to use as an input device. I have been having trouble getting the event que to respond that a button or hat arrow has been pressed. ...
0
by: Lunpa | last post by:
My project: I'm working on a game, where in the ui, it takes the pygame window, and shoves it into a gtk2 socket widget. (gtk2 widgets are generated with glade, with the exception of the socket...
1
by: liuliuliu | last post by:
hi -- sorry if this is trivial -- but how do you make a screenshot of a pygame display? i have a surface which is basically the entire visible screen -- how do you write this surface as an image...
11
by: dynamo | last post by:
Hi guys i have come again with more problems.This time it has to do with pygame.The following code does not give any error messages but it does not do what it is supposed to do either.the code is a...
3
by: globalrev | last post by:
im doing this : http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/ and when closing the program the window stays up and doesnt respond. i tried adding...
11
by: globalrev | last post by:
http://www.pygame.org/docs/ref/mixer.html import pygame #pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=3072) //it complained abiout words= so i guess its only the nbrs...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.