| |||||||||||
| |||||||||||
coroutine for game player movements
Threading and Coroutines. example from a Well House Consultants training course
More on Threading and Coroutines. [link]
Source code: charmoves Module: U114
--[[ Use of Lua coroutines to simulate the movement of a whole lot
of characters in parallel in a game. Jane has a 10 metre start, and 10 Johns are trying to catch her. Each John moves forward randomly between 0 and 1 metres per second. Jane moves foreard randomly between 0 and 0.9 metres. If Jane is not caught in 200 seconds she has won ]] target = 10 winner = 0 function movejohn() local covered = 0 while true do local stepsize = math.random() covered = covered + stepsize at = covered if at > target then winner = 1 else winner = 0 end coroutine.yield() end end function movejane() local covered = 10.0 while true do local stepsize = math.random()*0.9 covered = covered + stepsize at = covered target = at coroutine.yield() end end -- Create 10 different coroutine for 10 characters characters = {coroutine.create(movejane)} for k = 1, 10 do crh = coroutine.create(movejohn) characters[#characters+1] = crh end math.randomseed(os.time()) -- Run the movement routine for each in turn for seconds = 1, 200 do for person = 1,#characters do coroutine.resume(characters[person]) io.write (string.format("%6.2f ",at)) if winner == 1 then break end end if winner == 1 then io.write(" WONHER\n") break end io.write("\n") end --[[ Sample output: munchkin:laug11 grahamellis$ lua charmoves 10.63 0.58 0.94 0.74 0.37 0.94 0.15 0.26 0.85 0.96 0.07 11.46 0.79 1.77 1.31 1.05 1.69 0.95 0.38 1.49 1.94 0.09 11.91 1.40 1.95 1.97 1.27 2.05 1.64 0.46 2.18 2.64 0.22 12.34 1.88 2.05 2.65 1.37 2.35 2.48 0.49 2.33 3.43 1.20 12.41 2.04 2.09 3.41 1.57 3.00 3.08 1.33 2.71 4.40 1.67 12.43 2.45 2.47 4.18 1.85 3.55 3.19 1.89 3.11 5.03 2.12 12.80 2.86 2.47 5.13 2.36 3.72 3.34 1.98 4.07 5.12 3.12 13.66 3.09 2.51 5.91 2.74 3.76 3.99 2.70 4.21 5.66 3.96 13.80 3.27 3.18 6.85 3.15 4.44 4.28 3.33 4.40 5.72 4.84 13.81 3.91 3.49 6.86 4.02 4.80 5.10 3.90 5.27 6.28 5.39 13.97 4.52 4.12 7.46 4.87 5.58 5.57 4.58 5.39 7.24 5.69 14.18 5.09 4.44 8.11 5.42 6.16 5.86 5.18 6.10 8.17 6.55 14.98 5.67 4.73 8.82 5.82 6.72 6.83 5.70 6.95 8.38 7.02 15.85 6.55 5.48 9.81 6.42 6.95 7.08 6.58 7.89 8.58 7.29 15.85 7.50 6.08 10.62 7.33 7.31 7.76 7.55 8.25 8.98 7.97 16.68 8.43 6.61 10.87 8.10 8.16 8.47 8.07 9.13 9.36 8.20 16.80 9.08 7.20 11.81 8.80 8.28 8.78 8.46 10.10 9.52 8.68 17.35 9.92 7.97 12.55 9.51 8.91 9.71 9.43 10.69 9.90 9.63 15.04 10.00 8.17 13.11 10.09 9.27 10.55 9.57 11.56 9.96 10.49 18.24 10.17 8.94 13.46 10.21 9.32 10.79 9.74 11.85 10.43 11.01 18.30 11.02 9.75 14.36 10.27 9.66 11.08 9.86 12.27 11.14 11.24 18.63 11.31 9.94 15.24 10.55 10.43 11.34 10.00 12.86 12.07 11.54 18.65 11.85 10.32 15.41 11.44 10.54 11.65 10.56 13.23 12.27 11.73 19.50 12.17 10.98 16.35 11.45 11.20 12.19 10.62 13.95 12.80 12.16 20.30 12.61 11.43 17.16 12.10 11.74 13.10 11.58 14.84 13.21 12.40 20.30 13.31 11.94 15.09 12.31 12.26 13.27 11.80 15.40 13.94 12.66 21.03 14.14 12.79 18.24 12.63 12.66 13.38 12.57 15.76 14.91 13.15 21.51 14.32 12.86 19.19 13.24 12.75 13.72 13.57 16.43 15.40 13.25 21.68 14.62 13.03 19.81 13.28 13.13 14.69 13.88 16.45 16.15 13.84 22.18 15.35 13.06 20.23 13.88 13.50 14.92 14.06 16.63 16.91 14.25 22.82 15.42 13.40 20.27 14.05 14.02 15.15 14.33 17.19 17.08 15.17 23.14 15.82 13.84 20.69 14.97 14.98 15.39 14.46 17.96 15.04 16.13 23.32 15.97 14.28 21.25 15.26 15.17 15.86 15.13 18.36 15.07 16.35 23.80 16.62 14.53 21.36 15.78 15.66 16.45 16.11 18.45 18.10 16.53 23.96 15.04 15.51 22.29 16.10 15.94 16.55 17.04 19.33 18.70 17.47 24.44 18.09 16.09 22.74 16.54 16.21 17.49 15.09 20.25 18.82 18.32 24.87 18.52 16.69 23.02 17.40 16.32 18.05 17.62 20.90 18.87 19.14 25.17 19.12 17.48 23.94 17.40 16.85 18.58 18.35 21.73 19.46 19.31 25.47 19.90 18.20 24.37 18.21 17.67 19.05 18.36 21.90 19.55 20.14 26.22 20.67 19.15 24.70 19.03 18.40 19.85 18.89 22.32 19.62 20.76 26.41 20.98 19.67 25.39 19.32 19.34 20.63 19.65 22.92 20.10 21.76 26.92 21.06 20.56 25.52 20.06 19.38 21.41 20.15 23.46 20.69 22.61 27.65 21.74 21.42 25.94 20.29 19.56 22.08 20.70 23.97 21.06 23.56 27.99 22.55 21.59 26.21 21.05 20.46 22.89 21.46 24.77 22.04 24.50 28.68 23.00 22.03 26.99 21.53 20.73 23.57 21.78 25.52 22.51 25.12 29.01 23.54 22.28 27.52 22.33 20.79 24.32 22.75 25.83 23.31 25.88 29.21 24.15 22.48 27.97 22.40 20.82 24.57 22.75 26.62 24.25 26.44 30.07 24.70 22.96 28.11 23.29 21.60 24.78 23.13 26.87 24.67 26.54 30.45 24.90 23.07 28.19 23.95 21.90 25.64 23.20 27.72 25.33 27.50 31.31 24.97 23.33 28.77 24.51 22.43 26.29 23.60 27.76 25.90 27.72 31.57 25.86 23.66 29.50 25.46 22.58 26.37 23.81 28.26 26.46 28.51 31.95 26.28 24.09 30.01 25.76 22.85 26.88 24.62 29.23 26.68 29.41 32.14 26.70 24.44 30.78 26.39 23.07 27.70 25.29 30.00 27.19 30.24 32.51 27.22 25.09 31.59 27.21 23.48 28.12 26.24 30.50 27.71 30.58 32.94 27.45 25.18 32.53 27.98 23.81 28.59 26.55 31.31 28.32 31.11 33.17 28.45 25.33 33.52 WONHER munchkin:laug11 grahamellis$ ]] Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in Lua * Lua Programming * Lua Programming Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering Lua are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Threading and Coroutines." training module. You'll find a description of the topic and some
other closely related examples on the "Threading and Coroutines." module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |