Re: Python crash course

A word on Dependency Scavenger Hunts: I don't even remember what dependencies different packages require, only that some require more than others. They are usually either in the same place where you found the package in the first place, or can be found at pypi.python.org or by googling the package name.
(In this post, I'll put code in blockquotes, so it's easier to jump in/out of.)

Oh, and if you want to run a program from an external .py file (notepad *.py is how I create them), import it, like so:
import my_module



Here's an excerpt from my attempt at giving Pygame easier 2D audio (will explain afterward):

import math

def convert(pan=0.0, volume=0.0):
 v = ((volume+50.0)/50.0)
 if v<0.0 :
  v=0.0
 elif v>1.0 :
  v=1.0
 pan/=10.0 # or 100.0, I forget which BGT uses.
 if pan<-1.0 :
  pan=-1.0
 elif pan>1.0 :
  pan=1.0
 
 vl = v if pan<=0.0 else v*(1.0-pan)
 vr = v if pan>=0.0 else v*(1.0+pan)
 return (vl, vr)
Explanation:

To play sound using pygame, one must setup the mixer, get a sound pointing to the correct file, then invoke its play method.
To adjust the volume and balance, however, we work with the channel that ultimately plays the sound.
(It's something of a mess. Hence, everyone trying to come up with something better.)

For example, this should play a sound in the right speaker:

import pygame
from os.path import normpath as np

pygame.mixer.init()
my_sound=pygame.mixer.Sound(np("c:\\windows\\media\\ding.wav"))
my_channel=my_sound.play()
my_channel.set_volume(0.0, 1.0)

Meh, not impressive enough.

Box2D

So, here's a Box2D example. To run it, you'll need my port of Philip_bennefall's sound_pool: https://www.sendspace.com/file/h0m7ty

Just to note, it is kinda monstrous to just read without having looked into Box2D enough to understand what half the stuff does. I basically use this tutorial as my primary reference:
http://www.iforce2d.net/b2dtut/
The point of this example is to have a ball drop onto a box, then bounce it.

from Box2D import *
import pygame
import os.path
from sound_pool import *

pygame.mixer.pre_init()
pygame.mixer.init()

world=b2World(b2Vec2(0, -10), True)
true=True
false=False
none=None
null=None

statdef=b2BodyDef()
statdef.type=b2_staticBody
statdef.position.Set(10, 0)
statbod=world.CreateBody(statdef)
box=b2PolygonShape()
box.SetAsBox(100, 1)
fixdef=b2FixtureDef()
fixdef.shape=box
fixdef.density=1
fix=statbod.CreateFixture(fixdef)

balldef=b2BodyDef()
balldef.position.Set(10, 10)
balldef.type=b2_dynamicBody
ball=world.CreateBody(balldef)
circle=b2CircleShape()
circle.m_radius=1
circdef=b2FixtureDef()
circdef.shape=circle
circdef.density=1
circfix=ball.CreateFixture(circdef)
circfix.restitution=0.25
circfix.friction=0.5

snd=os.path.normpath("c:\\users\\cae\\aviewer\\sounds\\red.wav")
pool=sound_pool()
slot=pool.play_2d(snd, 0, 0, ball.position.x, ball.position.y, True)

frame=0

class CL(b2ContactListener) :
    def BeginContact(this, contact) :
        
        print "Begin Contact..." + str(frame)
        pool.play_stationary("ball.wav", False)
        
    
    def EndContact(this, contact) :
        print "End contact. " + str(frame)
        
    

cl=CL()
world.contactListener=cl

clock=pygame.time.Clock()

while (frame<1000) :
    world.Step(clock.tick(50)*0.001, 8, 3)
    frame+=1
    pose=ball.position
    pool.update_sound_2d(slot, pose.x, pose.y)
    if(frame==500) :
        #help(ball.ApplyLinearImpulse)
        ball.ApplyLinearImpulse(b2Vec2(-30, 50), ball.position, True) # I have no idea what the last value does.
        print "SERVE!\n"
        pool.play_stationary("ball.wav", False)
    # Force.
    

If you run the above, you should notice... ur, nothing, since I didn't include the sound files. But let's assume you have the two files it needs, then run it.
You should hear a looping sound get louder, until it bounces a bit--at which point the bounce sound should play.
Then it stops for a couple seconds, then there is another bounce, and it goes flying into oblivion.
It really shouldn't go flying into oblivion. I goofed up somewhere.
The solution to the oblivion problem is to use Edges. I seem to remember some confusion as to how to use those with my copy of PyBox2D, so I went with the Rectangle, for the basic test.

I could explain that code monstrosity, but I should have started packing by now. So... HTH?

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : momo7807 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dhruv via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dhruv via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : frastlin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dhruv via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector

Reply via email to