473,606 Members | 3,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1716
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.ite ritems():
# 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.appen d([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.ite ritems():
bs = bisect_left(ran ges, [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(na me)
names.sort()
return names

####

Cheers, Pad.

Oct 27 '05 #2
"Ben O'Steen" <bo*****@maysub divide.org> wrote in message
news:ma******** *************** **************@ python.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
5444
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 module to implement this. Capturing current program state and neatly replacing it later is proving to be trickier than I first imagined, so I'm here to ask for a little direction from wiser minds than mine! When my program initializes, each game...
7
1799
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. I've a program which is using self-written double-linked lists as a data structure. The template list consists of list elements and the list itself linking the list elements.
1
2047
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 priority (=1,2,3,etc.). What I'd like for it to do is automatically update rankings when a project rank is changed, a new project comes in, or a project is completed. For example, let's say there are 4 high priority projects. If High #2 is...
8
1677
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 a way to see that the data is in set by parenthesising it. This is all generated as I said using pexpect - Here is how I use it.. child = pexpect.spawn( _buildCadenceExe(), timeout=timeout) child.sendline("somefunction()")
1
2735
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 for it to be speedy for trees with thousands of nodes. At its core, the structure is just a list of lists arranged so that if node A has children B and C and node B has child D the data looks like: A = B =
0
1902
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... then taking that date range and finding dates specifiec between them (in a list of dates) and putting snap marks, so if you slide it near one of them it should snap to that tick, but that part i cant figure out. the rest seems ok so far... here...
2
2422
by: Netkiller | last post by:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Project: Network News Transport Protocol Server Program Description: 基于数据库的新闻组,实现BBS前端使用NNTP协议来访问贴子 Reference: NNTP协议: http://www.mibsoftware.com/userkt/0099.htm 正则表达式: http://wiki.woodpecker.org.cn/moin/RegExpInPython#head-2358765384844ed72f01658cbcde24613d941e9d
0
5556
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
2
10017
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 is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized at the time of its creation or later 2. The.types feature facilitates the definition of classes...
0
8036
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8448
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8126
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3948
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1572
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1313
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.