Posted: Thu Oct 06, 2011 3:08 pm Post subject: Assistance with some Game Elements
So, I'm getting a good head-start on my Final Project already, and am going to start coding the individual elements, then put it all together later on.
For any concepts or elements that I don't necessarily know how to do from scratch, I will ask here, and hope you can give me a tip or direction to go to accomplish that. Some pseudocode would be helpful for some, but for others, I would just need a good shove in the right direction.
As for the moment, how would I go about doing these things:
1. Putting a delay on doing something, like if you had a gun, you are only able to shoot once every X seconds/milliseconds, without interrupting the rest of the program (Time.Delay would delay whole program wouldn't it?)
2. Waves of enemies (regarding number of enemies) / Levels
3. If you were to draw an object at a certain angle depending on the mouse coordinates (character holding a gun, aiming where mouse is), how would you apply a restriction to where it aims (like, the gun only aims where your mouse is, if the angle at which that gun is pointing is within an X degree angle)
4. In a mouse-oriented game, are you able to have an image be the mouse cursor when in the game? I know you can just draw the picture to where the mouse coordinates are, but is there a way to make the regular image cursor be replace by the image
If I get any more questions, I'll add them later. Until then, I'll be working on the structure and other elements of the game. This is so, by the time I'm at the point in trying to figure these things out, I've already received feedback on how I can successfully integrate it.
Plus, maybe if It's good enough, I'll submit it, and if people like it enough, I'll most likely port it to a mobile (more like, overhaul it and make it a lot better, more features, etc.)
Thanks in Advance...
Sponsor Sponsor
DemonWasp
Posted: Thu Oct 06, 2011 4:44 pm Post subject: RE:Assistance with some Game Elements
1. Yes, Time.Delay will stop your whole program. You have to do this by keeping track of when you last did that thing; by comparing that to the current time, you can figure out whether you're allowed to do that again right now.
2. There are a lot of different ways to handle this. Think about exactly how / when you want your enemies to show up, and base your solution on that.
3. What you want to do is restrict variable "angle" to a range ( -45, +90 ), for example. You can do this by saying "if angle is above 90, use 90; if angle is below -45, use -45; otherwise, use angle." This should be pretty straightforward.
4. I'm pretty sure Turing doesn't support that. Plenty of other languages do, but not Turing (as far as I know).
Dreadnought
Posted: Thu Oct 06, 2011 7:54 pm Post subject: Re: Assistance with some Game Elements
1- There are two ways I like to do this. The first is using Time.Elapsed, like DemonWasp said, to find out at what time the gun fires then use a loop in your program to check when a certain time has passed.
The second way (which I prefer) is using a separate process which runs alongside your main process. Something like:
Turing:
var gun_can_fire :boolean:=true
process gun_delay (time_to_wait)
gun_can_fire :=false Time.Delay(time_to_wait)
gun_can_fire :=true end gun_delay
%This actually runs the process fork gun_delay (200)
This way this separate process is halted by Time.Delay instead of your main process. You call this process using fork 'process_name'.
Then you just tell the game not to fire when your variable (ex gun_can_fire) is false.
2- If you just want to store something like numbers of mobs/types of mobs I suggest using records and/or arrays to store this information. But as DemonWasp said there are many ways to this and you should choose what works best for you.
3- Well, assuming that your gun is in the center of your screen, your probably finding this angle using the distance from the mouse cursor to the center of the screen. I would just put a cap on this distance. Say if the distance is larger than some maximum amount then set the distance equal to that amount.
4- No that's not possible in turing. If you are ready to kill to do this then you could perhaps write a short program in a different language to change the cursor, save it as an executable file, then run it with turing using Sys.Exec but I can't promise that this will work and it seems like too much effort anyway.
const xPos :=350 const yPos :=350 var x, y, b :int:=0% variables for the Mouse.Where var t :real% Theta, or the angle var bltx, blty, bltvx, bltvy :real:=0% the bullet positions, and the amount the x and y changes var bltd :boolean:=true% whether or not the bullet is live or dead
loop Mouse.Where(x, y, b) if x = xPos and y > yPos then
t :=270 elsif x < xPos and y = yPos then
t :=0 elsif x = xPos and y < yPos then
t :=90 elsif x > xPos and y = yPos then
t :=180 elsif x > xPos then
t :=arctand((y - yPos) / (x - xPos)) + 180 else
t :=arctand((y - yPos) / (x - xPos)) endif % This thing here calculates your angle. It'll make sense if you've ever done vectors before. It uses the function tan-1. The ones before it are for mathematical correctness; you cannot divide anything by zero.
if b =1and bltd =truethen% Only shoots when the bullet is dead and the button is clicked.
bltd :=false
bltx := xPos - (40*cosd(t))% setting the position of the bullet
blty := yPos - (40*sind(t))% same
bltvx := -5*cosd(t)% setting the change in x
bltvy := -5*sind(t)% setting the change in y endif
Draw.FillOval(xPos, yPos, 20, 20, blue) Draw.ThickLine(xPos, yPos, xPos - round(40*cosd(t)), yPos - round(40*sind(t)),4, 7)% This is important for drawing shapes according to an angle. You add onto the original postion. In the case of x, its the radius * cos(angle). For y it's radius * sin (angle)
if bltd =falsethen Draw.FillOval(round(bltx),round(blty),4, 4, brightred) Draw.FillOval(round(bltx),round(blty),2, 2, 7)
bltx += bltvx % Changes the bullet x position relative to the change in x
blty += bltvy % Changes the bullet y position relative to the change in y if bltx > maxxor bltx < 0or blty > maxyor blty < 0then% If the bullet hits the edge, it dies
bltd :=true endif endif View.Update cls endloop
Beastinonyou
Posted: Mon Oct 10, 2011 12:08 pm Post subject: Re: RE:Assistance with some Game Elements
Raknarg @ Mon Oct 10, 2011 10:04 am wrote:
I acheived this using a counter. Its rather simple, even if you have multiple gun types.
This code will ensure that the guy will only shoot once the loop has run at least 50 times.
That's another good way to do it, but I find Dreadnought's approach to delaying it works great, and very easy. just pass parameters for the delay on the gun, and fork it.
As for your detailed approach for my third question, I Greatly Appreciate the time and effort you put into that example. However, I think I'm going to be using pictures guns, and rotating them respectively. Although, you need a rotated picture for every angle within your range. Right now, I'm trying to get the pictures of the guns to rotate on the end of the barrel of the gun, rather than the bottom left corner of every picture, without getting a part of the gun chopped off when rotating.
Also, I drew up a zombie last night on Photoshop, and then used Puppet Warp to make 4 Frames, and made it into a gif. How would I go about making the Zombie move, but cycling through the images. What I'm thinking is having a counter in the loop, and having it increment (1 to 4), drawing the frames by that counter, and when it gets to 4, reset it back to 1.
Btw, Thanks for all feedback from anyone. I'm sure my final result will be great, considering It won't be due til' the end of January.
Insectoid
Posted: Mon Oct 10, 2011 1:17 pm Post subject: RE:Assistance with some Game Elements
I dunno how to stop bits of your gun getting clipped in rotation (might even just be your draw order) but limiting it is pretty easy. You'll probably want to pre-generate rotated images for every other degree of rotation to conserve image space (Turing can only load 1000 pictures I think) without sacrificing execution speed. If the mouse is at a higher angle than the gun is allowed to be, just point the gun at the max angle. Same for minimum angle.
Finally, for your zombie, that's exactly what you should do. I've never used a gif in Turing, but there should be documentation about it.
Raknarg
Posted: Mon Oct 10, 2011 4:29 pm Post subject: RE:Assistance with some Game Elements
Pic.FileNewFrames
I dont quite know how it works, but it seems to be made for the use of gifs. i have another method if you like, if you can split the gif into seperate images.
Sponsor Sponsor
Beastinonyou
Posted: Mon Oct 10, 2011 7:11 pm Post subject: Re: Assistance with some Game Elements
Turing:
var zString :string:="Zombie Frame " var imgType :string:=".bmp" var zFrames :array1.. 4ofint var zCounter :int:=0
There I have just a simple way of loading 4 Frames of the Zombie, drawing them, and moving them across the x-axis.
Now for other frames for different sprites, it's just a matter of adding them into the initializing for structure, and storing in an array
Side Note: Where did you find "Pic.FileNewFrames"? I looked and doesn't exist in Turing Documentation, nor recognized by syntax
Raknarg
Posted: Mon Oct 10, 2011 7:55 pm Post subject: RE:Assistance with some Game Elements
Strange. Idk, i was looking through the syntax database here on Compsci.
So about your second question: What exactly are you trying to do?
Beastinonyou
Posted: Mon Oct 10, 2011 8:55 pm Post subject: Re: Assistance with some Game Elements
Once I can get the basics of the game down, I have some ideas of how I can add another game mode or two, to make it even better. Obviously though, this isn't going to be a stick-men game.
How would I go about creating waves of enemies, such as how many of each type, when they appear, how close together they are (mobs / cluster), etc. Should I make enemies increment by a constant, use random variable to determine type, and random variable to choose when to spawn enemies? What about boss / harder levels? Also, I could instead use a percentage based system, where a certain type of enemy will have a 10% chance of spawning, compared to the more common 50%, etc. Then, I could just keep track of how many total have been spawned, and correlate that to how many per level / wave.
Seems like I think about stuff more when I'm trying to explain how I should do something, but a second opinion is always helpful.
I think I'm going to pause on the animation and sprites for the moment, and get the game to function, rather than focusing on Aesthetics.
Raknarg
Posted: Tue Oct 11, 2011 2:54 pm Post subject: RE:Assistance with some Game Elements
I think I would go with a percentage. The only other way I could think about having a specific order of enemies would be using a text file with a set of commands.
Dreadnought
Posted: Tue Oct 11, 2011 9:12 pm Post subject: Re: Assistance with some Game Elements
Well, first off, the rotating gun (sorry that I didn't visualize it properly last time). To change the point at which the picture rotates, changing x and y in Pic.Rotate (picID, angle, x, y) should do the trick. It might take a bit of experimenting to get the right place.
The cutting off of corners is a problem since the resulting picture is the same size as the original. So you could make the picture bigger by adding a margin of background color around the gun in photoshop. Or you could rotate the gun in photoshop and save all angles you want (this is kinda a last resort).
For animating sprites, Raknarg is right Pic.FilenewFrames and other built-in functions exist to help do the kind of things your thinking of. If you don't have that function then you probably have an old version of turing on your computer. I think anything past 4.0.0 should have these. (the final version is 4.1.1a but you should actually use 4.1 because they screwed the last version up). The download link is in a post at the top of the forum I think.
If you do have a relatively up-to-date version you should also look at the built-in Sprite procedures that are great for animating images. Its much easier to work with than the Pic functions in my opinion. You will need a loop like you described anyway to keep track of gif frames but the Sprite stuff helps if you want to have many animated images on a background.
Finally, the mob waves. This is really up to you. You could store the number of each kind of enemy for each level in an array that you either hardcode into your program or you could store it in a text file. You could also just have an array to store chances of spawning and or level at which certain enemies start to spawn. There really is no best way to do this. Some ways offer more control over spawning, others are simpler and can be scaled or changed much easier. I'd suggest not storing huge lists of numbers to control mob waves. My favorite is something along the lines of a minimum level for each mob to start appearing and then either a multiplier, an adder or a percentage (like you suggested) to allow more spawning in later waves.
Beastinonyou
Posted: Tue Oct 11, 2011 10:04 pm Post subject: Re: Assistance with some Game Elements
I have experimented with rotating guns already, and have got a couple to work very well, after fiddling around with the point in which it rotates, and extra space around the image.
Also, I came across that today during class, as I saw someone in the documentation on Sprite.Animate and saw that line: "Pic.FileNewFrames", and I'm like, Holy Crap. I looked through mine, and didn't see it.
I'm assuming that is because I have installed turing onto the computer (most recent version that lets you install without having to go to File > Open everytime to open a Turing File), so by actually running the 4.1.1a or whatever (infinite .1's.. lol), I'll most likely see this
Now, what I'm working on, is if I have 5 y-values (for enemies to move along, like rows), and I randomly generate a variable 1 to 5, and depending on that variable, an enemy will spawn in that row, and continue to move along that axis.
Turing:
var rowY :array1.. 5ofint:=init(10, 50, 90, 140, 190)% 5 Rows var spriteX, spriteY, spriteVx, spriteVy :int:=0 var choose :int:=0
proc drawSprite (x, y, vx :int) drawfillbox(x + vx, y, x + vx + 15, y + 15, black)% Body drawfillbox(x + vx, y + 15, x + vx + 15, y + 30, red)% Head drawfillbox(x + vx, y, x + vx + 15, y - 15, blue)% Legs % On actual sprites, damage multipliers will be given for headshots, etc. end drawSprite
fcn chooseRows :int randomize randint(choose, 1, 5) result rowY (choose) end chooseRows
that manages to show an example of the rows, however, I am repeatedly changing rows by calling the chooseRows function over again. How would I generate a random row an enemy is supposed to spawn to, and keep it moving along that same row, doing this everytime an enemy spawns.
Dreadnought
Posted: Wed Oct 12, 2011 5:16 pm Post subject: Re: Assistance with some Game Elements
Ok, I haven't done much in Turing in a while so I went crazy. What you want is a way of storing data about multiple enemies. You could use an array of records, but that means you have to set the size of the array to the maximum number of enemies you can ever have. You could use a flexible array and then you could resize it when you want to make it bigger, but my understanding of flexible arrays are that they are not completely properly implemented in Turing and I have had issues working with them in the past.
So my solution to this is a linked list (a collection in Turing). If you don't know what that is, think of it as a chain. Each link in the chain tells you what the next and previous links are and also contains its own data. They use pointers which essentially reference the memory location where your data is stored. So each element of the list has a pointer that points to the next and previous elements. This is nice because we can easily add or remove elements by changing pointers.
I'm not sure how advanced you are in Turing but here is a bunch of example code that does what your example did while keeping enemies on their row. Note that I'm using the Sprite module and many short forms that I have indicated. I had to attach it instead of posting it because its nearly 200 lines (ya I love linked lists that much).
Please remember that is only my suggestion (a long one too) and that there are other ways of accomplishing what you want with or without a linked list. If you do like this approach though but want to go much deeper into personalizing enemies then you could take a look at classes. But that might be too much for what you're trying to do.
Hope this helps.
Enemy_List_Example.t
Description:
My example for using a linked list to keep track of enemies on screen.