Author Topic: My physics game  (Read 23026 times)

0 Members and 2 Guests are viewing this topic.

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #15 on: February 01, 2011, 06:28:25 pm »
I am intermediate At python i mainly have used it for pretty simple games. This is my first attempt at a more difficult game. I almost have the basics of the game working right now I need a sprite though and I'm trying to decide what to use.
and it wasn't that I didn't know how to do it it was that i wanted to try and find an optimized way of doing it without looping through every single pixel.
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: My physics game
« Reply #16 on: February 01, 2011, 06:49:07 pm »
When I say loop through every pixel, I only mean every pixel that is part of your element. So if you were doing a water simulation, you would loop through every pixel of water

Offline alberthrocks

  • Moderator
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 876
  • Rating: +103/-10
    • View Profile
Re: My physics game
« Reply #17 on: February 01, 2011, 07:01:07 pm »
I am intermediate At python i mainly have used it for pretty simple games. This is my first attempt at a more difficult game. I almost have the basics of the game working right now I need a sprite though and I'm trying to decide what to use.
and it wasn't that I didn't know how to do it it was that i wanted to try and find an optimized way of doing it without looping through every single pixel.
Hmm... good point. I'm not too sure how to search through an array without looping through it - a bit of .index(n) use may do it, but so far my tests show it only returns one index, not an array. :(
Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/


Proud member of ClrHome!

Miss my old signature? Here it is!
Spoiler For Signature:
Alternate "New" IRC post notification bot (Newy) down? Go here to reset it! http://withg.org/albert/cpuhero/

Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/

Activity remains limited due to busyness from school et al. Sorry! :( Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. :)

Don't expect me to be online 24/7 until summer. Contact me via FB if you feel it's urgent.


Proud member of ClrHome!

Spoiler For "My Projects! :D":
Projects:

Computer/Web/IRC Projects:
C______c: 0% done (Doing planning and trying to not forget it :P)
A_____m: 40% done (Need to develop a sophisticated process queue, and a pretty web GUI)
AtomBot v3.0: 0% done (Planning stage, may do a litmus test of developer wants in the future)
IdeaFrenzy: 0% done (Planning and trying to not forget it :P)
wxWabbitemu: 40% done (NEED MOAR FEATURES :P)

Calculator Projects:
M__ C_____ (an A____ _____ clone): 0% done (Need to figure out physics and Axe)
C2I: 0% done (planning, checking the demand for it, and dreaming :P)

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #18 on: February 01, 2011, 07:30:29 pm »
Here is my code so far All I have it do is move the sprite around with keyboard inputs. I know it is a little slow right now but that makes it easier for me. Next on my to do list is add gravity. I would appreciate any ideas for optimization or improvements.
Code: [Select]
#import modules
import pygame, math, random, os
from pygame.locals import *

#Check to see if pygame is working right
if not pygame.font:
    print 'Warning, fonts disabled'
if not pygame.mixer:
    print 'Warning, sound disabled'

#define any needed global variables here

    
#Create a function to load images for sprites.
def load_image(name, colorkey=None):
    fullname = os.path.join('data', name)
    try:
        image = pygame.image.load(fullname)
    #If an error exit
    except pygame.error, message:
        print 'Cannot load image:', fullname
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

#create a function to load sound
def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer or not pygame.mixer.get_init():
        return NoneSound()
    fullname = os.path.join('data', name)
    try:
        sound = pygame.mixer.Sound(fullname)
    #if an error exit
    except pygame.error, message:
        print 'Cannot load sound:', fullname
        raise SystemExit, message
    return sound

#classes for our game objects
class Player(pygame.sprite.Sprite):
    #moves a clenched fist on the screen, following the mouse
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image, self.rect = load_image('sprite.bmp', -1)
        self.direction=0

    def update(self):
        #move the player based on direction chosen
        if self.direction == 0:
            return
        elif self.direction == 1:
            newpos = self.rect.move((0, -2))
            self.rect = newpos
        elif self.direction == 2:
            newpos = self.rect.move((0, 2))
            self.rect = newpos
        elif self.direction == 3:
            newpos = self.rect.move((-2, 0))
            self.rect = newpos
        elif self.direction == 4:
            newpos = self.rect.move((2, 0))
            self.rect = newpos

    def up(self):
        self.direction=1

    def down(self):
        self.direction=2

    def left(self):
        self.direction=3

    def right(self):
        self.direction=4

    def still(self):
        self.direction=0

