Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/rocket-ci5.iml" filepath="$PROJECT_DIR$/rocket-ci5.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/resources.iml" filepath="$PROJECT_DIR$/.idea/resources.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="ca548a8f-03f5-4864-99be-de5c4ebdaa12" name="Default" comment="" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="TRACKING_ENABLED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileEditorManager">
<leaf />
</component>
<component name="GradleLocalSettings">
<option name="externalProjectsViewState">
<projects_view />
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="242" />
<option name="y" value="361" />
<option name="width" value="1400" />
<option name="height" value="990" />
</component>
<component name="ProjectView">
<navigator currentView="ProjectPane" proportions="" version="1">
<flattenPackages />
<showMembers />
<showModules />
<showLibraryContents />
<hideEmptyPackages />
<abbreviatePackageNames />
<autoscrollToSource />
<autoscrollFromSource />
<sortByType />
<manualOrder />
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="PackagesPane" />
<pane id="Scratches" />
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="resources" type="b2602c69:ProjectViewProjectNode" />
<item name="resources" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="resources" type="b2602c69:ProjectViewProjectNode" />
<item name="resources" type="462c0819:PsiDirectoryNode" />
<item name=".idea" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="resources" type="b2602c69:ProjectViewProjectNode" />
<item name="resources" type="462c0819:PsiDirectoryNode" />
<item name="audio" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="resources" type="b2602c69:ProjectViewProjectNode" />
<item name="resources" type="462c0819:PsiDirectoryNode" />
<item name="images" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="Scope" />
<pane id="AndroidView" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="settings.editor.selected.configurable" value="preferences.lookFeel" />
<property name="last_opened_file_path" value="$USER_HOME$/IdeaProjects/rocket-ci5" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration default="true" type="Applet" factoryName="Applet">
<option name="HTML_USED" value="false" />
<option name="WIDTH" value="400" />
<option name="HEIGHT" value="300" />
<option name="POLICY_FILE" value="$APPLICATION_HOME_DIR$/bin/appletviewer.policy" />
<module />
</configuration>
<configuration default="true" type="Application" factoryName="Application">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" />
<option name="PROGRAM_PARAMETERS" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="ENABLE_SWING_INSPECTOR" value="false" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<module name="" />
<envs />
</configuration>
<configuration default="true" type="JUnit" factoryName="JUnit">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<module name="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PACKAGE_NAME" />
<option name="MAIN_CLASS_NAME" />
<option name="METHOD_NAME" />
<option name="TEST_OBJECT" value="class" />
<option name="VM_PARAMETERS" value="-ea" />
<option name="PARAMETERS" />
<option name="WORKING_DIRECTORY" value="%MODULE_WORKING_DIR%" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="singleModule" />
</option>
<envs />
<patterns />
</configuration>
<configuration default="true" type="Remote" factoryName="Remote">
<option name="USE_SOCKET_TRANSPORT" value="true" />
<option name="SERVER_MODE" value="false" />
<option name="SHMEM_ADDRESS" value="javadebug" />
<option name="HOST" value="localhost" />
<option name="PORT" value="5005" />
</configuration>
<configuration default="true" type="TestNG" factoryName="TestNG">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<module name="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="SUITE_NAME" />
<option name="PACKAGE_NAME" />
<option name="MAIN_CLASS_NAME" />
<option name="METHOD_NAME" />
<option name="GROUP_NAME" />
<option name="TEST_OBJECT" value="CLASS" />
<option name="VM_PARAMETERS" value="-ea" />
<option name="PARAMETERS" />
<option name="WORKING_DIRECTORY" value="%MODULE_WORKING_DIR%" />
<option name="OUTPUT_DIRECTORY" />
<option name="ANNOTATION_TYPE" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="singleModule" />
</option>
<option name="USE_DEFAULT_REPORTERS" value="false" />
<option name="PROPERTIES_FILE" />
<envs />
<properties />
<listeners />
</configuration>
<configuration default="true" type="#org.jetbrains.idea.devkit.run.PluginConfigurationType" factoryName="Plugin">
<module name="" />
<option name="VM_PARAMETERS" value="-Xmx512m -Xms256m -XX:MaxPermSize=250m -ea" />
<option name="PROGRAM_PARAMETERS" />
<predefined_log_file id="idea.log" enabled="true" />
</configuration>
</component>
<component name="ShelveChangesManager" show_recycled="false">
<option name="remove_strategy" value="false" />
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="ca548a8f-03f5-4864-99be-de5c4ebdaa12" name="Default" comment="" />
<created>1523019732856</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1523019732856</updated>
</task>
<servers />
</component>
<component name="ToolWindowManager">
<frame x="-9" y="-9" width="1938" height="1048" extended-state="6" />
<layout>
<window_info id="Palette" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
<window_info id="Messages" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Palette&#9;" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Image Layers" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Capture Analysis" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
<window_info id="Maven Projects" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="false" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Capture Tool" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Designer" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Project" active="true" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="UI Designer" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Theme Preview" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
<window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
<window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
</layout>
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager />
<watches-manager />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/images/powerup_shield.png">
<provider selected="true" editor-type-id="images">
<state />
</provider>
</entry>
</component>
</project>
Digitized data copyright (c) 2012-2015, The Mozilla Foundation and Telefonica S.A.
with Reserved Font Name < Fira >,
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.
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
public class Background {
}
import java.awt.*;
import java.awt.image.BufferedImage;
public class Circle {
public BufferedImage image;
public int width;
public int height;
public Vector2D position;
public Vector2D velocity;
public Circle(Vector2D position, int width, int height, BufferedImage image, Vector2D velocity){
this.position = position;
this.image = image;
this.width = width;
this.height = height;
this.velocity = velocity;
}
public void run() {
this.position.addUp(this.velocity.normalize());
}
public void render(Graphics graphics) {
graphics.drawImage(this.image, (int)this.position.x, (int)this.position.y, this.height, this.width, null);
}
}
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GameCanvas extends JPanel{
private BufferedImage backBuffered; //trỏ null
private Graphics graphics;
private BufferedImage cycle;
private List<Star> stars;
private List<Circle> circles;
private Random random;
private int count = 0;
private Background background;
private Player player;
public GameCanvas() {
BufferedImage star; //chứa ảnh ngôi sao
//set size
this.setSize(1024, 600);
this.setupBackBufferd();
//cho hiển thị
this.setVisible(true);
//Load image
this.stars = new ArrayList<>();
this.random = new Random();
this.player = new Player(new Vector2D(200,200), new Vector2D(10, 0), Color.RED);
this.circles = new ArrayList<>();
}
private void setupStar(){
}
private void setupBackBufferd() {
this.backBuffered = new BufferedImage(1024, 600, BufferedImage.TYPE_4BYTE_ABGR);// khởi tạo object
// (con trỏ)
this.graphics = this.backBuffered.getGraphics();
BufferedImage back2 = this.backBuffered; // đang trỏ cùng đến một objject
back2.setRGB(20,20 ,255 );
}
@Override
protected void paintComponent(Graphics graphics) {
//nói về toàn bộ mọi thứ:
graphics.drawImage(this.backBuffered, 0, 0, null);
// graphics.setColor(Color.BLACK);
// graphics.fillRect(0, 0, 1024, 600);
// graphics.drawImage(this.star, this.positionStarX, this.positionStarY,100,100, null);
// graphics.drawImage(this.cycle, 100, 200,45,45, null);
}
private void drawBackground(){
this.graphics.setColor(Color.BLACK);
this.graphics.fillRect(0, 0, 1024,600 );
}
public void renderAll() {
this.drawBackground();
this.stars.forEach(star -> star.render(graphics));
this.player.render(this.graphics);
this.circles.forEach(circle -> circle.render(graphics));
this.repaint();
}
public void runAll(){
//Cập nhật tất cả mọi thứ
this.createStar();
this.stars.forEach(star -> star.run());
this.createPlayer();
this.player.playerMove();
this.circles.forEach(circle -> circle.run());
this.createCircle();
}
private void createStar(){
if (this.count == 30) {
Star star = new Star(
new Vector2D(1024, this.random.nextInt(600)),
20,
20,
this.loadImage("resources/images/star.png"),
new Vector2D(this.random.nextInt(2) + 1,0)
);
this.stars.add(star);
this.count = 0;
}
else {
this.count+=1;
}
}
private void createCircle(){
if (this.count == 30) {
Circle circle = new Circle(
new Vector2D(this.random.nextInt(1024), this.random.nextInt(600)),
40,
40,
this.loadImage("resources/images/Circle.png"),
new Vector2D(this.random.nextInt(2) + 1,0)
);
this.circles.add(circle);
this.count = 0;
}
else {
this.count+=1;
}
}
private void createPlayer(){
Player player = new Player(
new Vector2D(200,200),
new Vector2D(20,20),
Color.RED
);
}
private BufferedImage loadImage(String path){
try
{
return ImageIO.read(new File(path));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
import javax.swing.*;
public class GameWindow extends JFrame {
private GameCanvas gameCanvas;
private long lastTime = 0;
public GameWindow(){
//set size cho window
this.setSize(1024,600);
//Cho phép cửa sở window được hiển thị
this.setVisible(true);
this.setupGameCanvas();
this.setVisible(true);
}
private void setupGameCanvas(){
this.gameCanvas = new GameCanvas();
this.add(this.gameCanvas);
}
public void gameLoop() {
while (true) {
long curentTime = System.nanoTime();
if (curentTime - lastTime >= 17_000_000){
this.gameCanvas.runAll();
this.gameCanvas.renderAll();
this.lastTime = curentTime;
}
}
}
}
public class Main {
public static void main(String[] args) {
GameWindow gameWindow = new GameWindow();
gameWindow.gameLoop();
}
}
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Player {
public Vector2D position;
public Vector2D velocity;
public Color color;
private Polygon polygon;
private List<Vector2D> vertices;
Random random = new Random();
double angle = this.random.nextInt(360);
public Player(Vector2D position, Vector2D velocity, Color color){
this.position = position;
this.color = color;
this.velocity = velocity;
this.polygon = new Polygon();
this.vertices = Arrays.asList(
new Vector2D().rotate(angle),
new Vector2D(0, 16).rotate(angle),
new Vector2D(20, 8).rotate(angle)
);
this.vertices.forEach(vector2D -> polygon.addPoint((int) vector2D.x, (int) vector2D.y));
}
public void render(Graphics graphics) {
this.update();
graphics.setColor(this.color);
graphics.fillPolygon(this.polygon);
}
private void update(){
this.polygon.reset();//xóa hết các đỉnh cũ trong hình
Vector2D center = this.vertices
.stream()
.reduce(new Vector2D(),(v1, v2) -> v1.add(v2)) //bao nhiêu vector2D sẽ nhân hết vào
.multiply(1.0f / (float)this.vertices.size());
Vector2D translate = this.position.subtract(center);
this.vertices
.stream()
.map(vector2D -> vector2D.add(translate))//duyệt tất các phần tử -> vector sẽ được cộng thêm một vector khác, tạo ra một vector mới, list vector mới khác list vector mới
.forEach(vector2D -> polygon.addPoint((int) vector2D.x,(int) vector2D.y ));
}
public void playerMove() {
if (this.position.x > 1024) {
this.position.x = 0;
this.position.y = random.nextInt(600);
}
else if (this.position.x<=0){
this.position.x = 1024;
this.position.y = this.random.nextInt(600);
}
else if (this.position.y > 600) {
this.position.y = 0;
this.position.x = random.nextInt(1024);
}
else if (this.position.y<=0){
this.position.x = this.random.nextInt(1024);
this.position.y = 600;
}
this.position.addUp(velocity.rotate(angle));
}
}
import java.awt.*;
import java.awt.image.BufferedImage;
public class Star {
public BufferedImage image;
public int width;
public int height;
public Vector2D position;
public Vector2D velocity;
public Star(Vector2D position, int width, int height, BufferedImage image, Vector2D velocity){
this.position = position;
this.image = image;
this.width = width;
this.height = height;
this.velocity = velocity;
}
public void run() {
this.position.subtractBy(this.velocity );
}
public void render(Graphics graphics) {
graphics.drawImage(this.image, (int)this.position.x, (int)this.position.y, this.height, this.width, null);
}
}
public class Vector2D {
public float x;
public float y;
public Vector2D(float x, float y){
this.x = x;
this.y = y;
}
public Vector2D(){
this.x = 0;
this.y = 0;
}
public Vector2D set(float x, float y){//thay đổi giá trị của vector
this.x = x;
this.y = y;
return this;
}
public Vector2D set(Vector2D vector2D){
return this.set(vector2D.x, vector2D.y);
}
public Vector2D addUp(float x, float y){//tỏng vector
this.x += x;
this.y += y;
return this;
}
public Vector2D addUp(Vector2D vector2D){
return this.addUp(vector2D.x,vector2D.y );
}
public Vector2D add(float x, float y){//tổng vector
return new Vector2D(this.x+x, this.y+ y);
}
public Vector2D add(Vector2D vector2D){
return this.add( vector2D.x, vector2D.y);
}
public Vector2D subtractBy(float x, float y){
this.x -= x;
this.y -= y;
return this;
}
public Vector2D subtractBy(Vector2D vector2D){
return this.subtractBy(vector2D.x, vector2D.y);
}
public Vector2D subtract(float x, float y){
return new Vector2D( this.x-x,this.y-y);
}
public Vector2D subtract(Vector2D vector2D){
return this.subtract(vector2D.x, vector2D.y);
}
public Vector2D multiply(float n){
this.x *= n;
this.y *= n;
return this;
}
public Vector2D copy(){
return new Vector2D(this.x, this.y);
}
public float length(){
return (float) (Math.sqrt(this.x*this.x+this.y*this.y));
}
public Vector2D rotate(double angle){
double radian = Math.toRadians(angle);
float sin = (float)Math.sin(radian);
float cos = (float)Math.cos(radian);
return new Vector2D(this.x * cos - this.y * sin , this.x*sin + this.y*cos);
}
public Vector2D normalize(){
float length = this.length();
return new Vector2D(this.x / length, this.y / length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment