The Space Commander world is modelled as a 2D space, like looking at a map from above. Objects in the game exist and move in this 2D space. All our calculations will take place in this game world. When it’s time to draw objects on the screen we’ll then map the game world to our screen world.
To describe objects’ position and motion we need a few parameters.
This is the (x, y) coordinates of the object in the 2D game space. Screen coordinates tend to use the top left corner as the (0, 0) position with the positive x axis to the right and the positive y axis to the bottom. This differs from the conventional mathematical axes which have the positive x axis to the right but the positive y axis to the top.
It’s really a personal choice but I usually stick to the mathematical model unless the game world is quite simple. In this case we’ll be modelling vector quantities for velocity and acceleration, creating targeting code, etc., so using the mathematical model just makes it easier (for me) to see how angles and positions fit together.
You can make the game world match the screen world, but it doesn’t save much computing power and I can never get my head around the flipped maths!
When objects move or rotate, we need a way to describe their direction, or the way they are pointing. Using normal mathematical notation this gives us a 0 degree angle parallel to the positive x axis (i.e. horizontally to the right) with positive angles measured in an anti-clockwise direction from it.
Using this notation makes all our angle, sine, cosine and tangent calculations match with what we would expect.
As our world is imaginary, we don’t have to worry about real world distance measurements. Our positions will be easier if we use pixel coordinates, so it makes sense to measure distances in pixels as well.
Again, we get to choose our units of time. We can use seconds, but a more natural measurement inside the code is the game loop. Using one game loop as one unit of time simplifies the whole time process. If we set a speed of 5 pixels per loop, we simply need to add 5 pixels each time our game loop runs.
This method can have problems. It relies on the game loop running at a set speed. TIC80 runs the game loop at 60 loop per second. If our code gets too complex each game loop takes longer than 1/60 second to complete. Our game will start to slow down, and objects will visibly move slower on the screen.
If you’ve played any retro computer games, you’ll have seen this in action.
We want the player to be able to fly as far as they want in any direction, giving this idea of an infinite playing space. If we use a standard position, measured in pixels from a fixed origin we’ll hit a problem.
In computers, numbers have limits. If we use an integer variable to model the x coordinate of the player there will be a maximum (and minimum) value that this variable can store.
If you’re familiar with C programming, you’ll understand about using different numbers of bytes to store integers. A single byte (8 bit) integer can store values from 0 to 255. A 16-bit integer can store 0 to 65535. And so on.
If our player were to fly in one direction for a long time the coordinates may reach the limits of our variable. E.g. using 8-bit integers we can count up to 255, but adding 1 to this will result in an overflow error and our variable will wrap round to 0.
To get around this potential problem we’ll use the fact that we always want to player to be in the centre of the play area. So, we’ll use the player ship as our origin and have everything moving relative to it. So, if the player ship is travelling up, we keep the player stationary, but move everything else down, giving the same end movement.
In TIC80, which uses Lua, all numbers are represented by 64-bit floating-point numbers (real numbers with decimal places!). 10 bits are used for the exponent (the power of ten), 1 for the sign (positive and negative) and 53 for the mantissa (the significant figures of the number). This allows Lua to represent integer values from -2^53 to +2^53. We’d never reach that value, but I hope you get the idea of what we’re doing. If you ever do code in C, or even worse assembly, you’ll now know what to do!
So now we know the basic parameters we’re using let’s look at how we’ll model our data.
We want to create several helper functions that will allow us to calculate positions, movement, rotations and so on. We’ll have a few different object types for our player ship, enemy ships, missiles, stars, etc.
We could write separate functions for each object to, for example, rotate it. But each of these functions would basically be the same code repeated multiple times and this isn’t good coding practice.
So, we want each operation to be handled by a single helper function which will be able to work with all our objects.
e.g. rotate_object(player_ship) and rotate_object(enemy_ship) should both work.
This relies on each object in our game storing its model data in the same format. So, when we create a class (object definition) for each game object it will inherit this data format. This is one of the principles of Object Orientated Programming (OOP) – Inheritance. Or we can think of each object providing a defined set up to its values, an OOP Interface.
If you’re interested in learning Object Orientated Programming, please check out my Python courses on Udemy.
In TIC80 we have to implement this manually, but in other languages, such as Python, we can do it through the OOP coding system.
So, each object in the game will have,
We’ve already seen that the player ship isn’t going to move. Its position will always be at (0, 0). But it will have a velocity with angle and magnitude.
To move the player ship, we work out its x and y position increments, dx and dy.
1 2 |
dx = velocity.magnitude x cos(velocity.angle) dy = velocity.magnitude x sin(velocity.angle) |
We then take these away from the positions of every other object in the game.
1 2 3 4 |
for every_other_object position.x = position.x - dx position.y = position.y - dy next every_other_object |
Because we’re modelling our velocities as vectors (magnitude and direction) we can simply add our movements together to get the resultant motion. Each object has its own position and velocity, so we again calculate the incremental movement due to that motion and simply add that to its position after (or before) the movement relative to the player ship.
1 2 3 4 5 |
move object relative to player ship dx_object = velocity.magnitude x cos(velocity.angle) dy_object = velocity.magnitude x sin(velocity.angle) object.position.x = object.position.x + dx_object object.position.y = object.position.y + dy_object |
This then gives the new object position in (x, y) format using the player ship as the (0, 0) origin.
So, all of the previous calculations allow us to move our objects around in the 2D game world where we are obeying all the mathematical norms for the axes and angles. We need to work out how to draw this on the screen.
We know that we always want the player ship pointing vertically. This is a 90 degree angle, measured from the positive x axis. If the player ship is currently travelling at an angle A, we would need to rotate the whole game map by (90 – A) degrees to get the ship pointing up the screen.
Our screen coordinates are measured from the top left corner, with x increasing to the right and y increasing down the screen. So, to map our 2D game world to the screen we need to take the game world x coordinate, but the negative y coordinate. We then need to offset this by half the screen width and half the screen height to move the origin to the centre of the screen.
So, our helper function will look like,
1 2 3 |
new_position(x,y) = rotate (game_world_position around origin by (90 - player_ship_angle) degrees) new_position.x = new_position.x + screen_offset_x new_position.y = -new_position.y + screen_offset_y |
This leaves new position with the screen coordinates version of the game world position.
As we rotate an object around the origin to position it on the screen its coordinates will change, but so will its angle of rotation.
We know that we need to rotate the game world by (90 – player_ship_angle). So, when we draw an object on the screen world its rotation will also increase by the same amount,
1 |
object_screen_angle = object_game_world_angle + (90 - A) |
So, we’ve now got all the information and calculations we need to code this game world and map it to the screen.
Please have a look at the video and the code below to see how I’ve implemented our algorithms.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 |
-- title: Space Commander -- author: Bob Grant -- desc: -- script: lua -- global variables -- game states state={} state.start_screen = 0 state.playing = 1 state.player_exploding = 2 player_exploding_time = 300 state.level_up = 3 player_level_up_time = 300 state.end_game = 10 player_end_game_time = 400 state.scoreboard = 20 frame_timer = 0 -- TIC80 screen 240 x 136 screen_max_x = 239 screen_max_y = 136 screen_offset = {x=119, y=77} player_ship_max_bullets = 6 player_ship_rotation_speed = 0.015 player_ship_max_velocity = 1.5 player_ship_acceleration = 0.05 player_ship_radius = 7 player_ship_max_health = 50 player_bullet_velocity = player_ship_max_velocity * 2 player_bullet_max_age = 60 player_bullet_damage = 1 player_ship_collide_damage = 25 player_max_lives = 3 max_player_level = 4 max_enemies = 3 total_enemies = 3 enemy_id_counter = 0 enemy_ship_max_bullets = 6 enemy_ship_rotation_speed = 0.02 enemy_ship_max_velocity = 1.5 enemy_ship_radius = 4 enemy_max_health = 5 enemy_acceleration = 0.05 enemy_bullet_velocity = enemy_ship_max_velocity * 1.5 enemy_bullet_max_age = 60 enemy_bullet_damage = 1 enemy_bullet_reload = 150 enemy_target_distance = 70 enemy_shooting_range = 80 enemy_rebound_angle = math.pi enemy_rebound_speed = 1.0 enemy_min_visible_dist = 100 enemy_ship_respawn_dist = 2000 max_enemy_ship_type = 4 background_max_objects = 20 debug_value1=0 player_ship = {} player_bullets = {} enemy_bullets = {} enemies = {} particles={} background={} function init_variables(reset_enemies) -- player ship player_ship = {} player_ship.ship_type=player_level player_ship.position = {x=0, y=0} player_ship.velocity = {magnitude=0, angle=(math.pi/2)} player_ship.movement = {dx=0, dy=0} player_ship.radius = player_ship_radius player_ship.health = player_ship_max_health player_ship.timer = 0 if(reset_enemies) then -- respawn enemies enemies = {} max_enemies = (5*player_level) + 5 total_enemies = max_enemies * 3 enemy_id_counter = 0 else -- keep existing enemies but move them a bit further away for index,ship in ipairs(enemies) do ship.position.x = ship.position.x *1.5 ship.position.y = ship.position.y *1.5 ship.velocity.magnitude = 0 end -- for end particles={} background={} spawn_background{} end -- init_variables function init_game_state() game_state = state.start_screen last_game_state = game_state player_lives = player_max_lives player_score = 0 player_level = 0 end -- init_game_state ship_angle = 0 first_run = true function TIC() if first_run then for counter = 1,4 do spawn_enemy_test(counter, -100 +(40 * counter),50) end -- for first_run = false else cls() update_movement_params() update_background() check_ship_rotation() check_ship_acceleration() move_enemy_ships() draw_background() draw_player_ship() draw_enemy_ships() end -- if end -- TIC function spawn_enemy_test(test_ship_type, x,y) local enemy_ship = {} enemy_ship.ship_type = test_ship_type local spawn_angle = 0 local ship_x=x local ship_y=y enemy_ship.position = {x=ship_x, y=ship_y} enemy_ship.velocity = {magnitude=0, angle=0} enemy_ship.hit = false enemy_ship.movement_type=0 enemy_ship.timer = 0 if(enemy_ship.ship_type == 1) then enemy_ship.rotation_speed = 0.02 enemy_ship.max_velocity = 1.5 enemy_ship.max_health = 5 enemy_ship.health = 5 enemy_ship.acceleration = 0.05 enemy_ship.target_accuracy =0.2 enemy_ship.bullet_velocity = enemy_ship.max_velocity * 1.5 enemy_ship.bullet_max_age = 120 enemy_ship.bullet_damage = 1 enemy_ship.bullet_tracking = false enemy_ship.bullet_rotation_speed = 0.007 enemy_ship.bullet_trail = false enemy_ship.fire_speed = 32 enemy_ship.bullet_reload = 150 enemy_ship.bullets = 0 enemy_ship.bullet_radius = 4 enemy_ship.abs_max_bullets = 6 enemy_ship.target_distance = 50 enemy_ship.shooting_range = 80 enemy_ship.radius = 5 enemy_ship.score = 50 enemy_ship.bullets = 0 enemy_ship.max_bullets = math.floor(math.random(enemy_ship.abs_max_bullets)) + 1 elseif(enemy_ship.ship_type == 2) then enemy_ship.rotation_speed = 0.03 enemy_ship.max_velocity = 1.7 enemy_ship.max_health = 5 enemy_ship.health = 5 enemy_ship.acceleration = 0.1 enemy_ship.target_accuracy =0.15 enemy_ship.bullet_velocity = enemy_ship.max_velocity * 1 enemy_ship.bullet_max_age = 180 enemy_ship.bullet_damage = 2 enemy_ship.bullet_tracking = true enemy_ship.bullet_trail = true enemy_ship.bullet_rotation_speed = 0.01 enemy_ship.fire_speed = 32 enemy_ship.bullet_reload = 200 enemy_ship.bullets = 0 enemy_ship.bullet_radius = 4 enemy_ship.abs_max_bullets = math.floor(math.random(4)) + 1 enemy_ship.target_distance = 80 enemy_ship.shooting_range = 100 enemy_ship.radius = 6 enemy_ship.score = 100 enemy_ship.bullets = 0 enemy_ship.max_bullets = math.floor(math.random(enemy_ship.abs_max_bullets)) + 1 elseif(enemy_ship.ship_type == 3) then enemy_ship.rotation_speed = 0.03 enemy_ship.max_velocity = 1.8 enemy_ship.max_health = 5 enemy_ship.health = 5 enemy_ship.acceleration = 0.15 enemy_ship.target_accuracy =0.15 enemy_ship.bullet_velocity = enemy_ship.max_velocity * 1.5 enemy_ship.bullet_max_age = 180 enemy_ship.bullet_damage = 3 enemy_ship.bullet_tracking = false enemy_ship.bullet_trail = false enemy_ship.bullet_rotation_speed = 0.01 enemy_ship.fire_speed = 16 enemy_ship.bullet_reload = 200 enemy_ship.bullets = 0 enemy_ship.bullet_radius = 5 enemy_ship.abs_max_bullets = math.floor(math.random(6)) + 1 enemy_ship.target_distance = 80 enemy_ship.shooting_range = 100 enemy_ship.radius = 7 enemy_ship.score = 100 enemy_ship.bullets = 0 enemy_ship.max_bullets = math.floor(math.random(enemy_ship.abs_max_bullets)) + 1 elseif(enemy_ship.ship_type == 4) then enemy_ship.rotation_speed = 0.03 enemy_ship.max_velocity = 2 enemy_ship.max_health = 2 enemy_ship.health = 2 enemy_ship.acceleration = 0.2 enemy_ship.target_accuracy =0.15 enemy_ship.bullet_velocity = enemy_ship.max_velocity * 1 enemy_ship.bullet_max_age = 180 enemy_ship.bullet_damage = 3 enemy_ship.bullet_tracking = true enemy_ship.bullet_trail = true enemy_ship.bullet_rotation_speed = 0.014 enemy_ship.fire_speed = 16 enemy_ship.bullet_reload = 200 enemy_ship.bullets = 0 enemy_ship.bullet_radius = 5 enemy_ship.abs_max_bullets = math.floor(math.random(6)) + 1 enemy_ship.target_distance = 80 enemy_ship.shooting_range = 100 enemy_ship.radius = 8 enemy_ship.score = 200 enemy_ship.bullets = 0 enemy_ship.max_bullets = math.floor(math.random(enemy_ship.abs_max_bullets)) + 1 end table.insert(enemies,enemy_ship) end --spawn ememy function update_movement_params() player_ship.movement.dx= player_ship.velocity.magnitude * math.cos(player_ship.velocity.angle) player_ship.movement.dy= player_ship.velocity.magnitude * math.sin(player_ship.velocity.angle) end -- update_movement_params function check_ship_rotation() if(btn(2)) then player_ship.velocity.angle= player_ship.velocity.angle + player_ship_rotation_speed elseif(btn(3)) then player_ship.velocity.angle= player_ship.velocity.angle - player_ship_rotation_speed end player_ship.velocity.angle = wrap_angle(player_ship.velocity.angle) end --check_ship_rotation function check_ship_acceleration() if(btn(0)) then player_ship.velocity.magnitude= player_ship.velocity.magnitude + player_ship_acceleration if (player_ship.velocity.magnitude > player_ship_max_velocity) then player_ship.velocity.magnitude = player_ship_max_velocity end elseif(btn(1)) then player_ship.velocity.magnitude= player_ship.velocity.magnitude - player_ship_acceleration if (player_ship.velocity.magnitude < (-player_ship_max_velocity/2)) then player_ship.velocity.magnitude = (-player_ship_max_velocity/2) end end -- check for stationary if math.abs(player_ship.velocity.magnitude) < (player_ship_acceleration * 0.9) then player_ship.velocity.magnitude = 0 end end --check_ship_acceleration function move_relative_to_player(point) -- moves the point not the ship position point.x=point.x - player_ship.movement.dx point.y=point.y - player_ship.movement.dy return point end -- move_player_ship function move_by_velocity(point, velocity) -- moves the point local dx=velocity.magnitude * math.cos(velocity.angle) local dy=velocity.magnitude * math.sin(velocity.angle) point.x=point.x + dx point.y=point.y + dy return point end function move_enemy_ships() for index, ship in ipairs(enemies) do ship.position = move_relative_to_player(ship.position) end -- for end -- move_enemy_ships function spawn_enemy() if ((#enemies < max_enemies) and (enemy_id_counter < total_enemies)) then enemy_ship = {} enemy_ship.ship_type = 1 + (math.floor(math.random() * (player_level + 1))) if (enemy_ship.ship_type > max_enemy_ship_type) then enemy_ship.ship_type = max_enemy_ship_type end local spawn_angle = math.random() * (2 * math.pi) local spawn_distance = math.floor(math.random(500)) + 1000 local ship_x=spawn_distance * math.cos(spawn_angle) local ship_y=spawn_distance * math.sin(spawn_angle) enemy_ship.id = enemy_id_counter enemy_id_counter = enemy_id_counter + 1 enemy_ship.position = {x=ship_x, y=ship_y} enemy_ship.velocity = {magnitude=0, angle=0} enemy_ship.hit = false enemy_ship.movement_type=0 enemy_ship.timer = 0 if(enemy_ship.ship_type == 1) then enemy_ship.rotation_speed = 0.02 enemy_ship.max_velocity = 1.5 enemy_ship.max_health = 5 enemy_ship.health = 5 enemy_ship.acceleration = 0.05 enemy_ship.target_accuracy =0.2 enemy_ship.bullet_velocity = enemy_ship.max_velocity * 1.5 enemy_ship.bullet_max_age = 120 enemy_ship.bullet_damage = 1 enemy_ship.bullet_tracking = false enemy_ship.bullet_rotation_speed = 0.007 enemy_ship.bullet_trail = false enemy_ship.fire_speed = 32 enemy_ship.bullet_reload = 150 enemy_ship.bullets = 0 enemy_ship.bullet_radius = 4 enemy_ship.abs_max_bullets = 6 enemy_ship.target_distance = 50 enemy_ship.shooting_range = 80 enemy_ship.radius = 5 enemy_ship.score = 50 enemy_ship.bullets = 0 enemy_ship.max_bullets = math.floor(math.random(enemy_ship.abs_max_bullets)) + 1 elseif(enemy_ship.ship_type == 2) then enemy_ship.rotation_speed = 0.03 enemy_ship.max_velocity = 1.7 enemy_ship.max_health = 5 enemy_ship.health = 5 enemy_ship.acceleration = 0.1 enemy_ship.target_accuracy =0.15 enemy_ship.bullet_velocity = enemy_ship.max_velocity * 1 enemy_ship.bullet_max_age = 180 enemy_ship.bullet_damage = 2 enemy_ship.bullet_tracking = true enemy_ship.bullet_trail = true enemy_ship.bullet_rotation_speed = 0.01 enemy_ship.fire_speed = 32 enemy_ship.bullet_reload = 200 enemy_ship.bullets = 0 enemy_ship.bullet_radius = 4 enemy_ship.abs_max_bullets = math.floor(math.random(4)) + 1 enemy_ship.target_distance = 80 enemy_ship.shooting_range = 100 enemy_ship.radius = 6 enemy_ship.score = 100 enemy_ship.bullets = 0 enemy_ship.max_bullets = math.floor(math.random(enemy_ship.abs_max_bullets)) + 1 elseif(enemy_ship.ship_type == 3) then enemy_ship.rotation_speed = 0.03 enemy_ship.max_velocity = 1.8 enemy_ship.max_health = 5 enemy_ship.health = 5 enemy_ship.acceleration = 0.15 enemy_ship.target_accuracy =0.15 enemy_ship.bullet_velocity = enemy_ship.max_velocity * 1.5 enemy_ship.bullet_max_age = 180 enemy_ship.bullet_damage = 3 enemy_ship.bullet_tracking = false enemy_ship.bullet_trail = false enemy_ship.bullet_rotation_speed = 0.01 enemy_ship.fire_speed = 16 enemy_ship.bullet_reload = 200 enemy_ship.bullets = 0 enemy_ship.bullet_radius = 5 enemy_ship.abs_max_bullets = math.floor(math.random(6)) + 1 enemy_ship.target_distance = 80 enemy_ship.shooting_range = 100 enemy_ship.radius = 7 enemy_ship.score = 100 enemy_ship.bullets = 0 enemy_ship.max_bullets = math.floor(math.random(enemy_ship.abs_max_bullets)) + 1 elseif(enemy_ship.ship_type == 4) then enemy_ship.rotation_speed = 0.03 enemy_ship.max_velocity = 2 enemy_ship.max_health = 2 enemy_ship.health = 2 enemy_ship.acceleration = 0.2 enemy_ship.target_accuracy =0.15 enemy_ship.bullet_velocity = enemy_ship.max_velocity * 1 enemy_ship.bullet_max_age = 180 enemy_ship.bullet_damage = 3 enemy_ship.bullet_tracking = true enemy_ship.bullet_trail = true enemy_ship.bullet_rotation_speed = 0.014 enemy_ship.fire_speed = 16 enemy_ship.bullet_reload = 200 enemy_ship.bullets = 0 enemy_ship.bullet_radius = 5 enemy_ship.abs_max_bullets = math.floor(math.random(6)) + 1 enemy_ship.target_distance = 80 enemy_ship.shooting_range = 100 enemy_ship.radius = 8 enemy_ship.score = 200 enemy_ship.bullets = 0 enemy_ship.max_bullets = math.floor(math.random(enemy_ship.abs_max_bullets)) + 1 end table.insert(enemies,enemy_ship) end --if end --spawn ememy function spawn_background() -- initial background in player area background = {} for stars=1, background_max_objects do star_x = math.random(screen_max_x + (2*player_ship_max_velocity)) - player_ship_max_velocity - math.floor(screen_max_x/2) star_y = math.random(screen_max_y + (2*player_ship_max_velocity)) - player_ship_max_velocity - math.floor(screen_max_y/2) generate_background_object(0, star_x, star_y) end --for end -- spawn_background function update_background() local new_object=false for index,object in ipairs(background) do -- update for player moveent object.position = move_relative_to_player(object.position) object.velocity.angle = object.velocity.angle + object.rotation_speed -- wrap objects going out of working background area new_object=false if (object.position.x < -(player_ship_max_velocity + math.floor(screen_max_x/2))) then object.position.x = math.floor(screen_max_x/2) + player_ship_max_velocity new_object=true elseif (object.position.x > (player_ship_max_velocity + math.floor(screen_max_x/2))) then object.position.x = -(math.floor(screen_max_x/2) + player_ship_max_velocity) new_object=true end if (object.position.y < -(player_ship_max_velocity + math.floor(screen_max_y/2))) then object.position.y = math.floor(screen_max_y/2) + player_ship_max_velocity new_object=true elseif (object.position.y > math.floor(screen_max_y/2) + player_ship_max_velocity) then object.position.y = -(math.floor(screen_max_y/2) + player_ship_max_velocity) new_object=true end if (new_object) then object = change_background_object(object) end end -- for end -- update_background function generate_background_object(type, object_x, object_y) if (#background < background_max_objects) then -- make new object object = {} object.type = math.floor(math.random(10)) + 1 if (object.type>2) then object.type = 1 end object.position={} object.position.x = object_x object.position.y = object_y object.velocity={} object.velocity.magnitude=0 object.velocity.angle=0 object.rotation_speed = (math.random(4) - 2) /100 object.age=0 object.colour = math.floor(math.random(15)) + 1 table.insert(background, object) end end -- generate_background_object function change_background_object(object) object.type = math.floor(math.random(10)) + 1 if (object.type>2) then object.type = 1 end object.age=0 object.rotation_speed = (math.random(4) - 2) / 100 object.colour = math.floor(math.random(15)) + 1 return object end -- change_background_object function update_end_game() end -- update_end_game function update_scoreboard() end -- update_scoreboard function draw_start_screen() cls() print("SPACE COMMANDER", 30,30, 6,FALSE,2) spr(16,85,50,-1,3,0,0,2,2) print("PRESS Z TO PLAY", 35,110, 15,FALSE,2) end -- draw_start_screen function draw_playing() cls() --print(player_ship.health, 0,0) --print(angle_to_player.." - "..distance_to_player, 0,8) draw_background() draw_bullets() draw_player_ship() draw_enemy_ships() draw_particles() draw_radar() draw_dashboard() end -- draw_playing function draw_background() for index,object in ipairs(background) do draw_background_object(object) end -- for end -- draw_background function draw_background_object(object) local position = {} local velocity = {} position = translate_position_to_display(object.position) velocity = translate_velocity_to_display(object.velocity) if (object.type == 2) then pix(position.x, position.y, object.colour) pix(position.x+1, position.y, object.colour) pix(position.x, position.y+1, object.colour) pix(position.x+1, position.y+1, object.colour) elseif (object.type==3) then draw_object(10,{x=position.x, y=position.y},velocity.angle) else pix(position.x, position.y, object.colour) end end -- draw_background_object function draw_player_ship() local ship_icon = (player_level*2)+16 if(ship_icon > 22) then ship_icon=22 end spr(ship_icon, screen_offset.x-8, screen_offset.y-8, 0,1,0,0,2,2) end -- draw_player_ship function draw_enemy_ships() local position = {} local velocity = {} for index,ship in ipairs(enemies) do position = translate_position_to_display(ship.position) velocity = translate_velocity_to_display(ship.velocity) ship.screen_position = position if ((math.abs(ship.position.x ) < enemy_min_visible_dist) and (math.abs(ship.position.y) < enemy_min_visible_dist)) then circ(position.x, position.y,ship.radius,0) draw_object(ship.ship_type,position,velocity.angle) end end -- for end --draw_enemy_ships function draw_object(object_type, origin, angle) local object_shape = get_object_shape(object_type) local point={} local last_point={} local rotated_point for index,point in ipairs(object_shape) do -- rotate object point around {0,0} rotated_point = rotate_point({x=0, y=0}, point, angle) point.x = rotated_point.x point.y = rotated_point.y -- move object point to correct screen position point.x = point.x + origin.x point.y = -point.y + origin.y if (point.newline) then last_point = point else line(last_point.x, last_point.y, point.x, point.y, point.colour) last_point=point end end -- for end -- draw_object function get_object_shape(object_type) local object_shape = {} if (object_type == 0) then -- player object_type object_shape = {} table.insert(object_shape, {y=-5,x=-5, newline=true, colour=5}) table.insert(object_shape, {y=-5,x=0, newline=false, colour=5}) table.insert(object_shape, {y=0, x=5, newline=false, colour=5}) table.insert(object_shape, {y=5, x=0, newline=false, colour=5}) table.insert(object_shape, {y=5, x=-5, newline=false, colour=5}) table.insert(object_shape, {y=2, x=-5, newline=false, colour=5}) table.insert(object_shape, {y=2, x=-2, newline=false, colour=5}) table.insert(object_shape, {y=-2, x=-2, newline=false, colour=5}) table.insert(object_shape, {y=-2, x=-5, newline=false, colour=5}) table.insert(object_shape, {y=-5, x=-5, newline=false, colour=5}) table.insert(object_shape, {y=5, x=-5, newline=true, colour=5}) table.insert(object_shape, {y=2, x=-5, newline=false, colour=14}) table.insert(object_shape, {y=-2, x=-5, newline=true, colour=5}) table.insert(object_shape, {y=-5, x=-5, newline=false, colour=14}) elseif(object_type == 1) then -- level 1 ship object_shape = {} table.insert(object_shape, {x=-5, y=-5,newline=true, colour=5}) table.insert(object_shape, {x=0, y=-4,newline=false, colour=5}) table.insert(object_shape, {x=5, y=0, newline=false, colour=5}) table.insert(object_shape, {x=0, y=4, newline=false, colour=5}) table.insert(object_shape, {x=-5, y=5, newline=false, colour=5}) table.insert(object_shape, {x=-4, y=3, newline=false, colour=5}) table.insert(object_shape, {x=-3, y=0, newline=false, colour=5}) table.insert(object_shape, {x=-4, y=-3, newline=false, colour=5}) table.insert(object_shape, {x=-5, y=-5, newline=false, colour=5}) table.insert(object_shape, {x=4, y=0, newline=true, colour=14}) table.insert(object_shape, {x=2, y=1, newline=false, colour=14}) table.insert(object_shape, {x=0, y=0, newline=false, colour=14}) table.insert(object_shape, {x=2, y=-1, newline=false, colour=14}) table.insert(object_shape, {x=4, y=0, newline=false, colour=14}) table.insert(object_shape, {x=0, y=0, newline=false, colour=14}) elseif(object_type == 2) then -- level 2 ship object_shape = {} table.insert(object_shape, {x=-6, y=-6,newline=true, colour=13}) table.insert(object_shape, {x=3, y=-6,newline=false, colour=13}) table.insert(object_shape, {x=-2, y=-4, newline=false, colour=13}) table.insert(object_shape, {x=-3, y=-2, newline=false, colour=13}) table.insert(object_shape, {x=4, y=2, newline=false, colour=13}) table.insert(object_shape, {x=6, y=0, newline=false, colour=13}) table.insert(object_shape, {x=4, y=-2, newline=false, colour=13}) table.insert(object_shape, {x=-3, y=2, newline=false, colour=13}) table.insert(object_shape, {x=-2, y=4, newline=false, colour=13}) table.insert(object_shape, {x=3, y=6, newline=false, colour=13}) table.insert(object_shape, {x=-6, y=6, newline=false, colour=13}) table.insert(object_shape, {x=-4, y=0, newline=false, colour=13}) table.insert(object_shape, {x=-6, y=-6, newline=false, colour=13}) table.insert(object_shape, {x=5, y=0, newline=true, colour=9}) table.insert(object_shape, {x=3, y=1, newline=false, colour=9}) table.insert(object_shape, {x=1, y=0, newline=false, colour=9}) table.insert(object_shape, {x=3, y=-1, newline=false, colour=9}) table.insert(object_shape, {x=5, y=0, newline=false, colour=9}) table.insert(object_shape, {x=1, y=0, newline=false, colour=9}) elseif(object_type == 3) then -- level 3 ship object_shape = {} table.insert(object_shape, {x=-6, y=-0,newline=true, colour=6}) table.insert(object_shape, {x=8, y=-3,newline=false, colour=6}) table.insert(object_shape, {x=6, y=0, newline=false, colour=6}) table.insert(object_shape, {x=8, y=3, newline=false, colour=6}) table.insert(object_shape, {x=-6, y=0, newline=false, colour=6}) table.insert(object_shape, {x=-4, y=1, newline=true, colour=6}) table.insert(object_shape, {x=-2, y=6, newline=false, colour=6}) table.insert(object_shape, {x=2, y=2, newline=false, colour=6}) table.insert(object_shape, {x=-4, y=1, newline=true, colour=6}) table.insert(object_shape, {x=0, y=3, newline=false, colour=6}) table.insert(object_shape, {x=-4, y=1, newline=true, colour=6}) table.insert(object_shape, {x=-2, y=-6, newline=false, colour=6}) table.insert(object_shape, {x=2, y=-2, newline=false, colour=6}) table.insert(object_shape, {x=-4, y=-1, newline=true, colour=6}) table.insert(object_shape, {x=0, y=-3, newline=false, colour=6}) table.insert(object_shape, {x=5, y=0, newline=true, colour=14}) table.insert(object_shape, {x=3, y=1, newline=false, colour=14}) table.insert(object_shape, {x=1, y=0, newline=false, colour=14}) table.insert(object_shape, {x=3, y=-1, newline=false, colour=14}) table.insert(object_shape, {x=5, y=0, newline=false, colour=14}) table.insert(object_shape, {x=1, y=0, newline=false, colour=14}) elseif(object_type == 4) then -- level 4 ship object_shape = {} table.insert(object_shape, {x=-7, y=0,newline=true, colour=8}) table.insert(object_shape, {x=-4, y=1,newline=false, colour=8}) table.insert(object_shape, {x=-2, y=4,newline=false, colour=8}) table.insert(object_shape, {x=-4, y=4,newline=false, colour=8}) table.insert(object_shape, {x=-6, y=6,newline=false, colour=8}) table.insert(object_shape, {x=-7, y=9,newline=false, colour=8}) table.insert(object_shape, {x=-1, y=9,newline=false, colour=8}) table.insert(object_shape, {x=2, y=7,newline=false, colour=8}) table.insert(object_shape, {x=5, y=4,newline=false, colour=8}) table.insert(object_shape, {x=7, y=0,newline=false, colour=8}) table.insert(object_shape, {x=5, y=-4,newline=false, colour=8}) table.insert(object_shape, {x=2, y=-7,newline=false, colour=8}) table.insert(object_shape, {x=-1, y=-9,newline=false, colour=8}) table.insert(object_shape, {x=-7, y=-9,newline=false, colour=8}) table.insert(object_shape, {x=-6, y=-6,newline=false, colour=8}) table.insert(object_shape, {x=-4, y=-4,newline=false, colour=8}) table.insert(object_shape, {x=-2, y=-4,newline=false, colour=8}) table.insert(object_shape, {x=-4, y=-1,newline=false, colour=8}) table.insert(object_shape, {x=-7, y=-0,newline=false, colour=8}) table.insert(object_shape, {x=5, y=4,newline=true, colour=8}) table.insert(object_shape, {x=3, y=1,newline=false, colour=8}) table.insert(object_shape, {x=2, y=0,newline=false, colour=8}) table.insert(object_shape, {x=3, y=-1,newline=false, colour=8}) table.insert(object_shape, {x=5, y=-4,newline=false, colour=8}) elseif (object_type == 10) then --background star object_shape = {} table.insert(object_shape, {y=0,x=1, newline=true, colour=10}) table.insert(object_shape, {y=0,x=-1, newline=false, colour=math.floor(math.random(15)) + 1}) table.insert(object_shape, {y=1, x=0, newline=true, colour=10}) table.insert(object_shape, {y=-1, x=0, newline=false, colour=math.floor(math.random(15)) + 1}) elseif (object_type == 20) then -- player bullet object_shape = {} table.insert(object_shape, {x=0,y=0, newline=true, colour=10}) table.insert(object_shape, {x=5,y=0, newline=false, colour=10}) elseif (object_type == 21) then -- player bullet object_shape = {} table.insert(object_shape, {x=0,y=0, newline=true, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=3,y=0, newline=false, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=0,y=0, newline=true, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=-3,y=0, newline=false, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=0,y=0, newline=true, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=0,y=3, newline=false, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=0,y=0, newline=true, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=0,y=-3, newline=false, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=0,y=0, newline=true, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=-2,y=-2, newline=false, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=0,y=0, newline=true, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=2,y=-2, newline=false, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=0,y=0, newline=true, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=2,y=2, newline=false, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=0,y=0, newline=true, colour=math.floor(math.random(4))+7}) table.insert(object_shape, {x=-2,y=2, newline=false, colour=math.floor(math.random(4))+7}) elseif (object_type == 30) then -- enemy bullet type1 object_shape = {} table.insert(object_shape, {x=0,y=0, newline=true, colour=9}) table.insert(object_shape, {x=5,y=0, newline=false, colour=9}) elseif (object_type == 31) then -- enemy bullet type2 object_shape = {} table.insert(object_shape, {x=0,y=0, newline=true, colour=10}) table.insert(object_shape, {x=5,y=0, newline=false, colour=10}) table.insert(object_shape, {x=0,y=1, newline=true, colour=10}) table.insert(object_shape, {x=0,y=-1, newline=false, colour=10}) table.insert(object_shape, {x=3,y=1, newline=true, colour=10}) table.insert(object_shape, {x=3,y=-1, newline=false, colour=10}) elseif (object_type == 32) then -- enemy bullet type3 object_shape = {} table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=3,y=0, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=-3,y=0, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=3, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=-3, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=-2,y=-2, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=2,y=-2, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=2,y=2, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=-2,y=2, newline=false, colour=get_fire_colour()}) elseif (object_type == 33) then -- enemy bullet type4 object_shape = {} table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=3,y=0, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=-3,y=0, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=3, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=-3, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=-2,y=-2, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=2,y=-2, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=2,y=2, newline=false, colour=get_fire_colour()}) table.insert(object_shape, {x=0,y=0, newline=true, colour=get_fire_colour()}) table.insert(object_shape, {x=-2,y=2, newline=false, colour=get_fire_colour()}) end return object_shape end -- get_object_shape function get_fire_colour() -- returns a fire colour local colours = {6,8,12,14,15} return colours[math.random(#colours)] end -- get_fire_colour function rotate_point(origin, point, angle) local transposed_point = {} transposed_point.x = point.x - origin.x transposed_point.y = point.y - origin.y local rotated_point = {} rotated_point.x = (transposed_point.x * math.cos(angle)) - (transposed_point.y * math.sin(angle)) rotated_point.y = (transposed_point.y * math.cos(angle)) + (transposed_point.x * math.sin(angle)) -- update provided point rotated_point.x = rotated_point.x + origin.x rotated_point.y = rotated_point.y + origin.y return rotated_point end -- rotate_point function translate_position_to_display(position) local new_position = {} local adjusted_angle = (math.pi/2) - player_ship.velocity.angle adjusted_angle = wrap_angle(adjusted_angle) new_position = rotate_point({x=0,y=0}, position, adjusted_angle) new_position.x = new_position.x + screen_offset.x new_position.y = -new_position.y + screen_offset.y return new_position end -- translate_position_to_display function translate_velocity_to_display(velocity) local new_velocity={} new_velocity.angle = velocity.angle + (math.pi/2) - player_ship.velocity.angle new_velocity.angle = wrap_angle(new_velocity.angle) new_velocity.magnitude = velocity.magnitude return new_velocity end -- translate_velocity_to_display -- add two angle / magnitude vectors function add_velocities(v1, v2) local dx1=v1.magnitude * math.cos(v1.angle) local dy1=v1.magnitude * math.sin(v1.angle) local dx2=v2.magnitude * math.cos(v2.angle) local dy2=v2.magnitude * math.sin(v2.angle) local dx = dx1 + dx2 local dy = dy1 + dy2 local velocity = {} velocity.dx = dx velocity.dy = dy velocity.angle = math.atan2(dy, dx) velocity.magnitude = math.sqrt((dx*dx) + (dy*dy)) return velocity end function get_angle_to_target(subject, target) local dx = target.position.x-subject.position.x local dy = target.position.y-subject.position.y local angle_to_target = math.atan2(dy, dx) angle_to_target = wrap_angle(angle_to_target) return angle_to_target end --get_angle_to_target function wrap_angle(angle) local new_angle = angle if (new_angle > (2*math.pi)) then new_angle = new_angle - (2*math.pi) elseif(new_angle < 0) then new_angle = new_angle + (2*math.pi) end --if return new_angle end -- wrap_angle function add_angles(angle1, angle2) local new_angle = angle1 + angle2 new_angle = wrap_angle(new_angle) return new_angle end -- add_angles function get_angle_inc(target_angle, current_angle, target_accuracy) local increment = 0 local angle_diff = target_angle - current_angle if (angle_diff < 0) then angle_diff = (2*math.pi) + angle_diff end -- if if (math.abs(angle_diff) < target_accuracy) then increment = 0 elseif (angle_diff > math.pi) then increment = -1 else increment=1 end -- if return increment end -- get_angle_inc -- first run code -- run init functions to set up game init_game_state() init_variables(true) |