without warning with trace include win32lib.ew --I generally assume this is in your include path include graphex.ew integer wnd object gfx constant GAME_WIDTH = 640 constant GAME_HEIGHT = 480 wnd = create(Window, "Asteroid", 0, Default, Default, GAME_WIDTH, GAME_HEIGHT, {WS_POPUP,WS_THICKFRAME}) gfx = newGfx(wnd, 0, 0, GAME_WIDTH, GAME_HEIGHT) setRect(wnd, floor(SCREENWIDTH()/2 - GAME_WIDTH/2), floor(SCREENHEIGHT()/2 - GAME_HEIGHT/2), GAME_WIDTH, GAME_HEIGHT, w32False) constant gfxScale = 8 -- determines how big (most) shapes are -- shapes created with demo2-shapes.exw constant ASTEROID_NEW = 1, ASTEROID_ONE = 2, ASTEROID_TWO = 3, ASTEROID_END = 4 sequence asteroid asteroid = { {{2, 0, 0}, {-2, -2, 1}, {0, -2, 1}, {3, -1, 1}, {1, 2, 1}, {2, -1, 1}, {1, 2, 1}, {-2, 3, 1}, {-3, -1, 1}}, {{2, 0, 0}, {-2, -2, 1}, {1, -1, 1}, {2, 0, 1}, {1, 1, 1}, {-1, 1, 1}, {0, 1, 1}, {-1, 0, 1}}, {{2, 0, 0}, {-1, -2, 1}, {2, -1, 1}, {0, 1, 1}, {1, 1, 1}, {-1, 0, 1}, {-1, 1, 1}}, {{1, 0, 0}, {-1, -1, 1}, {1, -1, 1}, {1, 0, 1}, {0, 1, 1}, {-1, 0, 1}, {0, 1, 1}} } constant NUM_ASTEROIDS = 6 -- initial asteroid count sequence obstacle obstacle = {} sequence ext ext={{0, -4, 1}, {4, 0, 1}, {0, 4, 1}, {-4, 0, 1}, {1, -1, 0}, {2, -2, 1}, {-2, 0, 0}, {2, 2, 1}} sequence ship_shape ship_shape = {{-3, 3, 0}, {0, -3, 1}, {2, -1, 1}, {1, -3, 1}, {1, 3, 1}, {2, 1, 1}, {0, 3, 1}, {-2, 0, 1}, {0, -2, 1}, {-2, 0, 1}, {0, 2, 1}, {-2, 0, 1}, {2, -4, 0}, {1, 2, 1}, {1, -2, 1}, {-2, 0, 1}} sequence missile_shape missile_shape = {{-1, -1, 1}, {1, -3, 1}, {1, 3, 1}, {-1, 1, 1}, {0, -2, 0}, {-1, 1, 1}, {2, 0, 0}, {-1, -1, 1}} for i = 1 to length(asteroid) do asteroid[i] = gfxScaleShape(asteroid[i], gfxScale) end for ext = gfxScaleShape(ext, 4) sequence exit_btn exit_btn = {615, 16, ext} --after scaling constant T_GAME = 1001, T_PROTECT = 1004 setGfxAutoPaint(0) integer ships, score, ship_x, ship_y, mx, my, ship_dx, old_scale, ship_protect, level ships = 5 score = -10000 -- oops (10000 is added for a new level) level = 0 ship_x = floor(GAME_WIDTH/2) ship_y = GAME_HEIGHT - 70 sequence ship, missile ship = {{ship_x, ship_y, gfxScaleShape(ship_shape, 4)}, 0, 0} missile = {} -- none at the moment ship_protect = 1 -- protect a new ship briefly gfxClear(gfx, Black) gfx_draw(gfx, "shape", BrightWhite, exit_btn) -- the place where mouse events are handled for the gfx integer in_exit in_exit = BrightWhite procedure mouse_cage(integer self, integer event, sequence params) integer evt, mw sequence extent mx = params[2] my = params[3] evt = params[1] if gfxPointInSprite(mx, my, exit_btn) then in_exit = BrightRed else in_exit = BrightWhite end if if evt = LeftDown then if gfxPointInSprite(mx, my, exit_btn) then closeApp() end if if length(missile) = 0 and ships > 0 then -- fire missile missile = {{ship[1][1], ship_y, gfxScaleShape(missile_shape, gfxScale)}, 0, -15} end if end if if mx >= ship[1][1]+10 then ship_dx = 5 elsif mx <= ship[1][1]-10 then ship_dx = -5 else ship_dx = 0 end if end procedure -- set up the gfx to handle mouse events gfxAttachHandler(gfx, w32HMouse, routine_id("mouse_cage")) -- catch all mouse activity with the gfx procedure do_level_transition() -- in a library demo?? :) if 0 then end if end procedure -- return a number between -6 and 6 that is not -2, -1, 0, 1, or 2 -- which is +/- rand(3) + 3 function new_dx() integer num num = rand(3) + 3 if rand(100) < (3 * level) then num = -num end if return num end function function new_dy() return new_dx() end function procedure update_obstacles() atom dx, dy integer compass for i = 1 to length(obstacle) do if obstacle[i][1][1] > GAME_WIDTH then obstacle[i][1][1] = 0 end if if obstacle[i][1][2] > GAME_HEIGHT then obstacle[i][1][2] = 0 end if if obstacle[i][1][1] < 0 then obstacle[i][1][1] = GAME_WIDTH end if if obstacle[i][1][2] < 0 then obstacle[i][1][2] = GAME_HEIGHT end if dx = obstacle[i][2] dy = obstacle[i][3] obstacle[i][1][1] += dx obstacle[i][1][2] += dy if length(missile) and gfxSpriteOverlap(missile[1], obstacle[i][1]) then missile = {} if obstacle[i][4] = ASTEROID_END then -- last one score += 1000 obstacle = obstacle[1 .. i-1] & obstacle[i+1 .. length(obstacle)] return -- avoid for length problem else obstacle[i][4] += 1 obstacle[i][1][3] = asteroid[obstacle[i][4]] obstacle[i][2] = new_dx() obstacle[i][3] = new_dy() -- and add another one obstacle = append(obstacle, {{obstacle[i][1][1], obstacle[i][1][2], asteroid[obstacle[i][4]]}, new_dx(), new_dy(), obstacle[i][4]}) score += 50 * obstacle[i][4] end if end if if not ship_protect and gfxSpriteOverlap(obstacle[i][1], ship[1]) then ships = ships - 1 setTimer(wnd, T_PROTECT, 5000) ship[1][1] = floor(GAME_WIDTH/2) ship_protect = 1 end if end for if length(obstacle) = 0 then level += 1 score += 10000 do_level_transition() -- create more asteroids for i = 1 to 1 + level do -- {shape, dx, dy} obstacle = append(obstacle, {{rand(GAME_WIDTH), rand(300), asteroid[ASTEROID_NEW]}, new_dx(), new_dy(), ASTEROID_NEW}) end for end if end procedure procedure draw_obstacles() for i = 1 to length(obstacle) do gfx_draw(gfx, "shape", White, obstacle[i][1]) end for end procedure procedure draw_gamestats() for i = 1 to ships - 1 do gfx_draw(gfx, "shape", BrightGreen, {ship[1][1] - (i * 15), GAME_HEIGHT - 20 , ship_shape}) end for gfxTextOutAt(gfx, floor(GAME_WIDTH/2 - 30), 20, sprintf("Score: %06d", {score}), BrightGreen) gfxTextOutAt(gfx, floor(GAME_WIDTH/2 - 210), 20, sprintf("Level: %d", {level}), BrightGreen) end procedure procedure update_ship() if ship[1][1] = mx then ship_dx = 0 end if if ship_dx = 0 then return end if ship[1][1] += ship_dx if ship[1][1] > 620 then ship[1][1] = 620 -- just a guess end if if ship[1][1] <= 20 then ship[1][1] = 20 end if end procedure procedure draw_ship() if ship_protect = 1 then gfx_draw(gfx, "shape", BrightMagenta, ship[1]) ship_protect = 2 elsif ship_protect = 2 then gfx_draw(gfx, "shape", BrightGreen, ship[1]) ship_protect = 1 else gfx_draw(gfx, "shape", Yellow, ship[1]) end if end procedure procedure update_missile() missile[1][1] += missile[2] -- redundant at the moment missile[1][2] += missile[3] if missile[1][2] < 0 then missile = {} -- that was obviously useless :) score = score - 15 end if end procedure procedure draw_missile() if length(missile) then gfx_draw(gfx, "shape", Orange, missile[1]) end if end procedure setTimer(wnd, T_PROTECT, 5000) procedure timer_evt(integer self, integer event, sequence params) integer id id = params[1] if id = T_GAME then update_ship() update_obstacles() gfxClear(gfx, Black) gfx_draw(gfx, "shape", in_exit, exit_btn) if length(missile) then update_missile() draw_missile() end if draw_obstacles() draw_gamestats() if ships <= 0 then old_scale = setGfxFontScale(13) gfxTextOutAt(gfx, 100, 200, "GAME OVER", BrightBlue) gfxTextOutAt(gfx, 105, 205, "GAME OVER", BrightWhite) VOID = setGfxFontScale(old_scale) else draw_ship() end if doGfxPaint(wnd) elsif id = T_PROTECT then -- intro protection time ship_protect = 0 killTimer(wnd, T_PROTECT) end if end procedure setHandler(wnd, w32HTimer, routine_id("timer_evt")) setTimer(wnd, T_GAME, 70) -- 70ms update include w32start.ew -- crude collision detection -- skip this, as the collision detection doesn't work and -- it doesn't make sense to do it either :) -- (ever seen asteroids bouncing around off of each other??) -- for j = 1 to length(obstacle) do -- if j != i then -- -- determine how (if any) obstacle i overlaps obstacle j --- compass = gfxSpriteOverlap(obstacle[i][1], obstacle[j][1]) -- if compass = COMPASS_NORTH or compass = COMPASS_SOUTH then -- -- reverse y direction only -- obstacle[i][3] = -obstacle[i][3] -- elsif compass = COMPASS_WEST or compass = COMPASS_EAST then -- -- reverse x direction only -- obstacle[i][2] = -obstacle[i][2] --- elsif compass = COMPASS_NOREAST or compass = COMPASS_SOUWEST then -- -- reverse x and y -- obstacle[i][2] = -obstacle[i][2] -- obstacle[i][3] = -obstacle[i][3] -- elsif compass = COMPASS_SOUEAST or compass = COMPASS_NORWEST then -- -- end if -- -- end for