commit 9271e21bfb3879e7bfbf35c933e462ea473d5c31 Author: franvertedorhd Date: Sun Sep 22 09:32:09 2024 +0800 Initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e62ec04 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ +GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + 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 3 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, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/images/explosion_1.png b/images/explosion_1.png new file mode 100644 index 0000000..96cb745 Binary files /dev/null and b/images/explosion_1.png differ diff --git a/images/explosion_10.png b/images/explosion_10.png new file mode 100644 index 0000000..366d5a5 Binary files /dev/null and b/images/explosion_10.png differ diff --git a/images/explosion_2.png b/images/explosion_2.png new file mode 100644 index 0000000..faaab7f Binary files /dev/null and b/images/explosion_2.png differ diff --git a/images/explosion_3.png b/images/explosion_3.png new file mode 100644 index 0000000..8c8792b Binary files /dev/null and b/images/explosion_3.png differ diff --git a/images/explosion_4.png b/images/explosion_4.png new file mode 100644 index 0000000..e746841 Binary files /dev/null and b/images/explosion_4.png differ diff --git a/images/explosion_5.png b/images/explosion_5.png new file mode 100644 index 0000000..c408b50 Binary files /dev/null and b/images/explosion_5.png differ diff --git a/images/explosion_6.png b/images/explosion_6.png new file mode 100644 index 0000000..0070fd3 Binary files /dev/null and b/images/explosion_6.png differ diff --git a/images/explosion_7.png b/images/explosion_7.png new file mode 100644 index 0000000..1f1b120 Binary files /dev/null and b/images/explosion_7.png differ diff --git a/images/explosion_8.png b/images/explosion_8.png new file mode 100644 index 0000000..02daca0 Binary files /dev/null and b/images/explosion_8.png differ diff --git a/images/explosion_9.png b/images/explosion_9.png new file mode 100644 index 0000000..4269364 Binary files /dev/null and b/images/explosion_9.png differ diff --git a/images/health0.png b/images/health0.png new file mode 100644 index 0000000..8e917d5 Binary files /dev/null and b/images/health0.png differ diff --git a/images/health1.png b/images/health1.png new file mode 100644 index 0000000..176c46d Binary files /dev/null and b/images/health1.png differ diff --git a/images/health10.png b/images/health10.png new file mode 100644 index 0000000..a110888 Binary files /dev/null and b/images/health10.png differ diff --git a/images/health11.png b/images/health11.png new file mode 100644 index 0000000..f0f9aef Binary files /dev/null and b/images/health11.png differ diff --git a/images/health12.png b/images/health12.png new file mode 100644 index 0000000..e5170c9 Binary files /dev/null and b/images/health12.png differ diff --git a/images/health13.png b/images/health13.png new file mode 100644 index 0000000..e8a6a40 Binary files /dev/null and b/images/health13.png differ diff --git a/images/health14.png b/images/health14.png new file mode 100644 index 0000000..a106c84 Binary files /dev/null and b/images/health14.png differ diff --git a/images/health15.png b/images/health15.png new file mode 100644 index 0000000..b239074 Binary files /dev/null and b/images/health15.png differ diff --git a/images/health16.png b/images/health16.png new file mode 100644 index 0000000..6cc6814 Binary files /dev/null and b/images/health16.png differ diff --git a/images/health17.png b/images/health17.png new file mode 100644 index 0000000..9cb2ab8 Binary files /dev/null and b/images/health17.png differ diff --git a/images/health18.png b/images/health18.png new file mode 100644 index 0000000..a656ff8 Binary files /dev/null and b/images/health18.png differ diff --git a/images/health19.png b/images/health19.png new file mode 100644 index 0000000..6ab591c Binary files /dev/null and b/images/health19.png differ diff --git a/images/health2.png b/images/health2.png new file mode 100644 index 0000000..c0a2493 Binary files /dev/null and b/images/health2.png differ diff --git a/images/health3.png b/images/health3.png new file mode 100644 index 0000000..0f93b61 Binary files /dev/null and b/images/health3.png differ diff --git a/images/health4.png b/images/health4.png new file mode 100644 index 0000000..6cd43b3 Binary files /dev/null and b/images/health4.png differ diff --git a/images/health5.png b/images/health5.png new file mode 100644 index 0000000..9621f6f Binary files /dev/null and b/images/health5.png differ diff --git a/images/health6.png b/images/health6.png new file mode 100644 index 0000000..69d7de1 Binary files /dev/null and b/images/health6.png differ diff --git a/images/health7.png b/images/health7.png new file mode 100644 index 0000000..6369af6 Binary files /dev/null and b/images/health7.png differ diff --git a/images/health8.png b/images/health8.png new file mode 100644 index 0000000..7323509 Binary files /dev/null and b/images/health8.png differ diff --git a/images/health9.png b/images/health9.png new file mode 100644 index 0000000..ccb1e64 Binary files /dev/null and b/images/health9.png differ diff --git a/images/laser.png b/images/laser.png new file mode 100644 index 0000000..7d711ce Binary files /dev/null and b/images/laser.png differ diff --git a/images/meteorbrown1.png b/images/meteorbrown1.png new file mode 100644 index 0000000..042604f Binary files /dev/null and b/images/meteorbrown1.png differ diff --git a/images/meteorbrown2.png b/images/meteorbrown2.png new file mode 100644 index 0000000..0ed6da2 Binary files /dev/null and b/images/meteorbrown2.png differ diff --git a/images/meteorbrown3.png b/images/meteorbrown3.png new file mode 100644 index 0000000..d6f3c60 Binary files /dev/null and b/images/meteorbrown3.png differ diff --git a/images/meteorbrown4.png b/images/meteorbrown4.png new file mode 100644 index 0000000..7b56766 Binary files /dev/null and b/images/meteorbrown4.png differ diff --git a/images/meteorgrey1.png b/images/meteorgrey1.png new file mode 100644 index 0000000..9f99ff1 Binary files /dev/null and b/images/meteorgrey1.png differ diff --git a/images/meteorgrey2.png b/images/meteorgrey2.png new file mode 100644 index 0000000..a2818cd Binary files /dev/null and b/images/meteorgrey2.png differ diff --git a/images/meteorgrey3.png b/images/meteorgrey3.png new file mode 100644 index 0000000..841d5dd Binary files /dev/null and b/images/meteorgrey3.png differ diff --git a/images/meteorgrey4.png b/images/meteorgrey4.png new file mode 100644 index 0000000..bebd158 Binary files /dev/null and b/images/meteorgrey4.png differ diff --git a/images/playership.png b/images/playership.png new file mode 100644 index 0000000..e277114 Binary files /dev/null and b/images/playership.png differ diff --git a/images/playership_damage.png b/images/playership_damage.png new file mode 100644 index 0000000..109ff48 Binary files /dev/null and b/images/playership_damage.png differ diff --git a/images/powerup.png b/images/powerup.png new file mode 100644 index 0000000..82b21fc Binary files /dev/null and b/images/powerup.png differ diff --git a/images/spacebg.jpg b/images/spacebg.jpg new file mode 100644 index 0000000..3d59d5b Binary files /dev/null and b/images/spacebg.jpg differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..bba51f1 --- /dev/null +++ b/main.py @@ -0,0 +1,215 @@ +""" +TO DO: + - Splash screen + - Play again? +""" + +import random +import time +import pgzrun +from pgzhelper import * + +WIDTH = 800 +HEIGHT = 600 + +# globals +score = 0 +health = 19 +health_holdoff = 0 +powerup_time = 0 +game_over_played = False + +lasers = [] +laser_holdoff = 0 +laser_hit_idx = 0 + +music.play('music1') + + +# local helper functions +def rotate(actor): + if actor.angle == 360: + actor.angle = 0 + else: + actor.angle += 1 + + +def relocate(actor, scale=1): + # Avoid ship + x = ship.x + y = ship.y + while (x == ship.x) and (y == ship.y): + x = random.randint(20, 780) + y = random.randint(20, 580) + actor.x = x + actor.y = y + actor.scale = scale + actor.direction = random.randint(0, 359) + + +background = Actor('spacebg') + +health_bar = Actor(f'health{health}') +health_bar.scale = 0.8 +health_bar.x = 140 +health_bar.y = 30 + +ship = Actor('playership') +ship.images = [ship.image, 'playership_damage'] +ship.fps = 10 +ship.y = 550 +ship.x = 400 + +meteors = [Actor('meteorbrown1'), Actor('meteorbrown2'), Actor('meteorbrown3'), + Actor('meteorbrown4'), Actor('meteorgrey1'), Actor('meteorgrey2'), + Actor('meteorgrey3'), Actor('meteorgrey4')] + +for meteor in meteors: + relocate(meteor, round(random.uniform(0.5, 1.5), 1)) + +explosions = [] +powerups = [] + + +def on_mouse_move(pos, rel, buttons): + ship.x = pos[0] + ship.y = pos[1] + + +def on_mouse_down(pos, button): + global lasers, laser_holdoff + if button == 1: + if laser_holdoff == 0: + sounds.laser.play() + laser = Actor('laser') + laser.angle = 90 + laser.x = ship.x + laser.y = ship.y - 30 + lasers.append(laser) + laser_holdoff = 1 + else: + laser_holdoff -= 1 + +def update(): + global score, health, health_holdoff, laser_hit_idx, powerup_time + + health_bar.image = f'health{health}' + health_bar.scale = 0.8 + health_bar.x = 140 + health_bar.y = 30 + + # meteors + for meteor in meteors: + rotate(meteor) + meteor.move_in_direction(round(random.uniform(0.5, 1.5), 1)) # meteor speed + + # meteor collisions + if meteor.collidelist(lasers) != -1: + try: + del lasers[laser_hit_idx] + except IndexError: + pass + finally: + explotion = Actor('explosion_1') + explotion.images = [explotion.image, 'explosion_2', 'explosion_3', + 'explosion_4', 'explosion_5', 'explosion_6', 'explosion_7', + 'explosion_8', 'explosion_9', 'explosion_10'] + explotion.x = meteor.x + explotion.y = meteor.y + explotion.scale = 0.5 + explotion.fps = 10 + explosions.append(explotion) + sounds.explosion.play() + score += 1 + relocate(meteor, round(random.uniform(0.5, 1.5), 1)) + + if (meteor.x > 800) or (meteor.y > 600) or (meteor.x < 0) or (meteor.y < 0): + relocate(meteor, round(random.uniform(0.5, 1.5), 1)) + + for explotion in explosions: + if explotion.animate() >= 9: + explosions.remove(explotion) + + # ship collides with meteors + if ship.collidelist(meteors) != -1: + ship.animate() + if health_holdoff == 0: + if health > 0: + health -= 1 + sounds.loose.play() + health_holdoff = 50 + else: + health_holdoff -= 1 + else: + ship.image = 'playership' + + # laser collisions + for idx, laser in enumerate(lasers): + if laser.y > 595: + lasers.remove(laser) + elif laser.collidelist(meteors) != -1: + laser_hit_idx = idx + else: + laser.y -= 5 + + # Power Up + if health < 19 and powerup_time == 0: + # TO DO: fix random time - use a counter with random values: powerup_time + powerup = Actor('powerup') + relocate(powerup) + powerups.append(powerup) + powerup_time = random.randint(100, 300) + + if powerup_time > 0: + powerup_time -= 1 + + for powerup in powerups: + powerup.move_in_direction(round(random.uniform(0.5, 1.5), 1)) # powerup speed + # detect collision with ship + if powerup.colliderect(ship): + powerups.remove(powerup) + sounds.powerup.set_volume(4) + sounds.powerup.play() + health += 1 + if health > 19: + health = 19 + # remove if outside the screen + elif (powerup.x > 800) or (powerup.y > 600) or (powerup.x < 0) or (powerup.y < 0): + powerups.remove(powerup) + +def draw(): + global game_over_played + screen.fill((0, 0, 0)) + background.draw() + health_bar.draw() + if health > 0: + screen.draw.text(f'Score: {score}', (330, 18), color=(255, 255, 255), fontsize=40) + + for meteor in meteors: + meteor.draw() + + for explotion in explosions: + explotion.draw() + + for powerup in powerups: + powerup.draw() + + ship.draw() + + for laser in lasers: + laser.draw() + else: + powerups.clear() + meteors.clear() + explosions.clear() + lasers.clear() + screen.draw.text(f'Score: {score}', (250, 300), color=(245, 243, 127), fontsize=100) + screen.draw.text('Game Over', (220, 450), color=(255, 255, 255), fontsize=100) + music.stop() + if not game_over_played: + sounds.game_over.play() + game_over_played = True + + +# Main loop +pgzrun.go() \ No newline at end of file diff --git a/music/music1.mp3 b/music/music1.mp3 new file mode 100644 index 0000000..d7966be Binary files /dev/null and b/music/music1.mp3 differ diff --git a/pgzhelper.py b/pgzhelper.py new file mode 100644 index 0000000..b8f5718 --- /dev/null +++ b/pgzhelper.py @@ -0,0 +1,1392 @@ +# from __future__ import annotations +import math +import pygame +from pgzero.actor import Actor, POS_TOPLEFT, ANCHOR_CENTER, transform_anchor +from pgzero import game, loaders +import sys +import time +from typing import Sequence, Tuple, Union +from pygame import Vector2 + +_Coordinate = Union[Tuple[float, float], Sequence[float], Vector2] +_fullscreen = False + +def set_fullscreen(): + global _fullscreen + mod = sys.modules['__main__'] + mod.screen.surface = pygame.display.set_mode((mod.WIDTH, mod.HEIGHT), pygame.FULLSCREEN) + _fullscreen = True + +def set_windowed(): + global _fullscreen + mod = sys.modules['__main__'] + mod.screen.surface = pygame.display.set_mode((mod.WIDTH, mod.HEIGHT)) + _fullscreen = False + +def toggle_fullscreen(): + if _fullscreen: + set_windowed() + else: + set_fullscreen() + +def hide_mouse(): + pygame.mouse.set_visible(False) + +def show_mouse(): + pygame.mouse.set_visible(True) + +def distance_to(from_x, from_y, to_x, to_y): + dx = to_x - from_x + dy = to_y - from_y + return math.sqrt(dx**2 + dy**2) + +def distance_to_squared(from_x, from_y, to_x, to_y): + dx = to_x - from_x + dy = to_y - from_y + return dx**2 + dy**2 + +def direction_to(from_x, from_y, to_x, to_y): + dx = to_x - from_x + dy = from_y - to_y + + angle = math.degrees(math.atan2(dy, dx)) + if angle > 0: + return angle + + return 360 + angle + +def get_move(direction, distance): + angle = math.radians(direction) + dx = distance * math.cos(angle) + dy = -distance * math.sin(angle) + return (dx, dy) + +def move(x, y, direction, distance): + dx, dy = get_move(direction, distance) + return (x + dx, y + dy) + +class Collide(): + @staticmethod + def line_line(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2): + l1x2_l1x1 = l1x2-l1x1 + l1y2_l1y1 = l1y2-l1y1 + + determinant = (l2y2-l2y1)*l1x2_l1x1 - (l2x2-l2x1)*l1y2_l1y1 + + # Simplify: Parallel lines are never considered to be intersecting + if determinant == 0: + return False + + uA = ((l2x2-l2x1)*(l1y1-l2y1) - (l2y2-l2y1)*(l1x1-l2x1)) / determinant + if uA < 0 or uA > 1: + return False + + uB = (l1x2_l1x1*(l1y1-l2y1) - l1y2_l1y1*(l1x1-l2x1)) / determinant + if uB < 0 or uB > 1: + return False + + return True + + @staticmethod + def line_lines(l1x1, l1y1, l1x2, l1y2, l2): + l1x2_l1x1 = l1x2-l1x1 + l1y2_l1y1 = l1y2-l1y1 + + i = 0 + for l in l2: + determinant = (l[3]-l[1])*l1x2_l1x1 - (l[2]-l[0])*l1y2_l1y1 + + # Simplify: Parallel lines are never considered to be intersecting + if determinant == 0: + i += 1 + continue + + uA = ((l[2]-l[0])*(l1y1-l[1]) - (l[3]-l[1])*(l1x1-l[0])) / determinant + uB = (l1x2_l1x1*(l1y1-l[1]) - l1y2_l1y1*(l1x1-l[0])) / determinant + if 0 <= uA <= 1 and 0 <= uB <= 1: + return i + + i += 1 + + return -1 + + @staticmethod + def line_line_XY(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2): + determinant = (l2y2-l2y1)*(l1x2-l1x1) - (l2x2-l2x1)*(l1y2-l1y1) + + # Simplify: Parallel lines are never considered to be intersecting + if determinant == 0: + return (None, None) + + uA = ((l2x2-l2x1)*(l1y1-l2y1) - (l2y2-l2y1)*(l1x1-l2x1)) / determinant + uB = ((l1x2-l1x1)*(l1y1-l2y1) - (l1y2-l1y1)*(l1x1-l2x1)) / determinant + + if 0 <= uA <= 1 and 0 <= uB <= 1: + ix = l1x1 + uA * (l1x2 - l1x1) + iy = l1y1 + uA * (l1y2 - l1y1) + return (ix, iy) + + return (None, None) + + @staticmethod + def line_line_dist(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2): + ix, iy = Collide.line_line_XY(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2) + if ix is not None: + return distance_to(l1x1, l1y1, ix, iy) + return None + + @staticmethod + def line_line_dist_squared(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2): + ix, iy = Collide.line_line_XY(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2) + if ix is not None: + return distance_to_squared(l1x1, l1y1, ix, iy) + return None + + @staticmethod + def line_circle(x1, y1, x2, y2, cx, cy, radius): + r_sq = radius ** 2 + + dist_sq = (x1 - cx) ** 2 + (y1 - cy) ** 2 + if dist_sq <= r_sq: + return True + + dist_sq = (x2 - cx) ** 2 + (y2 - cy) ** 2 + if dist_sq <= r_sq: + return True + + dx = x2 - x1 + dy = y2 - y1 + l_sq = dx ** 2 + dy ** 2 + dot = (((cx - x1) * dx) + ((cy - y1) * dy)) / l_sq + + ix = x1 + dot * dx + if (dx!=0) and (ix < x1) == (ix < x2): + return False + + iy = y1 + dot * dy + if (dy!=0) and (iy < y1) == (iy < y2): + return False + + dist_sq = (ix - cx) ** 2 + (iy - cy) ** 2 + if dist_sq <= r_sq: + return True + + return False + + @staticmethod + def line_circle_XY(x1, y1, x2, y2, cx, cy, radius): + if Collide.circle_point(cx, cy, radius, x1, y1): + return (x1, y1) + + x1 -= cx + y1 -= cy + x2 -= cx + y2 -= cy + + if x2 < x1: + x_min, x_max = x2, x1 + else: + x_min, x_max = x1, x2 + + if y2 < y1: + y_min, y_max = y2, y1 + else: + y_min, y_max = y1, y2 + + # Coefficients of circle + c_r2 = radius ** 2 + + # Simplify if dx == 0: Vertical line + dx = x2 - x1 + if dx == 0: + d = c_r2 - x1**2 + if d < 0: + return (None, None) + elif d == 0: + i = 0 + else: + i = math.sqrt(d) + + iy = None + if y_min <= i <= y_max: + iy = i + + if y_min <= -i <= y_max: + if iy is None or abs(i - y1) > abs(-i - y1): + iy = -i + + if iy: + return (x1 + cx, iy + cy) + return (None, None) + + # Gradient of line + l_m = (y2 - y1) / dx + + # Simplify if l_m == 0: Horizontal line + if l_m == 0: + d = c_r2 - y1**2 + if d < 0: + return (None, None) + elif d == 0: + i = 0 + else: + i = math.sqrt(d) + ix = None + if x_min <= i <= x_max: + ix = i + + if x_min <= -i <= x_max: + if ix is None or abs(i - x1) > abs(-i - x1): + ix = -i + + if ix: + return (ix + cx, y1 + cy) + return (None, None) + + # y intercept + l_c = y1 - l_m * x1 + + # Coefficients of quadratic + a = 1 + l_m**2 + b = 2 * l_c * l_m + c = l_c**2 - c_r2 + + # Calculate discriminant and solve quadratic + discriminant = b**2 - 4 * a * c + if discriminant < 0: + return (None, None) + + if discriminant == 0: + d_root = 0 + else: + d_root = math.sqrt(discriminant) + + ix = None + i1 = (-b + d_root) / (2 * a) + if x_min <= i1 <= x_max: + ix = i1 + + + i2 = (-b - d_root) / (2 * a) + if x_min <= i2 <= x_max: + if ix is None or abs(i1 - x1) > abs(i2 - x1): + ix = i2 + + if ix: + return (ix + cx, l_m * ix + l_c + cy) + + return (None, None) + + @staticmethod + def line_circle_dist(x1, y1, x2, y2, cx, cy, radius): + ix, iy = Collide.line_circle_XY(x1, y1, x2, y2, cx, cy, radius) + if ix is not None: + return distance_to(x1, y1, ix, iy) + return None + + @staticmethod + def line_circle_dist_squared(x1, y1, x2, y2, cx, cy, radius): + ix, iy = Collide.line_circle_XY(x1, y1, x2, y2, cx, cy, radius) + if ix is not None: + return distance_to_squared(x1, y1, ix, iy) + return None + + @staticmethod + def line_rect(x1, y1, x2, y2, rx, ry, w, h): + if Collide.rect_points(rx, ry, w, h, [(x1, y1), (x2, y2)]) != -1: + return True + + half_w = w / 2 + half_h = h / 2 + rect_lines = [ + [rx - half_w, ry - half_h, rx - half_w, ry + half_h], + [rx - half_w, ry - half_h, rx + half_w, ry - half_h], + [rx + half_w, ry + half_h, rx - half_w, ry + half_h], + [rx + half_w, ry + half_h, rx + half_w, ry - half_h], + ] + if Collide.line_lines(x1, y1, x2, y2, rect_lines) != -1: + return True + + return False + + @staticmethod + def line_rect_XY(x1, y1, x2, y2, rx, ry, w, h): + if Collide.rect_point(rx, ry, w, h, x1, y1): + return (x1, y1) + + half_w = w / 2 + half_h = h / 2 + rect_lines = [ + [rx - half_w, ry - half_h, rx - half_w, ry + half_h], + [rx - half_w, ry - half_h, rx + half_w, ry - half_h], + [rx + half_w, ry + half_h, rx - half_w, ry + half_h], + [rx + half_w, ry + half_h, rx + half_w, ry - half_h], + ] + XYs = [] + for l in rect_lines: + ix, iy = Collide.line_line_XY(x1, y1, x2, y2, l[0], l[1], l[2], l[3]) + if ix is not None: + XYs.append((ix, iy)) + + length = len(XYs) + if length == 0: + return (None, None) + elif length == 1: + return XYs[0] + + ix, iy = XYs[0] + shortest_dist = (ix - x1) ** 2 + (iy - y1) ** 2 + for XY in XYs: + dist = (XY[0] - x1) ** 2 + (XY[1] - y1) ** 2 + if dist < shortest_dist: + ix, iy = XY + shortest_dist = dist + + return (ix, iy) + + @staticmethod + def line_rect_dist(x1, y1, x2, y2, rx, ry, w, h): + ix, iy = Collide.line_rect_XY(x1, y1, x2, y2, rx, ry, w, h) + if ix is not None: + return distance_to(x1, y1, ix, iy) + return None + + @staticmethod + def line_rect_dist_squared(x1, y1, x2, y2, rx, ry, w, h): + ix, iy = Collide.line_rect_XY(x1, y1, x2, y2, rx, ry, w, h) + if ix is not None: + return distance_to_squared(x1, y1, ix, iy) + return None + + @staticmethod + def line_obb_XY(x1, y1, x2, y2, ox, oy, w, h, angle): + half_width = w / 2 + half_height = h / 2 + r_angle = math.radians(angle) + costheta = math.cos(r_angle) + sintheta = math.sin(r_angle) + + tx = x1 - ox + ty = y1 - oy + rx = tx * costheta - ty * sintheta + ry = ty * costheta + tx * sintheta + + if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height: + return (x1, y1) + + wc = half_width * costheta + hs = half_height * sintheta + hc = half_height * costheta + ws = half_width * sintheta + p = [ + [ox + wc + hs, oy + hc - ws], + [ox - wc + hs, oy + hc + ws], + [ox + wc - hs, oy - hc - ws], + [ox - wc - hs, oy - hc + ws], + ] + obb_lines = [ + [p[0][0], p[0][1], p[1][0], p[1][1]], + [p[1][0], p[1][1], p[3][0], p[3][1]], + [p[3][0], p[3][1], p[2][0], p[2][1]], + [p[2][0], p[2][1], p[0][0], p[0][1]] + ] + + XYs = [] + for l in obb_lines: + ix, iy = Collide.line_line_XY(x1, y1, x2, y2, l[0], l[1], l[2], l[3]) + if ix is not None: + XYs.append((ix, iy)) + + length = len(XYs) + if length == 0: + return (None, None) + elif length == 1: + return XYs[0] + + ix, iy = XYs[0] + shortest_dist = (ix - x1) ** 2 + (iy - y1) ** 2 + for XY in XYs: + dist = (XY[0] - x1) ** 2 + (XY[1] - y1) ** 2 + if dist < shortest_dist: + ix, iy = XY + shortest_dist = dist + + return (ix, iy) + + @staticmethod + def line_obb_dist(x1, y1, x2, y2, ox, oy, w, h, angle): + ix, iy = Collide.line_obb_XY(x1, y1, x2, y2, ox, oy, w, h, angle) + if ix is not None: + return distance_to(x1, y1, ix, iy) + return None + + @staticmethod + def line_obb_dist_squared(x1, y1, x2, y2, ox, oy, w, h, angle): + ix, iy = Collide.obb_line_XY(x1, y1, x2, y2, ox, oy, w, h, angle) + if ix is not None: + return distance_to_squared(x1, y1, ix, iy) + return None + + @staticmethod + def circle_point(x1, y1, radius, x2, y2): + rSquare = radius ** 2 + dSquare = (x2 - x1)**2 + (y2 - y1)**2 + + if dSquare < rSquare: + return True + + return False + + @staticmethod + def circle_points(x, y, radius, points): + rSquare = radius ** 2 + + i = 0 + for point in points: + try: + px = point[0] + py = point[1] + except (KeyError, TypeError): + px = point.x + py = point.y + dSquare = (px - x)**2 + (py - y)**2 + + if dSquare < rSquare: + return i + i += 1 + + return -1 + + @staticmethod + def circle_line(cx, cy, radius, x1, y1, x2, y2): + return Collide.line_circle(x1, y1, x2, y2, cx, cy, radius) + + @staticmethod + def circle_circle(x1, y1, r1, x2, y2, r2): + rSquare = (r1 + r2) ** 2 + dSquare = (x2 - x1)**2 + (y2 - y1)**2 + + if dSquare < rSquare: + return True + + return False + + @staticmethod + def circle_rect(cx, cy, cr, rx, ry, rw, rh): + h_w = rw / 2 + h_h = rh / 2 + rect_l = rx - h_w + rect_t = ry - h_h + + if cx < rect_l: + dx2 = (cx - rect_l) ** 2 + elif cx > (rect_l + rw): + dx2 = (cx - rect_l - rw) ** 2 + else: + dx2 = 0 + + if cy < rect_t: + dy2 = (cy - rect_t) ** 2 + elif cy > (rect_t + rh): + dy2 = (cy - rect_t - rh) ** 2 + else: + dy2 = 0 + + dist2 = dx2 + dy2 + + if dist2 < (cr ** 2): + return True + + return False + + @staticmethod + def rect_point(x, y, w, h, px, py): + half_w = w / 2 + half_h = h / 2 + + if ( + px < x - half_w + or px > x + half_w + or py < y - half_h + or py > y + half_h + ): + return False + + return True + + @staticmethod + def rect_points(x, y, w, h, points): + half_w = w / 2 + half_h = h / 2 + min_x = x - half_w + max_x = x + half_w + min_y = y - half_h + max_y = y + half_h + + i = 0 + for point in points: + try: + px = point[0] + py = point[1] + except (KeyError, TypeError): + px = point.x + py = point.y + if ( + px >= min_x + and px <= max_x + and py >= min_y + and py <= max_y + ): + return i + i += 1 + + return -1 + + @staticmethod + def rect_line(x, y, w, h, lx1, ly1, lx2, ly2): + return Collide.line_rect(lx1, ly1, lx2, ly2, x, y, w, h) + + @staticmethod + def rect_circle(rx, ry, rw, rh, cx, cy, cr): + return Collide.circle_rect(cx, cy, cr, rx, ry, rw, rh) + + @staticmethod + def rect_rect(x1, y1, w1, h1, x2, y2, w2, h2): + h_w1 = w1 / 2 + h_h1 = h1 / 2 + h_w2 = w2 / 2 + h_h2 = h2 / 2 + + if ( + x2 - h_w2 > x1 + h_w1 + or x2 + h_w2 < x1 - h_w1 + or y2 - h_h2 > y1 + h_h1 + or y2 + h_h2 < y1 - h_h1 + ): + return False + + return True + + @staticmethod + def obb_point(x, y, w, h, angle, px, py): + half_width = w / 2 + half_height = h / 2 + b_radius_sq = half_width ** 2 + half_height ** 2 + tx = px - x + ty = py - y + + if tx ** 2 + ty ** 2 > b_radius_sq: + return False + + r_angle = math.radians(angle) + costheta = math.cos(r_angle) + sintheta = math.sin(r_angle) + + rx = tx * costheta - ty * sintheta + ry = ty * costheta + tx * sintheta + + if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height: + return True + + return False + + @staticmethod + def obb_points(x, y, w, h, angle, points): + half_width = w / 2 + half_height = h / 2 + r_angle = math.radians(angle) + costheta = math.cos(r_angle) + sintheta = math.sin(r_angle) + + i = 0 + for point in points: + try: + px = point[0] + py = point[1] + except (KeyError, TypeError): + px = point.x + py = point.y + + tx = px - x + ty = py - y + rx = tx * costheta - ty * sintheta + ry = ty * costheta + tx * sintheta + + if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height: + return i + i += 1 + + return -1 + + @staticmethod + def obb_line(x, y, w, h, angle, lx1, ly1, lx2, ly2): + half_width = w / 2 + half_height = h / 2 + r_angle = math.radians(angle) + costheta = math.cos(r_angle) + sintheta = math.sin(r_angle) + + tx = lx1 - x + ty = ly1 - y + rx = tx * costheta - ty * sintheta + ry = ty * costheta + tx * sintheta + + if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height: + return True + + tx = lx2 - x + ty = ly2 - y + rx = tx * costheta - ty * sintheta + ry = ty * costheta + tx * sintheta + + if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height: + return True + + wc = half_width * costheta + hs = half_height * sintheta + hc = half_height * costheta + ws = half_width * sintheta + p = [ + [x + wc + hs, y + hc - ws], + [x - wc + hs, y + hc + ws], + [x + wc - hs, y - hc - ws], + [x - wc - hs, y - hc + ws], + ] + obb_lines = [ + [p[0][0], p[0][1], p[1][0], p[1][1]], + [p[1][0], p[1][1], p[3][0], p[3][1]], + [p[3][0], p[3][1], p[2][0], p[2][1]], + [p[2][0], p[2][1], p[0][0], p[0][1]] + ] + + if Collide.line_lines(lx1, ly1, lx2, ly2, obb_lines) != -1: + return True + + return False + + @staticmethod + def obb_lines(x, y, w, h, angle, lines): + half_width = w / 2 + half_height = h / 2 + r_angle = math.radians(angle) + costheta = math.cos(r_angle) + sintheta = math.sin(r_angle) + + wc = half_width * costheta + hs = half_height * sintheta + hc = half_height * costheta + ws = half_width * sintheta + p = [ + [x + wc + hs, y + hc - ws], + [x - wc + hs, y + hc + ws], + [x + wc - hs, y - hc - ws], + [x - wc - hs, y - hc + ws], + ] + obb_lines = [ + [p[0][0], p[0][1], p[1][0], p[1][1]], + [p[1][0], p[1][1], p[3][0], p[3][1]], + [p[3][0], p[3][1], p[2][0], p[2][1]], + [p[2][0], p[2][1], p[0][0], p[0][1]] + ] + + i = 0 + for l in lines: + tx = l[0] - x + ty = l[1] - y + rx = tx * costheta - ty * sintheta + ry = ty * costheta + tx * sintheta + + if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height: + return i + + tx = l[2] - x + ty = l[3] - y + rx = tx * costheta - ty * sintheta + ry = ty * costheta + tx * sintheta + + if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height: + return i + + if Collide.line_lines(l[0], l[1], l[2], l[3], obb_lines) != -1: + return i + + i += 1 + + return -1 + + @staticmethod + def obb_circle(x, y, w, h, angle, cx, cy, radius): + half_width = w / 2 + half_height = h / 2 + tx = cx - x + ty = cy - y + + if tx ** 2 + ty ** 2 > (half_height + half_width + radius) ** 2: + return False + + r_angle = math.radians(angle) + costheta = math.cos(r_angle) + sintheta = math.sin(r_angle) + + rx = tx * costheta - ty * sintheta + ry = ty * costheta + tx * sintheta + + if (rx < -half_width - radius + or rx > half_width + radius + or ry < -half_height - radius + or ry > half_height + radius + ): + return False + + if (rx <= half_width and rx >= -half_width) or (ry <= half_height and ry >= -half_height): + return True + + dx = abs(rx) - half_width + dy = abs(ry) - half_height + dist_squared = dx ** 2 + dy ** 2 + if dist_squared > radius ** 2: + return False + + return True + + @staticmethod + def obb_circles(x, y, w, h, angle, circles): + half_width = w / 2 + half_height = h / 2 + r_angle = math.radians(angle) + costheta = math.cos(r_angle) + sintheta = math.sin(r_angle) + + i = 0 + for circle in circles: + tx = circle[0] - x + ty = circle[1] - y + + rx = tx * costheta - ty * sintheta + ry = ty * costheta + tx * sintheta + + if (rx < -half_width - circle[2] + or rx > half_width + circle[2] + or ry < -half_height - circle[2] + or ry > half_height + circle[2] + ): + i += 1 + continue + + if (rx <= half_width and rx >= -half_width) or (ry <= half_height and ry >= -half_height): + return i + + dx = abs(rx) - half_width + dy = abs(ry) - half_height + dist_squared = dx ** 2 + dy ** 2 + if dist_squared > circle[2] ** 2: + i += 1 + continue + + return i + + return -1 + + @staticmethod + def obb_rect(x, y, w, h, angle, rx, ry, rw, rh): + half_width = w / 2 + half_height = h / 2 + tx = rx - x + ty = ry - y + + if tx ** 2 + ty ** 2 > (half_height + half_width + rw + rh) ** 2: + return False + + r_angle = math.radians(angle) + costheta = math.cos(r_angle) + sintheta = math.sin(r_angle) + + tx2 = tx * costheta - ty * sintheta + ty2 = ty * costheta + tx * sintheta + + if tx2 > -half_width and tx2 < half_width and ty2 > -half_height and ty2 < half_height: + return True + + wc = half_width * costheta + hs = half_height * sintheta + hc = half_height * costheta + ws = half_width * sintheta + p = [ + [wc + hs, hc - ws], + [-wc + hs, hc + ws], + [wc - hs, -hc - ws], + [-wc - hs, -hc + ws], + ] + obb_lines = [ + [p[0][0], p[0][1], p[1][0], p[1][1]], + [p[1][0], p[1][1], p[3][0], p[3][1]], + [p[3][0], p[3][1], p[2][0], p[2][1]], + [p[2][0], p[2][1], p[0][0], p[0][1]] + ] + h_rw = rw / 2 + h_rh = rh / 2 + rect_lines = [ + [tx - h_rw, ty - h_rh, tx - h_rw, ty + h_rh], + [tx + h_rw, ty - h_rh, tx + h_rw, ty + h_rh], + [tx - h_rw, ty - h_rh, tx + h_rw, ty - h_rh], + [tx - h_rw, ty + h_rh, tx + h_rw, ty + h_rh] + ] + + for obb_p in p: + if obb_p[0] > tx - h_rw and obb_p[0] < tx + h_rw and obb_p[1] > ty - h_rh and obb_p[1] < ty + h_rh: + return True + + for obb_line in obb_lines: + l1x1 = obb_line[0] + l1y1 = obb_line[1] + l1x2 = obb_line[2] + l1y2 = obb_line[3] + l1x2_l1x1 = l1x2-l1x1 + l1y2_l1y1 = l1y2-l1y1 + + for rect_line in rect_lines: + l2x1 = rect_line[0] + l2y1 = rect_line[1] + l2x2 = rect_line[2] + l2y2 = rect_line[3] + + determinant = (l2y2-l2y1)*l1x2_l1x1 - (l2x2-l2x1)*l1y2_l1y1 + + # Simplify: Parallel lines are never considered to be intersecting + if determinant == 0: + continue + + uA = ((l2x2-l2x1)*(l1y1-l2y1) - (l2y2-l2y1)*(l1x1-l2x1)) / determinant + if uA < 0 or uA > 1: + continue + + uB = (l1x2_l1x1*(l1y1-l2y1) - l1y2_l1y1*(l1x1-l2x1)) / determinant + if uB < 0 or uB > 1: + continue + + return True + + return False + + @staticmethod + def obb_rects(x, y, w, h, angle, rects): + half_width = w / 2 + half_height = h / 2 + r_angle = math.radians(angle) + costheta = math.cos(r_angle) + sintheta = math.sin(r_angle) + + i = 0 + for rect in rects: + rx = rect[0] + ry = rect[1] + rw = rect[2] + rh = rect[3] + + tx = rx - x + ty = ry - y + + if tx ** 2 + ty ** 2 > (half_height + half_width + rw + rh) ** 2: + i += 1 + continue + + tx2 = tx * costheta - ty * sintheta + ty2 = ty * costheta + tx * sintheta + + if tx2 > -half_width and tx2 < half_width and ty2 > -half_height and ty2 < half_height: + return i + + wc = half_width * costheta + hs = half_height * sintheta + hc = half_height * costheta + ws = half_width * sintheta + p = [ + [wc + hs, hc - ws], + [-wc + hs, hc + ws], + [wc - hs, -hc - ws], + [-wc - hs, -hc + ws], + ] + obb_lines = [ + [p[0][0], p[0][1], p[1][0], p[1][1]], + [p[1][0], p[1][1], p[3][0], p[3][1]], + [p[3][0], p[3][1], p[2][0], p[2][1]], + [p[2][0], p[2][1], p[0][0], p[0][1]] + ] + h_rw = rw / 2 + h_rh = rh / 2 + rect_lines = [ + [tx - h_rw, ty - h_rh, tx - h_rw, ty + h_rh], + [tx + h_rw, ty - h_rh, tx + h_rw, ty + h_rh], + [tx - h_rw, ty - h_rh, tx + h_rw, ty - h_rh], + [tx - h_rw, ty + h_rh, tx + h_rw, ty + h_rh] + ] + + for obb_p in p: + if obb_p[0] > tx - h_rw and obb_p[0] < tx + h_rw and obb_p[1] > ty - h_rh and obb_p[1] < ty + h_rh: + return i + + for obb_line in obb_lines: + l1x1 = obb_line[0] + l1y1 = obb_line[1] + l1x2 = obb_line[2] + l1y2 = obb_line[3] + l1x2_l1x1 = l1x2-l1x1 + l1y2_l1y1 = l1y2-l1y1 + + for rect_line in rect_lines: + l2x1 = rect_line[0] + l2y1 = rect_line[1] + l2x2 = rect_line[2] + l2y2 = rect_line[3] + + determinant = (l2y2-l2y1)*l1x2_l1x1 - (l2x2-l2x1)*l1y2_l1y1 + + # Simplify: Parallel lines are never considered to be intersecting + if determinant == 0: + continue + + uA = ((l2x2-l2x1)*(l1y1-l2y1) - (l2y2-l2y1)*(l1x1-l2x1)) / determinant + if uA < 0 or uA > 1: + continue + + uB = (l1x2_l1x1*(l1y1-l2y1) - l1y2_l1y1*(l1x1-l2x1)) / determinant + if uB < 0 or uB > 1: + continue + + return i + + i += 1 + + return -1 + + def obb_obb(x, y, w, h, angle, x2, y2, w2, h2, angle2): + r_angle = math.radians(angle) + costheta = math.cos(r_angle) + sintheta = math.sin(r_angle) + + tx2 = x2 - x + ty2 = y2 - y + rx2 = tx2 * costheta - ty2 * sintheta + ry2 = ty2 * costheta + tx2 * sintheta + return Collide.obb_rect(rx2, ry2, w2, h2, angle2-angle, 0, 0, w, h) + + def obb_obbs(x, y, w, h, angle, obbs): + r_angle = math.radians(angle) + costheta = math.cos(r_angle) + sintheta = math.sin(r_angle) + for obb in obbs: + x2, y2, w2, h2, angle2 = obb + tx2 = x2 - x + ty2 = y2 - y + rx2 = tx2 * costheta - ty2 * sintheta + ry2 = ty2 * costheta + tx2 * sintheta + return Collide.obb_rect(rx2, ry2, w2, h2, angle2-angle, 0, 0, w, h) + + +class Actor(Actor): + def __init__(self, image:Union[str, pygame.Surface], pos=POS_TOPLEFT, anchor=ANCHOR_CENTER, **kwargs): + self._flip_x = False + self._flip_y = False + self._scale = 1 + self._mask = None + self._images = None + self._image_idx = 0 + self._subrects = None + self._transform_cnt = 0 + self._orig_surfs = {} + self._surfs = {} + self._animate_counter = 0 + self._animate_run = False + self._radius = None + self._collision_width = None + self._collision_height = None + self.fps = 5 + self.direction = 0 + subrect=kwargs.pop('subrect',None) + image_str = None + if isinstance(image,str): + image_str = image + super().__init__(image_str, pos, anchor, **kwargs) + if isinstance(image,pygame.Surface): + self._orig_surf = image + self._update_pos() + self._subrect=None + if subrect is not None: + self.subrect=subrect + + def distance_to(self, target): + if isinstance(target, Actor): + x, y = target.pos + else: + x, y = target + return distance_to(self.x, self.y, x, y) + + def distance_toXY(self, x, y): + return distance_to(self.x, self.y, x, y) + + def direction_to(self, target): + if isinstance(target, Actor): + x, y = target.pos + else: + x, y = target + return direction_to(self.x, self.y, x, y) + + def direction_toXY(self, x, y): + return direction_to(self.x, self.y, x, y) + + + def move_towards(self, target:Union[int, float, Actor, _Coordinate], dist, stop_on_target=True): + if isinstance(target, (int,float)): + direction = target + else: + direction = self.direction_to(target) + if stop_on_target: + target_distance = self.distance_to(target) + if (target_distance < dist) and dist>0: + dist = target_distance + self.x, self.y = move(self.x, self.y, direction, dist) + + def move_towardsXY(self, x, y, dist): + direction = self.direction_toXY(x, y) + self.x, self.y = move(self.x, self.y, direction, dist) + + def point_towards(self, actor, y=None): + self.angle = self.direction_to(actor) + + def point_towardsXY(self, x, y): + self.angle = direction_to(self.x, self.y, x, y) + + def move_in_direction(self, dist): + self.x, self.y = move(self.x, self.y, self.direction, dist) + + def move_forward(self, dist): + self.x, self.y = move(self.x, self.y, self.angle, dist) + + def move_left(self, dist): + self.x, self.y = move(self.x, self.y, self.angle + 90, dist) + + def move_right(self, dist): + self.x, self.y = move(self.x, self.y, self.angle - 90, dist) + + def move_back(self, dist): + self.x, self.y = move(self.x, self.y, self.angle, -dist) + + @property + def images(self): + return self._images + + @images.setter + def images(self, images): + self._subrects = None + self._images = images + if len(self._images) != 0: + self.image = self._images[0] + + def load_images(self, sheet_name:str, cols:int, rows:int, cnt:int=0, subrect:pygame.Rect=None): + self._subrects=[None]*cols*rows + self._image_idx=0 + sheet:pygame.Surface = loaders.images.load(sheet_name) + if subrect is not None: + sheet = sheet.subsurface(subrect) + for col in range(0,cols): + for row in range(0,rows): + width=sheet.get_width()/cols + height=sheet.get_height()/rows + self._subrects[col+row*cols]=(int(col*width),int(row*height),int(width),int(height)) + if len(self._subrects) != 0: + self.image = sheet_name + self.subrect = self._subrects[0] + + def sel_image(self, newimage:Union[str, int])-> bool: + try: + if isinstance(newimage, int): + if self._subrects is None and self._images is None: + return False + if self._subrects is not None: + self.subrect = self._subrects[newimage] + else: + self.image = self._images[newimage] + self._image_idx = newimage + return True + else: + self._image_idx = self._images.index(newimage) + self.image = newimage + except: + return False + + def next_image(self)-> int: + if self._subrects is not None: + next_image_idx = (self._image_idx+1) % len(self._subrects) + self._image_idx = next_image_idx + self.subrect = self._subrects[self._image_idx] + elif (self._images is not None) : + if (self.image in self._images): + next_image_idx = (self._images.index(self.image)+1) % len(self._images) + self._image_idx = next_image_idx + self.image = self._images[self._image_idx] + else: + self._image_idx = 0 + self.image = self._images[0] + else: + self._image_idx = 0 + return self._image_idx + + def animate(self)-> int: + now = int(time.time() * self.fps) + if self._animate_counter == 0: + self._animate_counter=now + frames_elapsed = now-self._animate_counter + + if frames_elapsed!=0: + self._animate_counter = now + idx=self.next_image() + return idx + else: + return -1 + + @property + def angle(self): + return self._angle + + @angle.setter + def angle(self, angle): + self._angle = angle + self._transform_surf() + self._transform_cnt+=1 + + @property + def scale(self): + return self._scale + + @scale.setter + def scale(self, scale): + self._scale = scale + self._transform_surf() + self._transform_cnt+=1 + + @property + def flip_x(self): + return self._flip_x + + @flip_x.setter + def flip_x(self, flip_x): + self._flip_x = flip_x + self._transform_surf() + self._transform_cnt+=1 + + @property + def flip_y(self): + return self._flip_y + + @flip_y.setter + def flip_y(self, flip_y): + self._flip_y = flip_y + self._transform_surf() + self._transform_cnt+=1 + + @property + def image(self): + return self._image_name + + @image.setter + def image(self, image): + if image is not None: + self._orig_surf = self._surf = loaders.images.load(image) + self._image_name = image + self._orig_surfs[image]=self._orig_surf + else: + self._orig_surf = self._surf = pygame.Surface((1,1),pygame.SRCALPHA) + self._image_name = '' + self._update_pos() + if image is not None: + if (image not in self._surfs) or (self._surfs[image][1]!=self._transform_cnt): + self._transform_surf() + self._surfs[image]=(self._surf,self._transform_cnt) + + @property + def subrect(self): + return self._subrect + @subrect.setter + def subrect(self, subrect:pygame.Rect): + subr = subrect + if subrect is not None: + subr=pygame.Rect(subrect) + if subr != self._subrect: + self._subrect = subr + if self._subrect is not None: + hashv=hash((subr.x, subr.y,subr.width,subr.height)) + surf_name=self._image_name+str(hashv) + if surf_name not in self._orig_surfs: + self._orig_surfs[surf_name] = loaders.images.load(self.image).subsurface(subr) + self._orig_surf=self._orig_surfs[surf_name] + self._update_pos() + if (surf_name not in self._surfs) or (self._surfs[surf_name][1]!=self._transform_cnt): + self._transform_surf() + self._surfs[surf_name]=(self._surf,self._transform_cnt) + self._surf=self._surfs[surf_name][0] + else: + self._orig_surf = self._surf = loaders.images.load(self.image) + self._update_pos() + self._transform_surf() + + @property + def orig_surf(self): + return self._orig_surf + + @orig_surf.setter + def orig_surf(self, surf:pygame.Surface): + self._orig_surf = self._surf =surf + self._update_pos() + self._transform_surf() + + def recalc(self): + self._surf = self._orig_surf + self._update_pos() + self._transform_surf() + + def _transform_surf(self): + self._surf = self._orig_surf + p = self.pos + + if self._scale != 1: + size = self._orig_surf.get_size() + self._surf = pygame.transform.scale(self._surf, (int(size[0] * self.scale), int(size[1] * self.scale))) + if self._flip_x: + self._surf = pygame.transform.flip(self._surf, True, False) + if self._flip_y: + self._surf = pygame.transform.flip(self._surf, False, True) + + self._surf = pygame.transform.rotate(self._surf, self._angle) + + self.width, self.height = self._surf.get_size() + w, h = self._orig_surf.get_size() + ax, ay = self._untransformed_anchor + anchor = transform_anchor(ax, ay, w, h, self._angle) + self._anchor = (anchor[0] * self.scale, anchor[1] * self.scale) + + self.pos = p + self._mask = None + + def collidepoint_pixel(self, x, y=0): + if isinstance(x, tuple): + y = x[1] + x = x[0] + if self._mask == None: + self._mask = pygame.mask.from_surface(self._surf) + + xoffset = int(x - self.left) + yoffset = int(y - self.top) + if xoffset < 0 or yoffset < 0: + return 0 + + width, height = self._mask.get_size() + if xoffset >= width or yoffset >= height: + return 0 + + return self._mask.get_at((xoffset, yoffset)) + + def collide_pixel(self, actor): + for a in [self, actor]: + if a._mask == None: + a._mask = pygame.mask.from_surface(a._surf) + + xoffset = int(actor.left - self.left) + yoffset = int(actor.top - self.top) + + return self._mask.overlap(actor._mask, (xoffset, yoffset)) + + def collidelist_pixel(self, actors): + for i in range(len(actors)): + if self.collide_pixel(actors[i]): + return i + return -1 + + def collidelistall_pixel(self, actors): + collided = [] + for i in range(len(actors)): + if self.collide_pixel(actors[i]): + collided.append(i) + return collided + + def _unrotated_size(self): + w = self._orig_surf.get_width()*self.scale + h = self._orig_surf.get_height()*self.scale + return w, h + + @property + def collision_width(self): + if self._collision_width is None: + w,_ = self._unrotated_size() + return w + return self._collision_width + + @collision_width.setter + def collision_width(self, collision_width): + self._collision_width = collision_width + + @property + def collision_height(self): + if self._collision_height is None: + _,h = self._unrotated_size() + return h + return self._collision_height + + @collision_height.setter + def collision_height(self, collision_height): + self._collision_height = collision_height + + def obb_collidepoint(self, x, y): + w,h = self._unrotated_size() + return Collide.obb_point(self.centerx, self.centery, w, h, self._angle, x, y) + + def obb_collidepoints(self, points): + w,h = self._unrotated_size() + return Collide.obb_points(self.centerx, self.centery, w, h, self._angle, points) + + def obb_collideobb(self, actor): + if self._collision_width is None and self._collision_height is None: + x,y = self.centerx, self.centery + else: + x,y = self.x, self.y + + if actor._collision_width is None and actor._collision_height is None: + x2,y2 = actor.centerx, actor.centery + else: + x2,y2 = actor.x, actor.y + + return Collide.obb_obb(x, y, self.collision_width, self.collision_height, self._angle, + x2, y2, actor.collision_width, actor.collision_height, actor._angle) + + @property + def radius(self): + if self._radius is None: + w,h = self._unrotated_size() + self._radius = min(w, h) * .5 + return self._radius + + @radius.setter + def radius(self, radius): + self._radius = radius + + def circle_collidepoints(self, points): + return Collide.circle_points(self.centerx, self.centery, self._radius, points) + + def circle_collidepoint(self, x, y): + return Collide.circle_point(self.centerx, self.centery, self._radius, x, y) + + def circle_collidecircle(self, actor): + return Collide.circle_circle(self.centerx, self.centery, self._radius, actor.centerx, actor.centery, actor._radius) + + def circle_colliderect(self, actor): + return Collide.circle_rect(self.centerx, self.centery, self._radius, actor.centerx, actor.centery, actor.width, actor.height) + + def circle_collideobb(self, actor): + w2, h2 = actor._unrotated_size() + return Collide.obb_circle(actor.centerx, actor.centery, w2, h2, actor.angle, + self.centerx, self.centery, self._radius) + + def draw(self): + game.screen.blit(self._surf, self.topleft) + + def get_rect(self): + return self._rect diff --git a/sounds/explosion.wav b/sounds/explosion.wav new file mode 100644 index 0000000..24f1678 Binary files /dev/null and b/sounds/explosion.wav differ diff --git a/sounds/game_over.wav b/sounds/game_over.wav new file mode 100644 index 0000000..92c785d Binary files /dev/null and b/sounds/game_over.wav differ diff --git a/sounds/laser.wav b/sounds/laser.wav new file mode 100644 index 0000000..7baddf0 Binary files /dev/null and b/sounds/laser.wav differ diff --git a/sounds/loose.wav b/sounds/loose.wav new file mode 100644 index 0000000..64eae2c Binary files /dev/null and b/sounds/loose.wav differ diff --git a/sounds/powerup.wav b/sounds/powerup.wav new file mode 100644 index 0000000..39afa00 Binary files /dev/null and b/sounds/powerup.wav differ