Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Jul 26, 2017 1:06:02 GMT -6
I'm looking trying my hand at Asteroids. After all, who doesn't like a cool game of asteroids... lol
I managed to figure out how to rotate a sprite (DrawImage_Rotate()) using the keyboard, but my BIG questions is, "How to shoot in the direction the sprite is facing?' Ideally I would like to control the navigation of the 'ship' with the arrow keys, use the mouse to 'point' towards the asteroids, then use the mouse button to shoot....
But first I need to know how to shoot in the direction the ship is facing? Any Ideas?
J
|
|
|
Post by n00b on Jul 27, 2017 6:26:12 GMT -6
Sorry about the late reply, I have been really sick lately. What you are trying to do is a basic trigonometry problem. Here is a basic code example:
Function rotateX(center_x, half_width, rot) H = Cos(rot) * half_width x = center_x + H Return x End Function
Function rotateY(center_y, half_height, rot) V = Sin(rot) * half_height y = center_y + V Return y End Function
WindowOpen(0, "tst", windowpos_Centered, windowpos_centered, 640, 480, 0) CanvasOpen(0, 640, 480, 0, 0, 640, 480, 0)
SetColor(rgb(255,0,0)) obj_x = 50 obj_y = 50 obj_width = 50 obj_height = 50 Rect(obj_x, obj_y, obj_width, obj_height)
SetColor(rgb(0,255,0)) For i = 0 to 359 x = rotateX( obj_x + (obj_width/2), obj_width/2, i ) y = rotateY( obj_y + (obj_height/2), obj_height/2, i) pset(x, y) Next
update()
waitkey()
You can just use the rotateX and rotateY functions from this example in your project if you want. Center_x and Center_y refer to the center of what you are rotating. The half_width and half_height are literally what they say, half the width and height of what you are rotating. And rot is the angle of your rotation. Hopefully this helps.
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Jul 27, 2017 16:06:52 GMT -6
Ah. Many thanks. This will either get the creative juices flowing or cause a simulated caffeine withdrawal... lol. I seriously hope my grandkids are going to like the game... But, knowing kids, their attention for 'new' stuff will probably distract them. They may play it, they may not, but try we must.
J
ps: I hope that you are feeling better...
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Jul 27, 2017 16:45:04 GMT -6
I just found the 'DrawImage_Rotate()'. If I had have found this earlier then you would have been spared the effort of your example. My apologies....
J
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Jul 27, 2017 18:24:15 GMT -6
Here is a crude test for rotation and firing (laser at the moment...) That's it. Nothing fancy. Rotate with left or right keys and fire with space, or joystick left and right and button 0 to fire. WindowOpen(0, "Rotate", 0, 0, 640, 480, 0) CanvasOpen(0, 640, 480, 0, 0, 640, 480, 0) Canvas(0)
loadImage(1,"assets/ship16.png") loadImage(2,"assets/bullet.png") player = 1 playerx = 312 playery = 232 angle = 0 rotSpeed = 0.5
shot = 2 shotx = playerx + 7 shoty = playery + 8 shotAngle = angle
pi = 3.141592654
while key(k_escape)=0 ClearCanvas() if key(k_left) or JoyAxis(0,0) < 0 then angle = angle - rotSpeed if angle < 0 then angle = 360 end if end if if key(k_right) or JoyAxis(0,0) > 0 then angle = angle + rotSpeed if angle > 360 then angle = 0 end if end if if key(k_space) or JoyButton(0,0) <> 0 then dx = (playerx + 7) + cos(angle*(pi/180)) * 200 dy = (playery + 8) + sin(angle*(pi/180)) * 200 setColor(rgb(255,0,0)) line(playerx + 7, playery + 8, dx, dy) 'rectfill(playerx +7,playery + 8,2,2) end if DrawImage_Rotate(player, playerx, playery, angle)
update() wend Assets are attached. The next part is to use moving bullets... I hope... J rotate-n-fire.zip (1.86 KB)
|
|