Stopping my controlled object going off the screen in pygame - pygame

i have started making a game in pygame using python 3.2.2
i have a title screen that has a new game button that when pressed a space background appears with a UFO you can control with the arrow keys
but my UFO can go off the screen in every direction,i want to make it stop on the border of the screen. and i want it to do this on every future screen that will appear later in the game.
i have no idea of the principles to do this...and where it should sit in the code. i read something about rectangles. do i need to define a rectangle then get the ufo to detect this rectangle and stop when it detects the rectangle
any help appreciated
im pretty good at working stuff out,so a push in the right direction would be appreciated.
thanks

What you need is collision detection. Here is some sample AABB collision detection code in python:
def collide(A_top, A_bot, A_left, A_right, B_top, B_bot, B_left, B_right):
if B_left < A_right and B_top < A_bot and B_right > A_left and B_bot > A_top:
return True #IS colliding
return False #IS NOT colliding
Now just collide with the borders of the screen, or anything else that happens to be on the screen.
OTHER SOLUTION:
Just check like this:
if ufo.x < 0 or ufo.y < 0 or ufo.x + ufo.width > screen.width or ufo.y + ufo.height > screen.height:
#Do collide code

Related

How to set a max/limit on how much a movieclip can be moved by x and y?

I'm making a Create-a-Character.One of the features is being able to adjust a facial feature’s placement.E.g. can move the nose up or down
(Through arrow buttons, example: 1 click on up button, move nose up by a little bit.)
But obviously I don't want the eyes or nose or lips floating outside the face or a nose ending up on a forehead that would be strange lol.
So how do I code so that the user can only move a movieclip a set amount of times in the chosen direction?
If you use just arrow buttons to move objects, it is very easy. Once a button is clicked, check object's position and move it if needed. Basic example:
// if arrow up clicked
if (nose.y > 100)
{nose.y -= 2}
// if down arrow clicked
if (nose.y < 140)
{nose.y += 2}
It's same for x axis and obviously, numbers 100 and 140 can be anything you want. It means, move objects just between these points.
User 987 answer is correct, however if your button moves the object at a faster pace say +-5, setting +-2 to the offset might not put it back in the boundary. It will correct itself by +-2 every frame there after, not accounting for if the user continues to hold down the button to try and exceed the boundary further. The nose will continue to glide further away.
The better way to implement this is to immediately set the nose back to the edge of the boundary.
if (nose.y > 140) {
nose.y = 140;
}

AS3 - Finding an objects x and y position relative to the stage

I'm new to ActionScript 3 and I have a character which you can control, the screen scrolls right along the stage and he can fire missiles.
The problem I'm getting is the missiles are created via these co-ords:
bullet.x = hero.mc.x;
bullet.y = hero.mc.y
These work fine untill the screen has scrolled to the right. I assume it's because the bullets are being spawned as a result of them using the canvas x,y and not the stages x,y
So i'm wondering how to find out the x and y of my hero in relative to the canvas so i can spawn the missiles on top of him!
Thanks, and if you need any more information let me know, I'm new to all this. Thank you.
You can do that with localToGlobal and globalToLocal. Your solution would be something like:
bulletPos = bullet.parent.localToGlobal(new Point(bullet.x, bullet.y));
Beware, though, as those are last resort functions. Normally, you'd have all your elements using the same 'layer', so comparisons are easier and faster.

How to Create Boundaries in AS3

I am new to AS3 and I wanted to know how to make a frame Boundary in flash, I am making a platformer game. My code Logic is like :
Whenever I move with ArrowKeys Move Background invert, so if I click Left key the background will move Left. so this is not really what I want as my Player (moving avatar) will always be stick to the middle of the stage. How to make a New boundary so whenever Player go near Right end of Stage ... Background should go left as much x moved outside boundary of player.
more illustration :
http://i.imgur.com/HOw6vHI.png
if(player.x > rightBound){
map.x += rightBound - player.x;
player.x = rightBound;
}
Repeat for the other 3 sides.

Controlling character movement, and keeping score while doing so, AS3

I have created a game where you have to follow the suggested arrow (going from right to left) to control the movement of the character. As you go trace the arrow from right to left, the characters position changes and he rubs his forehead. I wanted it to be very interactive, so you feel like you are controlling the character. As he rub his forehead, I want the "heat meter" to increase, and after it reaches hot, the level is completed. Is this even possible with Actionscript 3? Thank you in advance for letting me know, and if it is, how would I go about setting this up. I am very new to AS3. :/
Use gotoAndStop() to navigate to the head scratching menu using if statements (if(var thinking:Boolean == true) and to the heat meter: if(character.currentFrame == "scratchinghead") { var heatmeter:int ++; }
Then to win: if(heatmeter > 99) { gotoAndStop("win"); }
To check if the mouse is hovering, use MOUSE_ROLL_OVER.

how to make walls in actionscript 3.0?

i've been making a twist on the labyrinth game and i've got my ball to move with physics but im struggling with getting it to hit the walls around it. its currently a movie clip with black walls, and ive used this code to try and stop it:
if (character.hitTestObject(walls)){
character.x = //something
character.y = //something
}
all this does is when it hits any part of the movie clip, (even the blank spaces) it moves my character,
is there any sort of code i can use to maybe detect hitting a certain colour?
One way you could do this, is to use hitTestPoint() method to test if any of the corners have hit your wall.
hitTestPoint() tests only a single location to see if that point collides with an object. This is how you could test the top left corner of your character to see if it's touching the wall :
// I am assuming that x,y is the top left corner of your character
if (wall.hitPointTest(character.x, character.y, true))
{
// top left collided with wall
{
So you could do the same for all corners, or if you want, you can determine any collision points you want to check for the character.
Depending on your level of precision, this method might work just fine for your needs. But if you want pixel perfect collision, you can check out this link :
http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/