/* * SPL - The SPL Programming Language * Copyright (C) 2004, 2005 Clifford Wolf * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * pong.spl: Example for the "sdl" and "multimouse" modules: a pong game */ load "sdl"; load "multimouse"; var p1_mouse = 0; var p2_mouse = 1; var p1_direction = "bottom"; var p2_direction = "bottom"; sdl_init(640, 480, fullscreen: 1); sdl_title("SPL Pong"); #define pi 3.14159265 var img_background = sdl_image_load("background.png"); var img_bar_bottom = sdl_image_load("bar_bottom.png"); var img_bar_top = sdl_image_load("bar_top.png"); var img_ball = sdl_image_load("ball.png"); var sprite_background = sdl_sprite_create(); var sprite_bar_bottom = sdl_sprite_create(); var sprite_bar_top = sdl_sprite_create(); var sprite_ball = sdl_sprite_create(); sprite_background.image = img_background; sprite_bar_bottom.image = img_bar_bottom; sprite_bar_top.image = img_bar_top; sprite_ball.image = img_ball; sprite_bar_top.y = 0; sprite_bar_top.z = 1; sprite_bar_bottom.y = 450; sprite_bar_bottom.z = 1; sprite_ball.z = 2; var ball_x = 640./2 .- 10; var ball_y = 480./2 .- 10; var ball_speed = 10; var ball_dir = 90; var p1_pos = 640/2 - 50; var p2_pos = 640/2 - 50; var p1_score = 0; var p2_score = 0; var perf_ticks = 0; var perf_count = 0; if (defined multimouse[p1_mouse].error) write("Warning: Can't open /dev/input/mouse${p1_mouse}: ${multimouse[p1_mouse].error}\n"); if (defined multimouse[p2_mouse].error) write("Warning: Can't open /dev/input/mouse${p2_mouse}: ${multimouse[p2_mouse].error}\n"); multimouse[p1_mouse].direction = p1_direction; multimouse[p2_mouse].direction = p2_direction; function draw_box(x, y) { var buffer = sdl_copy(img_background, x, y, 10, 10); sdl_fill(buffer, 0, 0, 10, 10, 0, 0, 0, 128); sdl_blit(img_background, buffer, x, y); } while (not sdl_keystate("escape") and not sdl_keystate("q") and p1_score < 9 and p2_score < 9) { sprite_bar_top.x = p1_pos; sprite_bar_bottom.x = p2_pos; sprite_ball.x = ball_x; sprite_ball.y = ball_y; sdl_sprite_update(); perf_ticks += sdl_delay(25); perf_count++; if (perf_count > 1000) { var perf = perf_ticks ./ perf_count; write("Avg. sdl_delay(25) ret: $perf (${1000 ./ (25 .- perf)} fps)\n"); perf_count = perf_ticks = 0; } if (sdl_keystate("a")) p1_pos -= 10; if (sdl_keystate("d")) p1_pos += 10; if (sdl_keystate("left")) p2_pos -= 10; if (sdl_keystate("right")) p2_pos += 10; multimouse_update(); p1_pos += multimouse[p1_mouse].dx; p2_pos += multimouse[p2_mouse].dx; if (p1_pos < -90) p1_pos = -90; if (p2_pos < -90) p2_pos = -90; if (p1_pos > 630) p1_pos = 630; if (p2_pos > 630) p2_pos = 630; ball_x = ball_x .+ cos((ball_dir ./ 360) .* 2 .* pi) .* ball_speed; ball_y = ball_y .+ sin((ball_dir ./ 360) .* 2 .* pi) .* ball_speed; if (ball_dir > 90 and ball_dir < 270) { if (ball_x < 0) ball_dir = 180 - ball_dir; } else { if (ball_x > 620) ball_dir = 180 - ball_dir; } if (ball_y < -10 or ball_y > 470) { var goal_y; if (ball_y > 460) { draw_box(620, 10 + p1_score++*20); goal_y=430; } if (ball_y < 0) { draw_box(10, 460 - p2_score++*20); goal_y=0; } var buffer = sdl_copy(undef, 0, goal_y, 640, 50); sdl_fill(buffer, 0, 0, 640, 50, 255, 0, 0, 128); sdl_blit(undef, buffer, 0, goal_y); sdl_update(0, goal_y, 640, 50); sdl_delay(500); sdl_sprite_redraw(); ball_x = 640./2 .- 10; ball_y = 480./2 .- 10; p1_pos = 640/2 - 50; p2_pos = 640/2 - 50; ball_speed = 10; ball_dir = (p1_score + p2_score) % 2 == 0 ? 90 : 270; multimouse_update(); } if (ball_y < 30 and ball_x.+20 > p1_pos and ball_x < p1_pos.+100) ball_dir = 180 - (50 + ball_x - p1_pos); if (ball_y > 430 and ball_x.+20 > p2_pos and ball_x < p2_pos.+100) ball_dir = 230 + ball_x - p2_pos; ball_dir = (ball_dir + 360) % 360; ball_speed = ball_speed .+ 0.01; } sdl_quit(); write(<:> : : Score: : ------ : Player #1 ... $p1_score : Player #2 ... $p2_score : );