diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..e461bea --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..35e5838 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/highscore.dat diff --git a/.project b/.project new file mode 100644 index 0000000..4404ffd --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + JavascriptFlappy + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..bb35fa0 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/LICENSE-CC-BY-4.0.txt b/LICENSE-CC-BY-4.0.txt new file mode 100644 index 0000000..16b34d7 --- /dev/null +++ b/LICENSE-CC-BY-4.0.txt @@ -0,0 +1,284 @@ +You can access a better formatted version of this license at +https://creativecommons.org/licenses/by/4.0/ . + +=========Creative Commons Attribution 4.0 International Public License========= + +By exercising the Licensed Rights (defined below), You accept and agree to be +bound by the terms and conditions of this Creative Commons Attribution 4.0 +International Public License ("Public License"). To the extent this Public +License may be interpreted as a contract, You are granted the Licensed Rights in +consideration of Your acceptance of these terms and conditions, and the +Licensor grants You such rights in consideration of benefits the Licensor +receives from making the Licensed Material available under these terms and +conditions. + +Section 1 – Definitions. + + a. Adapted Material means material subject to Copyright and Similar Rights + that is derived from or based upon the Licensed Material and in which the + Licensed Material is translated, altered, arranged, transformed, or + otherwise modified in a manner requiring permission under the Copyright + and Similar Rights held by the Licensor. For purposes of this Public + License, where the Licensed Material is a musical work, performance, or + sound recording, Adapted Material is always produced where the Licensed + Material is synched in timed relation with a moving image. + + b. Adapter's License means the license You apply to Your Copyright and + Similar Rights in Your contributions to Adapted Material in accordance + with the terms and conditions of this Public License. + + c. Copyright and Similar Rights means copyright and/or similar rights + closely related to copyright including, without limitation, performance, + broadcast, sound recording, and Sui Generis Database Rights, without + regard to how the rights are labeled or categorized. For purposes of this + Public License, the rights specified in Section 2(b)(1)-(2) are not + Copyright and Similar Rights. + + d. Effective Technological Measures means those measures that, in the + absence of proper authority, may not be circumvented under laws + fulfilling obligations under Article 11 of the WIPO Copyright Treaty + adopted on December 20, 1996, and/or similar international agreements. + + e. Exceptions and Limitations means fair use, fair dealing, and/or any other + exception or limitation to Copyright and Similar Rights that applies to + Your use of the Licensed Material. + + f. Licensed Material means the artistic or literary work, database, or other + material to which the Licensor applied this Public License. + + g. Licensed Rights means the rights granted to You subject to the terms and + conditions of this Public License, which are limited to all Copyright and + Similar Rights that apply to Your use of the Licensed Material and that + the Licensor has authority to license. + + h. Licensor means the individual(s) or entity(ies) granting rights under + this Public License. + + i. Share means to provide material to the public by any means or process + that requires permission under the Licensed Rights, such as reproduction, + public display, public performance, distribution, dissemination, + communication, or importation, and to make material available to the + public including in ways that members of the public may access the + material from a place and at a time individually chosen by them. + + j. Sui Generis Database Rights means rights other than copyright resulting + from Directive 96/9/EC of the European Parliament and of the Council of + 11 March 1996 on the legal protection of databases, as amended and/or + succeeded, as well as other essentially equivalent rights anywhere in the + world. + + k. You means the individual or entity exercising the Licensed Rights under + this Public License. Your has a corresponding meaning. + +Section 2 – Scope. + + a. License grant. + 1. Subject to the terms and conditions of this Public License, the + Licensor hereby grants You a worldwide, royalty-free, + non-sublicensable, non-exclusive, irrevocable license to exercise the + Licensed Rights in the Licensed Material to: + A. reproduce and Share the Licensed Material, in whole or in part; + and + + B. produce, reproduce, and Share Adapted Material. + + 2. Exceptions and Limitations. For the avoidance of doubt, where + Exceptions and Limitations apply to Your use, this Public License + does not apply, and You do not need to comply with its terms and + conditions. + + 3. Term. The term of this Public License is specified in Section 6(a). + + 4. Media and formats; technical modifications allowed. The Licensor + authorizes You to exercise the Licensed Rights in all media and + formats whether now known or hereafter created, and to make technical + modifications necessary to do so. The Licensor waives and/or agrees + not to assert any right or authority to forbid You from making + technical modifications necessary to exercise the Licensed Rights, + including technical modifications necessary to circumvent Effective + Technological Measures. For purposes of this Public License, simply + making modifications authorized by this Section 2(a)(4) never + produces Adapted Material. + + 5. Downstream recipients. + A. Offer from the Licensor – Licensed Material. Every recipient of + the Licensed Material automatically receives an offer from the + Licensor to exercise the Licensed Rights under the terms and + conditions of this Public License. + + B. No downstream restrictions. You may not offer or impose any + additional or different terms or conditions on, or apply any + Effective Technological Measures to, the Licensed Material if + doing so restricts exercise of the Licensed Rights by any + recipient of the Licensed Material. + + 6. No endorsement. Nothing in this Public License constitutes or may be + construed as permission to assert or imply that You are, or that Your + use of the Licensed Material is, connected with, or sponsored, + endorsed, or granted official status by, the Licensor or others + designated to receive attribution as provided in + Section 3(a)(1)(A)(i). + + b. Other rights. + 1. Moral rights, such as the right of integrity, are not licensed under + this Public License, nor are publicity, privacy, and/or other similar + personality rights; however, to the extent possible, the Licensor + waives and/or agrees not to assert any such rights held by the + Licensor to the limited extent necessary to allow You to exercise the + Licensed Rights, but not otherwise. + + 2. Patent and trademark rights are not licensed under this Public + License. + + 3. To the extent possible, the Licensor waives any right to collect + royalties from You for the exercise of the Licensed Rights, whether + directly or through a collecting society under any voluntary or + waivable statutory or compulsory licensing scheme. In all other cases + the Licensor expressly reserves any right to collect such royalties. + +Section 3 – License Conditions. + +Your exercise of the Licensed Rights is expressly made subject to the following +conditions. + + a. Attribution. + + 1. If You Share the Licensed Material (including in modified form), You + must: + A. retain the following if it is supplied by the Licensor with the + Licensed Material: + i. identification of the creator(s) of the Licensed Material and + any others designated to receive attribution, in any + reasonable manner requested by the Licensor (including by + pseudonym if designated); + + ii. a copyright notice; + + iii. a notice that refers to this Public License; + + iv. a notice that refers to the disclaimer of warranties; + + v. a URI or hyperlink to the Licensed Material to the extent + reasonably practicable; + + B. indicate if You modified the Licensed Material and retain an + indication of any previous modifications; and + + C. indicate the Licensed Material is licensed under this Public + License, and include the text of, or the URI or hyperlink to, + this Public License. + + 2. You may satisfy the conditions in Section 3(a)(1) in any reasonable + manner based on the medium, means, and context in which You Share the + Licensed Material. For example, it may be reasonable to satisfy the + conditions by providing a URI or hyperlink to a resource that + includes the required information. + + 3. If requested by the Licensor, You must remove any of the information + required by Section 3(a)(1)(A) to the extent reasonably practicable. + + 4. If You Share Adapted Material You produce, the Adapter's License You + apply must not prevent recipients of the Adapted Material from + complying with this Public License. + +Section 4 – Sui Generis Database Rights. + +Where the Licensed Rights include Sui Generis Database Rights that apply to Your +use of the Licensed Material: + + a. for the avoidance of doubt, Section 2(a)(1) grants You the right to + extract, reuse, reproduce, and Share all or a substantial portion of the + contents of the database; + + b. if You include all or a substantial portion of the database contents in a + database in which You have Sui Generis Database Rights, then the database + in which You have Sui Generis Database Rights (but not its individual + contents) is Adapted Material; and + + c. You must comply with the conditions in Section 3(a) if You Share all or a + substantial portion of the contents of the database. + +For the avoidance of doubt, this Section 4 supplements and does not replace Your +obligations under this Public License where the Licensed Rights include other +Copyright and Similar Rights. + +Section 5 – Disclaimer of Warranties and Limitation of Liability. + + a. Unless otherwise separately undertaken by the Licensor, to the extent + possible, the Licensor offers the Licensed Material as-is and + as-available, and makes no representations or warranties of any kind + concerning the Licensed Material, whether express, implied, statutory, or + other. This includes, without limitation, warranties of title, + merchantability, fitness for a particular purpose, non-infringement, + absence of latent or other defects, accuracy, or the presence or absence + of errors, whether or not known or discoverable. Where disclaimers of + warranties are not allowed in full or in part, this disclaimer may not + apply to You. + + b. To the extent possible, in no event will the Licensor be liable to You on + any legal theory (including, without limitation, negligence) or otherwise + for any direct, special, indirect, incidental, consequential, punitive, + exemplary, or other losses, costs, expenses, or damages arising out of + this Public License or use of the Licensed Material, even if the Licensor + has been advised of the possibility of such losses, costs, expenses, or + damages. Where a limitation of liability is not allowed in full or in + part, this limitation may not apply to You. + + c. The disclaimer of warranties and limitation of liability provided above + shall be interpreted in a manner that, to the extent possible, most + closely approximates an absolute disclaimer and waiver of all liability. + +Section 6 – Term and Termination. + + a. This Public License applies for the term of the Copyright and Similar + Rights licensed here. However, if You fail to comply with this Public + License, then Your rights under this Public License terminate + automatically. + + b. Where Your right to use the Licensed Material has terminated under + Section 6(a), it reinstates: + 1. automatically as of the date the violation is cured, provided it is + cured within 30 days of Your discovery of the violation; or + + 2. upon express reinstatement by the Licensor. + + c. For the avoidance of doubt, this Section 6(b) does not affect any right + the Licensor may have to seek remedies for Your violations of this Public + License. + + d. For the avoidance of doubt, the Licensor may also offer the Licensed + Material under separate terms or conditions or stop distributing the + Licensed Material at any time; however, doing so will not terminate this + Public License. + + e. Sections 1, 5, 6, 7, and 8 survive termination of this Public License. + +Section 7 – Other Terms and Conditions. + + a. The Licensor shall not be bound by any additional or different terms or + conditions communicated by You unless expressly agreed. + + b. Any arrangements, understandings, or agreements regarding the Licensed + Material not stated herein are separate from and independent of the terms + and conditions of this Public License. + +Section 8 – Interpretation. + + a. For the avoidance of doubt, this Public License does not, and shall not + be interpreted to, reduce, limit, restrict, or impose conditions on any + use of the Licensed Material that could lawfully be made without + permission under this Public License. + + b. To the extent possible, if any provision of this Public License is deemed + unenforceable, it shall be automatically reformed to the minimum extent + necessary to make it enforceable. If the provision cannot be reformed, it + shall be severed from this Public License without affecting the + enforceability of the remaining terms and conditions. + + c. No term or condition of this Public License will be waived and no failure + to comply consented to unless expressly agreed to by the Licensor. + + d. Nothing in this Public License constitutes or may be interpreted as a + limitation upon, or waiver of, any privileges and immunities that apply + to the Licensor or You, including from the legal processes of any + jurisdiction or authority. diff --git a/LICENSE-MIT.txt b/LICENSE-MIT.txt new file mode 100644 index 0000000..c342059 --- /dev/null +++ b/LICENSE-MIT.txt @@ -0,0 +1,19 @@ +Copyright (c) 2018 Uko Koknevics + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/LICENSE-OFL.txt b/LICENSE-OFL.txt new file mode 100644 index 0000000..70881e0 --- /dev/null +++ b/LICENSE-OFL.txt @@ -0,0 +1,93 @@ +Copyright 2012 The Press Start 2P Project Authors (cody@zone38.net), with Reserved Font Name "Press Start 2P". + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..22d7efc --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +## Backstory +One day I found out that Java has a built-in engine for JavaScript. I had to check it out. It was disappointing that it didn't recognize `const` and `let` keywords, at least in the Oracle 1.8 JDK. Well, as it turned out, it is using Edition 5.1 of ECMAScript so, I guess, that might be the reason. But what was interesting was the fact that I could access Java classes from JavaScript. So I had to test out if I could make something graphical with that. I could. So, well, a few hours later this was born. So I present to you *(drum-roll)* +# Flappy Bird written in Java and JavaScript +Yes, that's right. You may want to kill me now, but don't worry, so do I. +## How to play it? +So you want to play it huh. You can grab a .jar file from the GitHub releases and run it with `java -jar jarfile.jar` or compile it yourself with `javac JsMain.java` in the `src/` folder and then play it with `java JsMain`. You can also just open all of this in Eclipse IDE and compile & run from there. + +Oh yea I guess you need at least Java 1.6 for the JavaScript engine, but I don't know, maybe I'm using something else newer. Java 8 should work. +## No, I mean how do I control the bird? +* Space to flap +* Escape to exit the game (there aren't any confirmation dialogs here) +* R to restart the game (there aren't any confirmation dialogs here either) + +## I like the code, can I use it? +I know no one will say this, but I had to add this here. Yes, you can use the code, it's free. + +### License + +Copyright (C) 2018 Uko Koknevics + +This project is free software. There is no warranty; not even for merchantability or fitness for a particular purpose. + +The file `PressStart2P-Regular.ttf` located in `src/res/` folder is licensed under SIL Open Font License 1.1. See the file `LICENSE-OFL.txt` for more info. + +You may use, copy, modify and redistribute all the other files included in this distribution, individually or in aggregate, subject to the terms and conditions of the MIT license. See the file `LICENSE-MIT.txt` for more info. + +In addition, you may, at your own option, use, copy, modify and redistribute all images included in this distribution located in `src/res/` folder according to the terms and conditions of the Creative Commons Attribution 4.0 International Public License. See the file `LICENSE-CC-BY.txt` for more info. \ No newline at end of file diff --git a/src/JsMain.java b/src/JsMain.java new file mode 100644 index 0000000..611e23c --- /dev/null +++ b/src/JsMain.java @@ -0,0 +1,17 @@ +import java.io.InputStreamReader; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; + +public class JsMain { + public static void main(String[] args) { + try { + ScriptEngine engine = new ScriptEngineManager().getEngineByMimeType("application/javascript"); + ClassLoader cl = JsMain.class.getClassLoader(); + engine.eval(new InputStreamReader(cl.getResourceAsStream("res/script.js"))); + } catch (Exception e) { + e.printStackTrace(System.err); + System.exit(-1); + } + } +} diff --git a/src/res/LICENSE-OFL.txt b/src/res/LICENSE-OFL.txt new file mode 100644 index 0000000..70881e0 --- /dev/null +++ b/src/res/LICENSE-OFL.txt @@ -0,0 +1,93 @@ +Copyright 2012 The Press Start 2P Project Authors (cody@zone38.net), with Reserved Font Name "Press Start 2P". + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/src/res/PressStart2P-Regular.ttf b/src/res/PressStart2P-Regular.ttf new file mode 100644 index 0000000..39adf42 Binary files /dev/null and b/src/res/PressStart2P-Regular.ttf differ diff --git a/src/res/bg.png b/src/res/bg.png new file mode 100644 index 0000000..f4321cc Binary files /dev/null and b/src/res/bg.png differ diff --git a/src/res/bird.png b/src/res/bird.png new file mode 100644 index 0000000..cb9d63f Binary files /dev/null and b/src/res/bird.png differ diff --git a/src/res/pipe.png b/src/res/pipe.png new file mode 100644 index 0000000..36c02b9 Binary files /dev/null and b/src/res/pipe.png differ diff --git a/src/res/pipe_bottom.png b/src/res/pipe_bottom.png new file mode 100644 index 0000000..35d970b Binary files /dev/null and b/src/res/pipe_bottom.png differ diff --git a/src/res/pipe_top.png b/src/res/pipe_top.png new file mode 100644 index 0000000..cc500d9 Binary files /dev/null and b/src/res/pipe_top.png differ diff --git a/src/res/script.js b/src/res/script.js new file mode 100644 index 0000000..eae0f1f --- /dev/null +++ b/src/res/script.js @@ -0,0 +1,301 @@ +"use strict"; // idk if strict even works + +// classes +var Color = java.awt.Color; +var Dimension = java.awt.Dimension; +var Font = java.awt.Font; + +var KeyAdapter = java.awt.event.KeyAdapter; +var KeyEvent = java.awt.event.KeyEvent; + +var BufferedImage = java.awt.image.BufferedImage; + +var File = java.io.File; +var FileWriter = java.io.FileWriter; + +var ClassLoader = java.lang.ClassLoader; +var Integer = java.lang.Integer; +var Runnable = java.lang.Runnable; +var System = java.lang.System; +var Thread = java.lang.Thread; + +var Scanner = java.util.Scanner; + +var ImageIO = javax.imageio.ImageIO; + +var JFrame = javax.swing.JFrame; +var JPanel = javax.swing.JPanel; + +// implemented on use +var MyPanel = Java.extend(JPanel, Runnable); + +var MyKeyListener = Java.extend(KeyAdapter); + +// class definitions end here + +// images +var bg = ImageIO.read(ClassLoader.getSystemResourceAsStream("res/bg.png")); +var bird = ImageIO.read(ClassLoader.getSystemResourceAsStream("res/bird.png")); +var pipe = ImageIO.read(ClassLoader.getSystemResourceAsStream("res/pipe.png")); +var pipe_bottom = ImageIO.read(ClassLoader.getSystemResourceAsStream("res/pipe_bottom.png")); +var pipe_top = ImageIO.read(ClassLoader.getSystemResourceAsStream("res/pipe_top.png")); + +var font = Font.createFont(Font.TRUETYPE_FONT, ClassLoader.getSystemResourceAsStream("res/PressStart2P-Regular.ttf")); +font = font.deriveFont(10.0); + +// location of square, coordinates are from 0 to 1 +var x = 0.1; +var y = 0.5; + +// background offset +var bg_offset = 0; +var bg_speed = 1 / 50; + +// size of the square +var sq_w = 0.05; +var sq_h = 0.05; + +// size of a hole +var hole_w = sq_w * 3; +var hole_h = sq_h * 4.5; + +// distance between holes +var distance = 0.3 + hole_w; + +// an array of hole locations, there are 5 holes at any time +var holes = []; + +var speed; + +var gravity = 1 / 500000; + +// BufferedImage for double buffered rendering +var buf = null; +var buf_g = null; + +var gameover; + +var passed_first_hole; + +var score; +var old_highscore = 0; +var highscore = 0; + +var highscore_file = new File("highscore.dat"); +if (!highscore_file.exists()) { + highscore_file.createNewFile(); + var writer = new FileWriter(highscore_file); + writer.write("0"); + writer.close(); +} else { + var reader = new Scanner(highscore_file); + highscore = reader.nextInt(); + old_highscore = highscore; + reader.close(); +} + +function init() { + y = 0.5; + + for (var i = 0; i < 5; i++) { + holes[i] = { + y: Math.random() * (1 - hole_h), + x: 1 + i * distance + }; + } + + speed = { + x: 1 / 5000, + y: 0 + }; + + gameover = false; + + passed_first_hole = false; + score = 0; +} +init(); + +// delta - elapsed milliseconds since last update call +function update(delta) { + if (gameover && old_highscore != highscore) { + var writer = new FileWriter(highscore_file); + writer.write(Integer.toString(highscore)); + writer.close(); + old_highscore = highscore; + } + + y += speed.y; + if (y < 0) { + y = 0; + } + + if (y >= 1) { + gameover = true; + return; + } + + speed.y += gravity * delta; + + if (gameover) + return; + + bg_offset += delta * bg_speed; + + for (var i in holes) { + holes[i].x -= speed.x * delta; + } + + if (holes[0].x < - hole_w) { + passed_first_hole = false; + holes = holes.slice(1); + holes.push({ + x: holes[holes.length - 1].x + distance, + y: Math.random() * (1 - hole_h) + }); + } + + // could put holes.length instead of 2 but it works like this and is theoretically faster + for (var i = 0; i < 2; i++) { + if (x + sq_w > holes[i].x && x < holes[i].x + hole_w && + (y < holes[i].y || y + sq_h > holes[i].y + hole_h)) { + gameover = true; + return; + } + } + + if (!passed_first_hole && x > holes[0].x + hole_w) { + passed_first_hole = true; + score++; + if (score > highscore) { + highscore = score; + } + } +} + +// g - Graphics object for drawing +// width, height - size of panel +function draw(g, width, height) { + // if the window is made too small it will crash the BufferedImage constructor + if (width <= 0 || height <= 0) + return; + + // if buffer hasn't been initialized or the window has been resized, we make a new buffer + if (buf == null || buf.getWidth() != width || buf.getHeight() != height) { + buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + buf_g = buf.getGraphics(); + } + + // draw the bg + while (bg_offset > width) { + bg_offset -= width; + } + buf_g.drawImage(bg, -bg_offset, 0, width, height, null); + buf_g.drawImage(bg, width - bg_offset, 0, width, height, null); + + // draw the holes + for (var i in holes) { + buf_g.drawImage(pipe, holes[i].x * width, 0, hole_w * width, holes[i].y * height - pipe_top.getHeight(), null); + buf_g.drawImage(pipe_top, holes[i].x * width, holes[i].y * height - pipe_top.getHeight(), hole_w * width, pipe_top.getHeight(), null); + buf_g.drawImage(pipe, holes[i].x * width, (holes[i].y + hole_h) * height + pipe_bottom.getHeight(), hole_w * width, height, null); + buf_g.drawImage(pipe_bottom, holes[i].x * width, (holes[i].y + hole_h) * height, hole_w * width, pipe_bottom.getHeight(), null); + } + + // draw the square + width -= sq_w * width; + height -= sq_h * height; + buf_g.drawImage(bird, x * width - 5, y * height - 5, sq_w * width + 10, sq_h * height + 10, null); + + var score_str = "Score: " + score; + var hiscore_str = "Highscore: " + highscore; + + buf_g.setFont(font); + var metrics = buf_g.getFontMetrics(); + + buf_g.setColor(Color.darkGray); + buf_g.fillRect(8, 13 - metrics.getAscent(), metrics.stringWidth(score_str) + 4, metrics.getHeight() + 4); + buf_g.fillRect(8, 28 - metrics.getAscent(), metrics.stringWidth(hiscore_str) + 4, metrics.getHeight() + 4); + + buf_g.setColor(Color.white); + buf_g.drawString(score_str, 10, 15); + buf_g.drawString(hiscore_str, 10, 30); + + if (gameover) { + var over_str = "Game Over"; + var press_str = "Press R to restart"; + + var over_x = (width - metrics.stringWidth(over_str)) / 2; + var press_x = (width - metrics.stringWidth(press_str)) / 2; + + buf_g.setColor(Color.darkGray); + buf_g.fillRect(over_x - 2, height / 2 - 12 - metrics.getAscent(), metrics.stringWidth(over_str) + 4, metrics.getHeight() + 4); + buf_g.fillRect(press_x - 2, height / 2 + 8 - metrics.getAscent(), metrics.stringWidth(press_str) + 4, metrics.getHeight() + 4) + + buf_g.setColor(Color.white); + buf_g.drawString("Game Over", over_x, height / 2 - 10); + buf_g.drawString("Press R to restart", press_x, height / 2 + 10); + } + + // draw the buffer + g.drawImage(buf, 0, 0, null); +} + + +// make the frame +var frame = new JFrame(); +frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); +frame.setTitle("Hello World!"); + +// class is implemented here bcs i did it with the panel class, too +var listener = new MyKeyListener({ + keyPressed: function(e) { + if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { + System.exit(0); + } else if (e.getKeyCode() == KeyEvent.VK_SPACE) { + if (!gameover) { + speed.y = -1 / 1000; + } + } else if (e.getKeyCode() == KeyEvent.VK_R) { + init(); + } + } +}); + +frame.addKeyListener(listener); + +// class is implemented here bcs idk how else to do Java.super() +var panel = new MyPanel({ + // paint() is empty bcs we don't want to do the normal rendering + paint: function(g) {}, + run: function() { + // this will be used to store the panel size + var size = new Dimension(); + + // for timing + var oldTime = System.currentTimeMillis(); + var time = oldTime; + while (true) { + oldTime = time; + time = System.currentTimeMillis(); + var delta = time - oldTime; + var graphics = Java.super(panel).getGraphics(); + // graphics might be null if the panel isn't yet visible + if (graphics != null) { + size = Java.super(panel).getSize(size); + update(delta); + draw(graphics, size.width, size.height); + } + // sleep a bit + Thread.sleep(1); + } + } +}); +frame.add(panel); + +// start the thread before making the frame visible so that there isn't an empty frame +new Thread(panel).start(); + +frame.setSize(640, 480); +// this centers the frame +frame.setLocationRelativeTo(null); +frame.setVisible(true); \ No newline at end of file