1 | /** |
---|
2 | * Copyright (c) 2001-2014 Mathew A. Nelson and Robocode contributors |
---|
3 | * All rights reserved. This program and the accompanying materials |
---|
4 | * are made available under the terms of the Eclipse Public License v1.0 |
---|
5 | * which accompanies this distribution, and is available at |
---|
6 | * http://robocode.sourceforge.net/license/epl-v10.html |
---|
7 | */ |
---|
8 | package sample; |
---|
9 | |
---|
10 | |
---|
11 | import robocode.HitByBulletEvent; |
---|
12 | import robocode.Robot; |
---|
13 | import robocode.ScannedRobotEvent; |
---|
14 | |
---|
15 | import java.awt.*; |
---|
16 | |
---|
17 | |
---|
18 | /** |
---|
19 | * PaintingRobot - a sample robot that demonstrates the onPaint() and |
---|
20 | * getGraphics() methods. |
---|
21 | * Also demonstrate feature of debugging properties on RobotDialog |
---|
22 | * <p/> |
---|
23 | * Moves in a seesaw motion, and spins the gun around at each end. |
---|
24 | * When painting is enabled for this robot, a red circle will be painted |
---|
25 | * around this robot. |
---|
26 | * |
---|
27 | * @author Stefan Westen (original SGSample) |
---|
28 | * @author Pavel Savara (contributor) |
---|
29 | */ |
---|
30 | public class PaintingRobot extends Robot { |
---|
31 | |
---|
32 | /** |
---|
33 | * PaintingRobot's run method - Seesaw |
---|
34 | */ |
---|
35 | public void run() { |
---|
36 | while (true) { |
---|
37 | ahead(100); |
---|
38 | turnGunRight(360); |
---|
39 | back(100); |
---|
40 | turnGunRight(360); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * Fire when we see a robot |
---|
46 | */ |
---|
47 | public void onScannedRobot(ScannedRobotEvent e) { |
---|
48 | // demonstrate feature of debugging properties on RobotDialog |
---|
49 | setDebugProperty("lastScannedRobot", e.getName() + " at " + e.getBearing() + " degrees at time " + getTime()); |
---|
50 | |
---|
51 | fire(1); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * We were hit! Turn perpendicular to the bullet, |
---|
56 | * so our seesaw might avoid a future shot. |
---|
57 | * In addition, draw orange circles where we were hit. |
---|
58 | */ |
---|
59 | public void onHitByBullet(HitByBulletEvent e) { |
---|
60 | // demonstrate feature of debugging properties on RobotDialog |
---|
61 | setDebugProperty("lastHitBy", e.getName() + " with power of bullet " + e.getPower() + " at time " + getTime()); |
---|
62 | |
---|
63 | // show how to remove debugging property |
---|
64 | setDebugProperty("lastScannedRobot", null); |
---|
65 | |
---|
66 | // gebugging by painting to battle view |
---|
67 | Graphics2D g = getGraphics(); |
---|
68 | |
---|
69 | g.setColor(Color.orange); |
---|
70 | g.drawOval((int) (getX() - 55), (int) (getY() - 55), 110, 110); |
---|
71 | g.drawOval((int) (getX() - 56), (int) (getY() - 56), 112, 112); |
---|
72 | g.drawOval((int) (getX() - 59), (int) (getY() - 59), 118, 118); |
---|
73 | g.drawOval((int) (getX() - 60), (int) (getY() - 60), 120, 120); |
---|
74 | |
---|
75 | turnLeft(90 - e.getBearing()); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Paint a red circle around our PaintingRobot |
---|
80 | */ |
---|
81 | public void onPaint(Graphics2D g) { |
---|
82 | g.setColor(Color.red); |
---|
83 | g.drawOval((int) (getX() - 50), (int) (getY() - 50), 100, 100); |
---|
84 | g.setColor(new Color(0, 0xFF, 0, 30)); |
---|
85 | g.fillOval((int) (getX() - 60), (int) (getY() - 60), 120, 120); |
---|
86 | } |
---|
87 | } |
---|