Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 502 503 [504] 505 506 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 886095 times)

highmax28

  • Bay Watcher
  • I think this is what they call a tantrum spiral...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7545 on: June 12, 2015, 10:33:39 pm »

Ok, so I posted in my last shot what I did with my car assignment. Now my teacher wants me to upgrade it with if statements and loops. I want to get fancy and for every car that finishes gonig across the screen, I want to use a Math.random function to spew out a random car that's a different color out.

However, I have no idea how to swap to different images through javascript, but I'm PRETTY sure its doable without causing a 10 car pileup. It currently spews one image of a car and I want it to randomly pick the image, but the way how its set, it requires an image to be created.

My question is: how do I go about doing this, because I have NO idea where to start or how to word it properly. This is my code with each car already placed inside the CSS file. I commented out the other cars because they randomly popped up on my screen

Code: [Select]
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>Car Object Animation with Callback</title>
    <style type="text/css">
        body {
            background-color: #AADDCC;
background: url("background.png");
background-size: 1600px 770px;
background-repeat: no-repeat;
padding-top: 40px;
        }
        #lCar1 {
        position:absolute;
        right:253px;
        bottom:243px;
        z-index:-1;
        }
#lCar2 {
        position:absolute;
        right:253px;
        bottom:243px;
        z-index:-1;
        }
#lCar3 {
        position:absolute;
        right:253px;
        bottom:243px;
        z-index:-1;
        }
#lCar4 {
        position:absolute;
        right:253px;
        bottom:243px;
        z-index:-1;
        }
#lCar5 {
        position:absolute;
        right:253px;
        bottom:243px;
        z-index:-1;
        }
#lCar6 {
        position:absolute;
        right:253px;
        bottom:243px;
        z-index:-1;
        }
#rCar1 {
        position:absolute;
        left: 253px;
        left:253px;
        bottom:122px;
        z-index:-1;
}
#rCar2 {
        position:absolute;
        left: 253px;
        left:253px;
        bottom:122px;
        z-index:-1;
        }
#rCar3 {
        position:absolute;
        left: 253px;
        left:253px;
        bottom:122px;
        z-index:-1;
}
#rCar4 {
        position:absolute;
        left: 253px;
        left:253px;
        bottom:122px;
        z-index:-1;
        }
#rCar5 {
        position:absolute;
        left: 253px;
        left:253px;
        bottom:122px;
        z-index:-1;
        }
#rCar6 {
        position:absolute;
        left: 253px;
        left:253px;
        bottom:122px;
        z-index:-1;
        }
#clouds {
        position:absolute;
        right: 55px;
        top:135px;
        z-index:99;
}

#clouds2 {
        position:absolute;
        right: 55px;
        top: 105px;
        z-index:99;
}
#clouds3 {
        position:absolute;
        right: 55px;
        top:155px;
        z-index:99;
}
#clouds4 {
        position:absolute;
        right: 255px;
        top:55px;
        z-index:99;
}


    </style>


</head>
<body style="overflow: hidden">
    <audio autoplay loop>
        <source src="026- Earthbound - Buy Somethin_ Will Ya!.mp3" type="audio/mpeg">
    </audio>
    <audio id="honk">
        <source src="Car honk.mp3" type="audio/mpeg">
    </audio>
    <img alt="cloud" id="clouds" src="clouds.png" />
<img alt="cloud" id="clouds2" src="clouds.png" />
<img alt="cloud" id="clouds3" src="clouds.png" />
<img alt="cloud" id="clouds4" src="clouds.png" />
<!--
    <img alt="lcar1" id="lCar1" src="leftCar1.png" />
<img alt="lcar2" id="lCar2" src="leftCar2.png" />
<img alt="lcar3" id="lCar3" src="leftCar3.png" />
<img alt="lcar4" id="lCar4" src="leftCar4.png" />
<img alt="lcar5" id="lCar5" src="leftCar5.png" />
-->
<img alt="lcar6" id="lCar6" src="leftCar6.png" />
<!--
<img alt="rcar1" id="rCar1" src="rightCar1.png" />
<img alt="rcar2" id="rCar2" src="rightCar2.png" />
<img alt="rcar3" id="rCar3" src="rightCar3.png" />
-->
<img alt="rcar4" id="rCar4" src="rightCar4.png" />
<!--
<img alt="rcar5" id="rCar5" src="rightCar5.png" />
<img alt="rcar6" id="rCar6" src="rightCar6.png" />
-->
            <script>
function playAudio() {
honk.play();
}
                // create  Car factory using the constructor function
                function CarsLeft(id) {
                    this.speed = Math.floor((Math.random() * 100) + 1); // generate a random number between 1 and 100 //prompt("Enter a number between one and 100", 75);
                    this.speed = 101 - this.speed;// inverse the speed to be used in the built in setTimeOut method
                    this.car = document.getElementById(id);//grab onto the image
                    this.carCounter = -254;// set the car counter which will control the pixel location
                    this.lapCounter = 0; // used to count the laps

                    // The Call back function is to count laps and to learn about Call back functions
                    this.callbackFun = function (car) {
                        alert(" Finished lap number " + car.lapCounter + " for car " + id);
                    }

                    // the moviit function changes the css right property and accepts a function
                    this.moveIt = function (callback) {
                       
                        this.carCounter += 3;
                        this.car.style.right = this.carCounter + "px";

                       

                        var _this = this;// Becase this is the this we want as it changes once inside the setTimeOut function

                        if (this.carCounter < (screen.width)) {
                            setTimeout(function () { _this.moveIt(callback) }, this.speed);// send moveIt the callback function as an argument


                        } else {
                            this.lapCounter++;// increment the lap Counter

                            //callback(this); // Call the call back function which gives alert for each lap

//Play the car honking when it reaches the end of the screen
playAudio();

                            //alert(" Lap number " + this.lapCounter);// visual display of the lab counter
                            this.carCounter = -254;// reset the car Counter back to -400
                            setTimeout(function () { _this.moveIt(callback) }, this.speed);
                        }

                    }// end of move it function


     
                };

                // create  Car factory using the constructor function
                function CarsRight(id) {
                    this.speed = Math.floor((Math.random() * 100) + 1); // generate a random number between 1 and 100 //prompt("Enter a number between one and 100", 75);
                    this.speed = 101 - this.speed;// inverse the speed to be used in the built in setTimeOut method
                    this.car = document.getElementById(id);//grab onto the image
                    this.carCounter = -254;// set the car counter which will control the pixel location
                    this.lapCounter = 0; // used to count the laps

                    // The Call back function is to count laps and to learn about Call back functions
                    this.callbackFun = function (car) {
                        alert(" Finished lap number " + car.lapCounter + " for car " + id);
                    }

                    // the moviit function changes the css right property and accepts a function
                    this.moveIt = function (callback) {
                       
                        this.carCounter += 3;
                        this.car.style.left = this.carCounter + "px";

                       

                        var _this = this;// Becase this is the this we want as it changes once inside the setTimeOut function

                        if (this.carCounter < (screen.width)) {
                            setTimeout(function () { _this.moveIt(callback) }, this.speed);// send moveIt the callback function as an argument


                        } else {
                            this.lapCounter++;// increment the lap Counter

                            //callback(this); // Call the call back function which gives alert for each lap

//Play the car honking when it reaches the end of the screen
playAudio();

                            //alert(" Lap number " + this.lapCounter);// visual display of the lab counter
                            this.carCounter = -254;// reset the car Counter back to -400
                            setTimeout(function () { _this.moveIt(callback) }, this.speed);
                        }

                    }// end of move it function


     
                };




