472,784 Members | 998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Help: Quick way to test if value lies within a list of lists ofranges?

Scenario:
=========

Using PyGame in particular, I am trying to write an application that will
run a scripted timeline of events, eg at 5.5 seconds, play xxx.mp3 and put
the image of a ball on screen, at 7.8 seconds move the ball up and down.
At this point, I hear you say 'Oh, like Flash'.

Yes, well... Like Flash, but I don't want to take this app in the same
direction as Macromedia took Flash, nor do I (ever) want the two to be
compatible.

One particular need is for the timeline to be quickly traversable. If I
want to go to time=239.4 seconds, I'd like it to go there pretty much
painlessly and hopefully with one call. (Same with play, reverse and pause
really) I also want it to play for a long duration, with lots of different
items (images, audio, etc.)

Let me be a little more specific:

sprite(a) -> (onscreen during) 2 - 10 secs, 20 - 50 secs
sprite(b) -> (onscreen during) 15 - 30 secs
sprite(c) -> (onscreen during) 42 - 50 secs
.....

I need a quick way to rattle off a list of sprites that should be on
screen at a given time. Needless to say the number of sprites will be
variable, and might even change unpredictably in game.

E.G.

onscreen(time = 8.8 secs): return [sprite(a)]

onscreen(time = 44.134 secs): return [sprite(a), sprite(c)]

onscreen(time = 28 secs): return [sprite(a), sprite(b)]

(NB Anything from 10 -> 200 sprites would be normal intended usage, each
set -up with a list of start,stop times.)

Any suggestions on a clever way to do this? I don't like the idea of
looping through 100+ sprites and test them each an arbitary number of
times every update. Is this just something unavoidable which should be
written in C for speed and included with SWIG or something similar?

Ben

(Anti-Zealotry ward)
:0:
* ^Subject:.*Re: Microsoft Hatred FAQ
| gzip >> junk-archive.gz
IMO, We all have our personal experience or beliefs as to how MS operates..
Petitioning MEP's/Senators will accomplish more than arguing on a Python
mailing list.

Oct 27 '05 #1
2 1684
Would this help?
'''
pp(name2ranges) {'a': [[5, 12], [27, 89], [120, 137]],
'b': [[6, 23], [26, 84], [200, 222]],
'c': [[2, 22], [47, 60], [67, 122]]} names4val(28) ['a', 'b'] names4val(11) ['a', 'c', 'b'] names4val(145) []


'''
from pprint import pprint as pp

name2ranges = {
'a': [[48,60], [27,89], [5,12], [120,137]],
'b': [[78,84], [26,79], [200,222], [6,23], [72,74]],
'c': [[67,122],[2,22], [47,60]]
}

for name, ranges in name2ranges.iteritems():
# sort ranges
name2ranges[name].sort()
# Coalesce overlapping ranges.
# If the max of one range is >= the min of the next range then
# the adjacent ranges are combined
ln = len(ranges)
newranges = []
i = 0
while i< ln:
mn,mx = ranges[i]
j = i+1
while j< ln and ranges[j][0] <=mx:
mx = max(mx, ranges[j][1])
j += 1
newranges.append([mn,mx])
i = j
name2ranges[name] = newranges

def names4val(val):
" find the names whose ranges intersect the value"
from bisect import bisect_left
names = []
for name,ranges in name2ranges.iteritems():
bs = bisect_left(ranges, [val,val])
if (bs and ranges[bs-1][0] <= val <= ranges[bs-1][1] ) or (
bs <len(ranges) and ranges[bs][0] <= val <= ranges[bs][1] ):
names.append(name)
names.sort()
return names

####

Cheers, Pad.

Oct 27 '05 #2
"Ben O'Steen" <bo*****@maysubdivide.org> wrote in message
news:ma*************************************@pytho n.org...
Scenario:
=========

Using PyGame in particular, I am trying to write an application that will
run a scripted timeline of events, eg at 5.5 seconds, play xxx.mp3 and put
the image of a ball on screen, at 7.8 seconds move the ball up and down.
At this point, I hear you say 'Oh, like Flash'.

Yes, well... Like Flash, but I don't want to take this app in the same
direction as Macromedia took Flash, nor do I (ever) want the two to be
compatible.

One particular need is for the timeline to be quickly traversable. If I
want to go to time=239.4 seconds, I'd like it to go there pretty much
painlessly and hopefully with one call. (Same with play, reverse and pause
really) I also want it to play for a long duration, with lots of different
items (images, audio, etc.)

<snip>

Ben -

This sounds very similar to a discrete event simulator, which uses a
timeline to track when the "next" event is supposed to occur, and advances
by events, rather than by straight elapsing of time (so that a 3 week
simulation doesn't take 3 weeks to run!). SimPy does this very thing, and
it also has a "real-time" mode so that simulations can be watched as they
would really transpire.

The one difference is that with SimPy, you don't really script the timeline
ahead of time, but instead you script the individual events, and the
resources that the various processes have to compete/contend for, thereby
causing the events to get spread out over time.

But you might get some clues from SimPy's traversal of the event queue, and
the alternative model of building the queue dynamically while "executing" it
might give you some other ideas about your game design. (I do recall that
SimPy's event queue can eventually grow to hold thousands of events, and a
huge speedup was accomplished a ver versions ago by using the bisect module
to insert new events at the proper location in the queue. But this is only
relevant if you build the queue dynamically.)

-- Paul
Oct 28 '05 #3

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

Similar topics

4
by: Jacob H | last post by:
Hello list... I'm developing an adventure game in Python (which of course is lots of fun). One of the features is the ability to save games and restore the saves later. I'm using the pickle...
7
by: Christian Christmann | last post by:
Hi, in the past I always appreciated your help and hope that you also can help me this time. I've spent many many hours but still can't solve the problem by myself and you are my last hope. ...
1
by: prime80 | last post by:
I'm building a project database to keep track of the various engineering projects ongoing in our department. These projects will be grouped by priority (=High, Medium, Low) and ranked within the...
8
by: rh0dium | last post by:
Hi all, I am using python to drive another tool using pexpect. The values which I get back I would like to automatically put into a list if there is more than one return value. They provide me...
1
by: David Hirschfield | last post by:
I've written a tree-like data structure that stores arbitrary python objects. The objective was for the tree structure to allow any number of children per node, and any number of root nodes...and...
0
by: Brian Henry | last post by:
Ok I've never implemented a snap location before so I dont really know what im doing wrong here... anyways, I am making a custom slider control that takes dates as its values instead of integers......
2
by: Netkiller | last post by:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Project: Network News Transport Protocol Server Program Description: 基于数据库的新闻组,实现BBS前端使用NNTP协议来访问贴子...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.