wxAsteroids 1.0 → 1.1
raw patch · 5 files changed
+161/−112 lines, 5 filesdep ~basedep ~wx
Dependency ranges changed: base, wx
Files
- Asteroids.lhs +66/−59
- CHANGELOG.md +22/−0
- README.md +58/−0
- README.txt +0/−43
- wxAsteroids.cabal +15/−10
Asteroids.lhs view
@@ -3,20 +3,23 @@ Asteroids.lhs + The game consists of a spaceship that can move to the left and right + using the arrow keys. There is an infinite supply of random rocks + (asteroids) that move vertically downwards. Whenever the spaceship + hits a rock, the rock becomes a flaming ball. In a more realistic + version, this would destroy the ship, but we choose a more peaceful + variant here. + > import Graphics.UI.WX > import Graphics.UI.WXCore as WXCore > import System.Directory (setCurrentDirectory) > import System.Random > import Paths_wxAsteroids (getDataDir) -The game consists of a spaceship that can move to the left and right -using the arrow keys. There is an infinite supply of random rocks -(asteroids) that move vertically downwards. Whenever the spaceship -hits a rock, the rock becomes a flaming ball. In a more realistic -version, this would destroy the ship, but we choose a more peaceful -variant here. We start by defining some constants: + We start by defining some constants: + > height :: Int > height = 300 @@ -29,14 +32,14 @@ > chance :: Double > chance = 0.1 -One can access wxHaskell functionality, like the portable -database binding, without using the GUI functionality. - -For simplicity, we use fixed dimensions for the game field, given -by width and height. The diameter is the diameter of the rocks, and -the chance is the chance that a new rock appears in a given time -frame. The main function of our game is asteroids that creates the -user interface: + One can access wxHaskell functionality, like the portable + database binding, without using the GUI functionality. + + For simplicity, we use fixed dimensions for the game field, given + by width and height. The diameter is the diameter of the rocks, and + the chance is the chance that a new rock appears in a given time + frame. The main function of our game is asteroids that creates the + user interface: > asteroids :: IO () > asteroids = @@ -53,7 +56,6 @@ > set f [statusBar := [status]] > > t <- timer f [ interval := 50 -> -- , on command := advance vrocks f > , on command := advance status vrocks f > ] > @@ -72,29 +74,35 @@ > set pause [on command := set t [enabled :~ not]] > set quit [on command := close f] -The quit menu simply closes the frame. The pause menu toggles the -enabled state of the timer by applying the not function. Turning off -the timer effectively pauses the game. The new menu is interesting -as it starts a completely new asteroids game in another frame. As we -dont use any global variables, the new game functions completely -independent from any other asteroids game. Finally, we show the -menu by specifying the menu bar of the frame: + The quit menu simply closes the frame. The pause menu toggles the + enabled state of the timer by applying the not function. Turning off + the timer effectively pauses the game. The new menu is interesting + as it starts a completely new asteroids game in another frame. As we + dont use any global variables, the new game functions completely + independent from any other asteroids game. Finally, we show the + menu by specifying the menu bar of the frame: > set f [menuBar := [game]] > -> set f [ text := "Asteroids" +> shipWidth <- bitmapGetWidth ship +> +> set f [ text := "Asteroids" > , bgcolor := white > , layout := space width height > , on paint := draw vrocks vship > , on leftKey := varUpdate vship (\x -> max 0 (x - 5)) >> return () -> , on rightKey := varUpdate vship (\x -> min width (x + 5)) >> return () +> , on rightKey := varUpdate vship (\x -> min (width - shipWidth) (x + 5)) >> return () > , on (charKey '-') := set t [interval :~ \i -> i * 2] > , on (charKey '+') := set t [interval :~ \i -> max 10 (div i 2)] > ] -The status is passed to the advance function, which updates the -status field with the count of rocks that are currently visible: + MOD HJvT 121216 Set focus, to let Linux builds respond to keyboard +> WXCore.windowSetFocus f + + The status is passed to the advance function, which updates the + status field with the count of rocks that are currently visible: + > advance :: (Textual w, Paint w1) => w -> Var [[a]] -> w1 -> IO () > advance status vrocks f = > do @@ -103,9 +111,9 @@ > set status [text := "rocks: " ++ show (length r)] > repaint f -The vrocks variable holds an infinite list of all future rock positions. -This infinite list is generated by the randomRocks function -that takes a random number generator g as its argument: + The vrocks variable holds an infinite list of all future rock positions. + This infinite list is generated by the randomRocks function + that takes a random number generator g as its argument: > randomRocks :: RandomGen g => g -> [[Point]] > randomRocks g = flatten [] (map fresh (randoms g)) @@ -129,15 +137,15 @@ > track x = [point x (y - diameter) | y <- [0, 6 .. height + 2 * diameter]] -The standard randoms function generates an infinite list of random -numbers in the range [0, 1). The fresh function compares each number -agains the chance, and if a new rock should appear, it generates -a finite list of positions that move the rock from the top to the bottom -of the game field. The expression map fresh (randoms g) -denotes an infinite list, where each element contains either an empty -list, or a list of positions for a new rock. Finally, we flatten this list -into a list of time frames, where each element contains the position -of every rock in that particular time frame. + The standard randoms function generates an infinite list of random + numbers in the range [0, 1). The fresh function compares each number + agains the chance, and if a new rock should appear, it generates + a finite list of positions that move the rock from the top to the bottom + of the game field. The expression map fresh (randoms g) + denotes an infinite list, where each element contains either an empty + list, or a list of positions for a new rock. Finally, we flatten this list + into a list of time frames, where each element contains the position + of every rock in that particular time frame. > draw :: Var [[Point2 Int]] -> Var Int -> DC a -> b -> IO () > draw vrocks vship dc _view = @@ -156,22 +164,21 @@ > when (or collisions) > (play explode) -The draw function was partially parameterised with the vrocks and -vship variables. The last two parameters are supplied by the paint -event handler: the current device context (dc) and view area (view). -The device context is in this case the window area on the screen, -but it could also be a printer or bitmap for example. - -First, we retrieve the current rocks and x position of the spaceship. -The position of the spaceship, ship, is at a fixed y-position. The -current rock positions are simply the head of the rocks list. The -collisions list tells for each rock position whether it collides with the -ship. Finally, we draw the ship and all the rocks. As a final touch, -we also play a sound fragment of an explosion when a collision -has happened. The collide function just checks if two positions -are too close for comfort using standard vector functions from the -wxHaskell library: - + The draw function was partially parameterised with the vrocks and + vship variables. The last two parameters are supplied by the paint + event handler: the current device context (dc) and view area (view). + The device context is in this case the window area on the screen, + but it could also be a printer or bitmap for example. + + First, we retrieve the current rocks and x position of the spaceship. + The position of the spaceship, ship, is at a fixed y-position. The + current rock positions are simply the head of the rocks list. The + collisions list tells for each rock position whether it collides with the + ship. Finally, we draw the ship and all the rocks. As a final touch, + we also play a sound fragment of an explosion when a collision + has happened. The collide function just checks if two positions + are too close for comfort using standard vector functions from the + wxHaskell library: > collide :: Point2 Int -> Point2 Int -> Bool > collide pos0 pos1 = @@ -189,11 +196,11 @@ > in drawBitmap dc rockPicture pos True [] -The drawBitmap function takes a device context, a bitmap, a position, -the transparency mode, and a list of properties as arguments. -The bitmap for a rock is changed to a burning ball when it collides -with the spaceship. To finish the program, we define the resources -that we used: + The drawBitmap function takes a device context, a bitmap, a position, + the transparency mode, and a list of properties as arguments. + The bitmap for a rock is changed to a burning ball when it collides + with the spaceship. To finish the program, we define the resources + that we used: > rock :: Bitmap () > rock = bitmap "rock.ico"
+ CHANGELOG.md view
@@ -0,0 +1,22 @@+ + +Change log wxAsteroids +====================== + +2015-09-23 Version 1.1 +---------------------- + +* Set focus to frame, to let Linux builds respond to keyboard +* Limited ship movement, to keep it in sight, when moving to the right +* Maintainer set to wxHaskell developer mailing list +* Source-repository address added to cabal file +* Updated links in README.md and wxAsteroids.cabal +* Improved formatting in README.md + + + +2009-03-07 Version 1.0 +---------------------- + +Converted from the document "wxHaskell - A Portable and Concise GUI +Library for Haskell", updated and cabalized.
+ README.md view
@@ -0,0 +1,58 @@+wxAsteroids+===========++A demonstration of how to use wxHaskell+---------------------------------------++Your space ship enters an asteroid belt, try to avoid +collisions!++wxAsteroids is a game demonstrating the wxHaskell GUI.+To run the game, you will need wxHaskell, see:++ <https://wiki.haskell.org/WxHaskell#Documentation>++If wxHaskell is installed, and you have cabal-install +on your system, give the folllowing command to install +wxAsteroids:++```sh+cabal install wxAsteroids+```++Another option is, to download the wxAsteroids tarball from+[Hackage](http://hackage.haskell.org/package/wxAsteroids)+and unpack it; go to the directory with the game code +and enter the commands: ++```sh+runhaskell Setup configure+runhaskell Setup build+runhaskell Setup install+```++You will get a message about the directory in which the +executable is installed; this directory must be in the +search path. ++Give the following command to start the game:++```sh+wxAsteroids+```++Controls:++ * Use the left and right cursor keys to move the ship sideways. + * Ctrl-n creates a new window with a new Asteroids game. + * Ctrl-p pauses/resumes the game. + * To increase the speed of the space ship, press '+'; + * to slow down, press '-'.++For a detailed description of wxHaskell and the program, see:+[wxHaskell, A Portable and Concise GUI Library for +Haskell](http://legacy.cs.uu.nl/daan/download/papers/wxhaskell.pdf)++Further information about wxHaskell can be found at:+ <https://wiki.haskell.org/WxHaskell>+
− README.txt
@@ -1,43 +0,0 @@- -Your space ship enters an asteroid belt, try to avoid -collisions! - -wxAsteroids is a game demonstrating the wxHaskell GUI. - -To run the game, you will need wxHaskell, see: - http://www.haskell.org/haskellwiki/WxHaskell/Download - -If wxHaskell is installed, and you have cabal-install -on your system, give the folllowing command to install -wxAsteroids: - cabal install wxAsteroids - -Another option is, to download the wxAsteroids tarball -from Hackage: - http://hackage.haskell.org/cgi-bin/hackage-scripts/package/wxAsteroids -and unpack it; go to the directory with the game code -and enter the commands: - runhaskell Setup configure - runhaskell Setup build - runhaskell Setup install - -You will get a message about the directory in which the -executable is installed; this directory must be in the -search path. - -Give the following command to start the game: - wxAsteroids - -Use the left and right cursor keys to move the ship sideways. -Ctrl-n creates a new window with a new Asteroids game. -Ctrl-p pauses/resumes the game. -To increase the speed of the space ship, press '+'; -to slow down, press '-'. - -For a detailed description of wxHaskell and the program, see: -"wxHaskell, A Portable and Concise GUI Library for Haskell" - http://legacy.cs.uu.nl/daan/download/papers/wxhaskell.pdf - -Further information about wxHaskell can be found at: - http://www.haskell.org/haskellwiki/WxHaskell -
wxAsteroids.cabal view
@@ -2,26 +2,31 @@ Synopsis: Try to avoid the asteroids with your space ship Description: The Asteroids game, using the wxHaskell GUI, as described in - "wxHaskell, A Portable and Concise GUI Library for Haskell" - http://legacy.cs.uu.nl/daan/download/papers/wxhaskell.pdf -Homepage: http://www.haskell.org/haskellwiki/wxAsteroids -Version: 1.0 + \"wxHaskell, A Portable and Concise GUI Library for Haskell\" + <http://legacy.cs.uu.nl/daan/download/papers/wxhaskell.pdf> +Homepage: https://wiki.haskell.org/WxAsteroids +Version: 1.1 License: BSD3 License-file: LICENSE.txt Author: Daan Leijen -Maintainer: Nobody +Maintainer: wxHaskell developer mailing list <wxhaskell-devel@lists.sourceforge.net> Stability: Stable -Cabal-Version: >= 1.2 +Cabal-Version: >= 1.6 Build-type: Simple Category: Game -Tested-with: GHC == 6.10.1 +Tested-with: GHC == 6.10.1, GHC == 7.6.3, GHC == 7.10.2 Extra-Source-Files: - README.txt + README.md + CHANGELOG.md Data-dir: . Data-files: rock.ico, burning.ico, ship.ico, explode.wav Executable wxAsteroids - Build-Depends: base, directory, random, wxcore, wx + Build-Depends: base < 5, directory, random, wxcore, wx > 0.90 Main-Is: Asteroids.lhs - if !os(windows) + if os(mingw32) GHC-Options: "-optl-mwindows" + +Source-repository head + type: git + location: https://github.com/wxHaskell/wxAsteroids