function Clouds(id) {
                    this.speed = Math.floor((Math.random() * 75) + 1); // generate a random number between 1 and 100 //prompt("Enter a number between one and 100", 75);
                    this.speed = 101 - this.speed;// inverse the speed to be used in the built in setTimeOut method
                    this.car = document.getElementById(id);//grab onto the image
                    this.carCounter = -100;// set the car counter which will control the pixel location
                    this.lapCounter = 0; // used to count the laps

                    // The Call back function is to count laps and to learn about Call back functions
                    this.callbackFun = function (car) {
                        alert(" FInished lap number " + car.lapCounter + " for car " + id);
                    }

                    // the moviit function changes the css right property and accepts a function
                    this.moveIt = function (callback) {
                       
                        this.carCounter += 3;
                        this.car.style.right = this.carCounter + "px";

                       

                        var _this = this;// Becase this is the this we want as it changes once inside the setTimeOut function

                        if (this.carCounter < (screen.width+100)) {
                            setTimeout(function () { _this.moveIt(callback) }, this.speed);// send moveIt the callback function as an argument


                        } else {
                            this.lapCounter++;// increment the lap Counter

                            //callback(this); // Call the call back function which gives alert for each lap


                            //alert(" Lap number " + this.lapCounter);// visual display of the lab counter
                            this.carCounter = -100;// reset the car Counter back to -400
                            setTimeout(function () { _this.moveIt(callback) }, this.speed);
                        }

                    }// end of move it function


     
                };


                var myCar1 = new CarsLeft('lCar6');
                myCar1.moveIt(myCar1.callbackFun);

var myCar2 = new CarsRight('rCar4');
                myCar2.moveIt(myCar2.callbackFun);

var cloud1 = new Clouds('clouds');
                cloud1.moveIt(cloud1.callbackFun);

var cloud2 = new Clouds('clouds2');
                cloud2.moveIt(cloud2.callbackFun);

var cloud3 = new Clouds('clouds3');
                cloud3.moveIt(cloud3.callbackFun);

var cloud4 = new Clouds('clouds4');
                cloud4.moveIt(cloud4.callbackFun);

   
    </script>

</body>
</html>
Logged
just shot him with a balistic arrow, i think he will get stuned from that >.>

"Guardian" and Sigfriend Of Necrothreat
Jee wilikers, I think Highmax is near invulnerable, must have been dunked in the river styx like achilles was.
Just make sure he wears a boot.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7546 on: June 16, 2015, 07:28:36 am »

Can't you just set the src on the car in question and change that?
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

EnigmaticHat

  • Bay Watcher
  • I vibrate, I die, I vibrate again
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7547 on: June 16, 2015, 03:15:13 pm »

-FOV stuff-
You should consider doing it the crawl way: only check line of sight for one team, give all units the same sight range.
Logged
"T-take this non-euclidean geometry, h-humanity-baka. I m-made it, but not because I l-li-l-like you or anything! I just felt s-sorry for you, b-baka."
You misspelled seance.  Are possessing Draignean?  Are you actually a ghost in the shell? You have to tell us if you are, that's the rule

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7548 on: June 16, 2015, 05:21:24 pm »

So, Project Euler Problem 7. Worked out a program that gets the right answer. I'm sure it's not as efficient as it could be. Like, at all. Anyone got anything to say on how they'd handle it? I've tried thinking of some formula or something but it seems the only solution I can think of involves loops just stepping through the process.

Spoiler (click to show/hide)
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7549 on: June 16, 2015, 08:12:11 pm »

So, Project Euler Problem 7. Worked out a program that gets the right answer. I'm sure it's not as efficient as it could be. Like, at all. Anyone got anything to say on how they'd handle it? I've tried thinking of some formula or something but it seems the only solution I can think of involves loops just stepping through the process.

Spoiler (click to show/hide)
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes / https://en.wikipedia.org/wiki/Sieve_of_Sundaram / https://en.wikipedia.org/wiki/Sieve_of_Atkin
or
https://en.wikipedia.org/wiki/Prime_number_theorem#Approximations_for_the_nth_prime_number
The Nth prime number should be between N(log(N)+log(log(N)))-N and N(log(N)+log(log(N))) for N≥6. You might want to be sure round up on the larger term instead of truncating to be safe.
"log()" is the function for natural log in <math.h>
« Last Edit: June 16, 2015, 08:58:28 pm by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

TheDarkStar

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7550 on: June 16, 2015, 08:53:59 pm »

Also, you can significantly speed up your algorithm by going changing the conditions of the for loop in the primeCalc function to
Code: [Select]
for(int i=1;(i*i)<=num;i++)
This makes the prime-checking take the square root of the number of checks as it would otherwise. You could also implement it with the sqrt() function from <math.h>, but that isn't as efficient.
Logged
Don't die; it's bad for your health!

it happened it happened it happen im so hyped to actually get attacked now

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #7551 on: July 01, 2015, 12:48:08 am »