#function to load the game engine
def game_engine():
    #nothing done here yet
    return

#function for playing the game
def play_game():
    #load the game engine
    game_engine()
    #start the game
    #Initialize Everything
    pygame.init()
    screen = pygame.display.set_mode((975, 325))
    pygame.display.set_caption('Physics!')
    pygame.mouse.set_visible(1)
    turn = 0
    win = False
    lose = False
    achievements = 0

    #Create The Backgound
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250, 250, 250))

    #Display The Background
    screen.blit(background, (0, 0))
    pygame.display.flip()

    #Prepare Game Objects
    clock = pygame.time.Clock()
    player = Player()
    allsprites = pygame.sprite.RenderPlain((player))
    #Main Loop
    while 1:
        clock.tick(25)

    #Handle Input Events
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                return
            if event.type == KEYDOWN and event.key == K_UP:
                player.up()
            elif event.type == KEYDOWN and event.key == K_DOWN:
                player.down()
            elif event.type == KEYDOWN and event.key == K_LEFT:
                player.left()
            elif event.type == KEYDOWN and event.key == K_RIGHT:
                player.right()
            else:
                player.still()
                
                

        allsprites.update()

    #Draw Everything
        screen.blit(background, (0, 0))
        allsprites.draw(screen)
        pygame.display.flip()

def main():
    #define needed local variables here
    
    #start game
    play_game()
    
if __name__ == '__main__':
    main()

EDIT: @alberthrocks that is why i intend to at least at 1 point in the game have nearly the whole screen consumed by a mixture of fire and water and That would considerably slow down the game to have to loop through all of them.
« Last Edit: February 01, 2011, 07:32:39 pm by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: My physics game
« Reply #19 on: February 01, 2011, 07:34:39 pm »
It moves a sprite around?  I thought you were trying to add cellular automata?

Ashbad

  • Guest
Re: My physics game
« Reply #20 on: February 01, 2011, 07:38:28 pm »
maybe it's the game of life, except you can also move around in a 3D environment and shoot the pixels O.o

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #21 on: February 01, 2011, 07:49:19 pm »
I still don't quite get how to do cellular automata efficiently yet so I haven't put it in. I'm slowly adding in game parts and physics. I need collision detection and an efficient way of doing cellular automata. I'll have gravity set up soon. It might be a slightly different way from what you had on the physics lessons.
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: My physics game
« Reply #22 on: February 01, 2011, 07:53:15 pm »
You are going to do cellular automata as well as sprites with gravity and collision?

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #23 on: February 01, 2011, 07:55:01 pm »
Yes if I can get it all working correctly I will implement all of those.
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: My physics game
« Reply #24 on: February 01, 2011, 07:56:32 pm »
Hmmm you do know that the cellular automata will have different gravity than the sprites right?  Unless you are making the sprites have abnormal gravity so that they can match the water?  What kind of game are you actually making?

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #25 on: February 01, 2011, 08:01:45 pm »
I was going to make a jumping style where you jump up and avoid objects like fire, water, and possibly sand. I might develop farther on this design eventually for now that is it. The game will not be the same every time and will have a randomness to the events. The main part i want to emphasize in this game is the physics.

Why can't I have cellular automata use the same gravity?
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: My physics game
« Reply #26 on: February 01, 2011, 08:03:07 pm »
because with the rules of cellular automata, it would make the sand/water fall at 1 pixel per frame, where as true gravity would be parabolic.

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: My physics game
« Reply #27 on: February 01, 2011, 08:06:53 pm »
Not necessarily, Builderboy. You could add a third variable to the array that controls velocity and change it accordingly. It'd still be a cellular automaton.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #28 on: February 01, 2011, 08:07:28 pm »
Oh I was planning on trying to set up a rule that would make gravity work right.

How would I be able to test all the pixels in a small area if they were of different types. I'm planning on having many different kind of things in this so I don't want it going through platforms and stuff like that.
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: My physics game
« Reply #29 on: February 01, 2011, 08:24:19 pm »
Oh well in that case everything should work alright, although it sounds like its getting complicated.  As for the collision, you would just have to have a whole bunch of rules unfortunately :[