So I wanted to get some opinions on the best way to handle this.

I'm trying to think of a non-messy way to handle switching between GUI layouts. Basically, each layout consists of a number of window objects drawn inside the actual app window. When I want to switch to a different layout, I remove all current windows from the screen and add the ones that are part of the new layout.

I was going to create a plain-old-data "Layout" class (or maybe even just typedef a vector of window objects), but I also need a function for each layout which moves around and resizes its windows whenever the app window size is changed. The only clean way I can think to do this is to make a base "UILayout" class, and then each time I want a new layout I just create a new child class which inherits init() and resize() functions. Then whenever my window resizes, all I have to do is call the currently selected layout's resize() function.

But is this a horrible idea? The "Layout" objects will really only need one instance for the duration of the program, so creating an entire new class in a header file every time I make a new one seems extremely wasteful. But the obvious alternative, defining free functions instead of making classes, sounds even messier. Using a giant if-else chain to check which resize() function I should call is not something I want to do.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7552 on: July 01, 2015, 01:24:08 am »

Use a class per layout. It keeps your code organized and you'll eventually want to add more methods to layouts than just "init" and "resize".
Logged

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #7553 on: July 01, 2015, 01:30:01 am »

I'd just make two separate functions like so:
Code: [Select]
def draw_ui_1():
    put_stuff_here()

def draw_ui_2():
    put_stuff_there()

#in your main loop
if uiMode == 1:
    draw_ui_1()
else:
    draw_ui_2()
All you Real Programmers™ probably want to stab me now.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7554 on: July 01, 2015, 02:51:11 am »

Quote
Using a giant if-else chain to check which resize() function I should call is not something I want to do.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #7555 on: July 01, 2015, 02:55:21 am »

Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7556 on: July 01, 2015, 03:16:00 am »

The way layout things like that are usually handled is by anchors. You chose weather the window is anchored to the nearest edge of the screen or to a percentage or whatever.

If you're rolling your own GUI, I would recommend trying out a few premade UI systems and see how they do it. Unity's is pretty good.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7557 on: July 01, 2015, 03:28:26 am »

You probably don't need a whole new class for every windows layout. You need a single class and each one has a set of data to tell it which windows to create and where to create them. Then the resize function just cycles through the array and places all the needed windows.

Spawn an array of these objects when the app starts, there's your layouts. This way of thinking will also make dynamically adding layouts possible (data polymorphism), whereas code-level polymorphism will only allow you to modify layouts by recompiling code.

EDIT: if you need polymorphism, make that at the level of different window types that are in a polymorphic container of some type inside each layout. Then, the layouts can spawn different types of objects without having to know how they work internally.
« Last Edit: July 01, 2015, 06:56:30 am by Reelya »
Logged

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #7558 on: July 01, 2015, 10:34:33 am »

The way layout things like that are usually handled is by anchors. You chose weather the window is anchored to the nearest edge of the screen or to a percentage or whatever.

If you're rolling your own GUI, I would recommend trying out a few premade UI systems and see how they do it. Unity's is pretty good.
You probably don't need a whole new class for every windows layout. You need a single class and each one has a set of data to tell it which windows to create and where to create them. Then the resize function just cycles through the array and places all the needed windows.

Spawn an array of these objects when the app starts, there's your layouts. This way of thinking will also make dynamically adding layouts possible (data polymorphism), whereas code-level polymorphism will only allow you to modify layouts by recompiling code.

I gave up on GUI programming a while back. I'm using a pre-made one.

Which is why I'm having trouble here. This window class is already meant to work a certain way, and it has (almost) every feature I need. It scales and moves around elements inside windows, I just need a way to move the windows themselves when I resize the program window. And yeah, it would be a much better idea to have pseudo-polymorphic behavior at the data level instead of using inherited classes, especially since this header has to be visible to more or less the whole program. I could always create an "Alignment" class and wrap up the Window class with it, then when I resize I just move the windows based on the alignment data they're paired with. I think I'll try that.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7559 on: July 02, 2015, 03:06:34 pm »

If I have a function that returns two ints, how would I define two variables in the main that are meant to store the two ints returned by the function?

Spoiler: example (click to show/hide)
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.
Pages: 1 ... 502 503 [504] 505 506 ... 796