├── Python-Robocode ├── __init__.py ├── main.sh ├── robotImages │ ├── dead.png │ ├── tile.png │ ├── blast.png │ ├── gunGrey.png │ ├── radar.png │ ├── robot.png │ ├── small.png │ ├── baseGrey.png │ ├── smallRed.png │ ├── smallgrey.png │ ├── tileHori.png │ ├── tileVert.png │ └── robotTitre.png ├── GUI │ ├── robotImages │ │ ├── small.png │ │ ├── smallRed.png │ │ └── smallgrey.png │ ├── scrolltext.py │ ├── outPrint.ui │ ├── outPrint.py │ ├── RobotInfo.py │ ├── Ui_outPrint.py │ ├── battle.py │ ├── Ui_RobotInfo.py │ ├── RobotInfo.ui │ ├── window.py │ ├── window.ui │ ├── Ui_battle.py │ ├── Ui_window.py │ └── battle.ui ├── Objects │ ├── animation.py │ ├── statistic.py │ ├── radarField.py │ ├── physics.py │ ├── bullet.py │ ├── graph.py │ └── robot.py ├── main.py └── Robots │ ├── target.py │ ├── track_target.py │ ├── coin.py │ ├── charlier.py │ ├── wall_runner.py │ ├── demo.py │ ├── wall_tt.py │ └── T800.py ├── .gitignore └── README.md /Python-Robocode/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python-Robocode/main.sh: -------------------------------------------------------------------------------- 1 | python main.py -------------------------------------------------------------------------------- /Python-Robocode/robotImages/dead.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/dead.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/tile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/tile.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/blast.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/blast.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/gunGrey.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/gunGrey.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/radar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/radar.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/robot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/robot.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/small.png -------------------------------------------------------------------------------- /Python-Robocode/GUI/robotImages/small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/GUI/robotImages/small.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/baseGrey.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/baseGrey.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/smallRed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/smallRed.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/smallgrey.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/smallgrey.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/tileHori.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/tileHori.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/tileVert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/tileVert.png -------------------------------------------------------------------------------- /Python-Robocode/GUI/robotImages/smallRed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/GUI/robotImages/smallRed.png -------------------------------------------------------------------------------- /Python-Robocode/robotImages/robotTitre.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/robotImages/robotTitre.png -------------------------------------------------------------------------------- /Python-Robocode/GUI/robotImages/smallgrey.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turkishviking/Python-Robocode/HEAD/Python-Robocode/GUI/robotImages/smallgrey.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.directory 3 | *.eric4project 4 | *~ 5 | *.e4p 6 | *.metadata 7 | *.project 8 | *.pydevproject 9 | *.idea 10 | *.spyproject 11 | -------------------------------------------------------------------------------- /Python-Robocode/Objects/animation.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | 5 | class animation(): 6 | 7 | def __init__(self, name): 8 | self.list = [] 9 | self.name = name 10 | -------------------------------------------------------------------------------- /Python-Robocode/Objects/statistic.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | class statistic(): 5 | 6 | def __init__(self): 7 | self.first = 0 8 | self.second = 0 9 | self.third = 0 10 | self.points = 0 11 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/scrolltext.py: -------------------------------------------------------------------------------- 1 | 2 | from PyQt6.QtWidgets import QSlider, QTextEdit 3 | from PyQt6.QtCore import pyqtSignal 4 | 5 | class scrolltext(QTextEdit): 6 | def __init__(self, parent=None): 7 | QSlider.__init__(self, parent) 8 | self.wheelScrollSignal = pyqtSignal(int, int) 9 | self.wheelScrollSignal.connect(scrollContentsBy) 10 | 11 | 12 | def wheelEvent(self, event): 13 | self.wheelScrollSignal.emit(0,event.delta() / 120) 14 | # self.emit(SIGNAL("scrol(int)"), event.delta()/120) 15 | print("e") 16 | -------------------------------------------------------------------------------- /Python-Robocode/main.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | import sys 5 | import os 6 | sys.path.append(os.getcwd() + "/GUI") 7 | sys.path.append(os.getcwd() + "/Objects") 8 | sys.path.append(os.getcwd() + "/robotImages") 9 | sys.path.append(os.getcwd() + "/Robots") 10 | from window import MainWindow 11 | from PyQt6.QtWidgets import QApplication 12 | 13 | 14 | if __name__ == "__main__": 15 | 16 | app = QApplication(sys.argv) 17 | app.setApplicationName("Python-Robocode") 18 | myapp = MainWindow() 19 | myapp.show() 20 | sys.exit(app.exec()) 21 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/outPrint.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Form 4 | 5 | 6 | 7 | 0 8 | 0 9 | 444 10 | 383 11 | 12 | 13 | 14 | Form 15 | 16 | 17 | 18 | robotImages/small.pngrobotImages/small.png 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/outPrint.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | Module implementing outPrint. 5 | """ 6 | 7 | from PyQt6.QtWidgets import QWidget 8 | from PyQt6.QtGui import QTextCursor 9 | 10 | from Ui_outPrint import Ui_Form 11 | 12 | class outPrint(QWidget, Ui_Form): 13 | """ 14 | Class documentation goes here. 15 | """ 16 | def __init__(self, parent = None): 17 | """ 18 | Constructor 19 | """ 20 | QWidget.__init__(self, parent) 21 | self.setupUi(self) 22 | 23 | 24 | def add(self, msg): 25 | if self.isTextEmpty: 26 | self.textEdit.setPlainText(msg) 27 | self.isTextEmtpy = False 28 | else: 29 | self.textCursor.movePosition(QTextCursor.MoveOperation.End) 30 | self.textCursor.insertText("\n" + msg) 31 | -------------------------------------------------------------------------------- /Python-Robocode/Objects/radarField.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | 5 | from PyQt6.QtWidgets import QGraphicsItemGroup, QGraphicsPolygonItem, QGraphicsEllipseItem 6 | from PyQt6.QtGui import QPolygonF, QColor, QBrush, QPen 7 | 8 | 9 | class radarField(QGraphicsItemGroup): 10 | 11 | def __init__(self, qPointList, bot, rType): 12 | QGraphicsItemGroup.__init__(self) 13 | self.rType = rType 14 | if rType == "poly": 15 | self.item = QGraphicsPolygonItem() 16 | self.robot = bot 17 | self.polygon = QPolygonF(qPointList) 18 | self.item.setPolygon(self.polygon) 19 | 20 | elif rType == "round": 21 | self.item = QGraphicsEllipseItem() 22 | self.robot = bot 23 | self.item.setRect(qPointList[0], qPointList[1],qPointList[2],qPointList[3]) 24 | 25 | color = QColor(255, 100, 6, 10) 26 | brush = QBrush(color) 27 | pen = QPen(color) 28 | self.item.setBrush(brush) 29 | self.item.setPen(pen) 30 | self.addToGroup(self.item) 31 | 32 | 33 | def setVisible(self, bol): 34 | if bol: 35 | color = QColor(255, 100, 6, 15) 36 | else: 37 | color = QColor(255, 100, 6, 0) 38 | brush = QBrush(color) 39 | pen = QPen(color) 40 | self.item.setBrush(brush) 41 | self.item.setPen(pen) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/RobotInfo.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | Module implementing RobotInfo. 5 | """ 6 | 7 | from PyQt6.QtWidgets import QWidget 8 | from PyQt6.QtCore import pyqtSlot 9 | 10 | from outPrint import outPrint 11 | from Ui_RobotInfo import Ui_Form 12 | 13 | class RobotInfo(QWidget, Ui_Form): 14 | """ 15 | Class documentation goes here. 16 | """ 17 | def __init__(self, parent = None): 18 | """ 19 | Constructor 20 | """ 21 | QWidget.__init__(self, parent) 22 | self.setupUi(self) 23 | self.out = outPrint() 24 | 25 | 26 | @pyqtSlot() 27 | def on_pushButton_clicked(self): 28 | """ 29 | Slot documentation goes here. 30 | """ 31 | 32 | self.out.setWindowTitle(str(self.robot)) 33 | self.out.show() 34 | 35 | @pyqtSlot(int) 36 | def on_progressBar_valueChanged(self, value): 37 | """ 38 | Slot documentation goes here. 39 | """ 40 | 41 | value -= 7 42 | if value <=0: 43 | value = 0 44 | if value >= 50: 45 | green = 255 46 | red = int(510 - (value*2)*2.55) 47 | else: 48 | red = 255 49 | green = int((value*2)*2.55) 50 | self.progressBar.setStyleSheet(""" 51 | QProgressBar { 52 | border: 2px solid grey; 53 | border-radius: 5px; 54 | text-align: center; 55 | height: 5px; 56 | } 57 | QProgressBar::chunk { 58 | background-color: rgb(""" + str(red) + "," + str(green) + """,0); 59 | } 60 | """) 61 | 62 | -------------------------------------------------------------------------------- /Python-Robocode/Robots/target.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | from robot import Robot #Import a base Robot 5 | import math 6 | 7 | class Target(Robot): #Create a Robot 8 | 9 | def init(self): #To initialyse your robot 10 | 11 | 12 | #Set the bot color in RGB 13 | self.setColor(255, 0, 0) 14 | self.setGunColor(255, 0, 0) 15 | self.setRadarColor(255, 0, 0) 16 | self.setBulletsColor(255, 0, 0) 17 | 18 | self.radarVisible(True) # if True the radar field is visible 19 | 20 | #get the map size 21 | size = self.getMapSize() 22 | 23 | self.lockRadar("gun") 24 | self.setRadarField("thin") 25 | self.inTheCorner = False 26 | 27 | 28 | 29 | def run(self): #main loop to command the bot 30 | # angle=self.getHeading() % 360 31 | # print ("going angle:",angle) 32 | # self.move(5) 33 | pass 34 | 35 | def onHitWall(self): 36 | pass 37 | 38 | def sensors(self): 39 | pass 40 | 41 | def onRobotHit(self, robotId, robotName): # when My bot hit another 42 | pass 43 | 44 | def onHitByRobot(self, robotId, robotName): 45 | pass 46 | 47 | def onHitByBullet(self, bulletBotId, bulletBotName, bulletPower): #NECESARY FOR THE GAME 48 | pass 49 | 50 | def onBulletHit(self, botId, bulletId):#NECESARY FOR THE GAME 51 | pass 52 | 53 | def onBulletMiss(self, bulletId): 54 | pass 55 | 56 | def onRobotDeath(self): 57 | pass 58 | 59 | def onTargetSpotted(self, botId, botName, botPos):#NECESARY FOR THE GAME 60 | pass 61 | -------------------------------------------------------------------------------- /Python-Robocode/Robots/track_target.py: -------------------------------------------------------------------------------- 1 | import math 2 | from robot import Robot 3 | 4 | FIRE_DISTANCE = 500 5 | BULLET_POWER = 2 6 | 7 | class TargetTracker(Robot): 8 | 9 | def init(self): 10 | self.setColor(180, 180, 180) 11 | self.setGunColor(200, 200, 200) 12 | self.setRadarColor(200, 100, 0) 13 | self.setBulletsColor(255, 255, 230) 14 | 15 | self.radarVisible(True) 16 | 17 | self.areaSize = self.getMapSize() 18 | 19 | self.lockRadar("gun") 20 | self.setRadarField("thin") 21 | 22 | def run(self): 23 | self.gunTurn(5) 24 | 25 | def onHitWall(self): 26 | pass 27 | 28 | def sensors(self): 29 | pass 30 | 31 | def onRobotHit(self, robotId, robotName): 32 | pass 33 | 34 | def onHitByRobot(self, robotId, robotName): 35 | pass 36 | 37 | def onHitByBullet(self, bulletBotId, bulletBotName, bulletPower): 38 | pass 39 | 40 | def onBulletHit(self, botId, bulletId): 41 | pass 42 | 43 | def onBulletMiss(self, bulletId): 44 | pass 45 | 46 | def onRobotDeath(self): 47 | pass 48 | 49 | def onTargetSpotted(self, botId, botName, botPos): 50 | pos = self.getPosition() 51 | dx = botPos.x() - pos.x() 52 | dy = botPos.y() - pos.y() 53 | 54 | my_gun_angle = self.getGunHeading() % 360 55 | enemy_angle = math.degrees(math.atan2(dy, dx)) - 90 56 | a = enemy_angle - my_gun_angle 57 | if a < -180: 58 | a += 360 59 | elif 180 < a: 60 | a -= 360 61 | self.gunTurn(a) 62 | 63 | dist = math.sqrt(dx**2 + dy**2) 64 | if dist < FIRE_DISTANCE: 65 | self.fire(BULLET_POWER) 66 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/Ui_outPrint.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Form implementation generated from reading ui file '/home/charlie/python/PyQt-Robocode/Python-Robocode/GUI/outPrint.ui' 4 | # 5 | # Created: Thu May 30 02:58:40 2013 6 | # by: PyQt4 UI code generator 4.9.3 7 | # Modified: Thu Oct 17 12:30:00JST 2019 8 | # by: hjmr 9 | # 10 | # WARNING! All changes made in this file will be lost! 11 | 12 | 13 | from PyQt6.QtWidgets import QApplication, QWidget, QTextEdit 14 | from PyQt6.QtWidgets import QSizePolicy, QVBoxLayout, QHBoxLayout 15 | from PyQt6.QtGui import QIcon, QPixmap, QTextCursor 16 | from PyQt6.QtCore import QMetaObject 17 | 18 | 19 | class Ui_Form(object): 20 | def setupUi(self, Form): 21 | Form.setObjectName("Form") 22 | Form.resize(444, 383) 23 | icon = QIcon() 24 | icon.addPixmap(QPixmap("robotImages/small.png"), QIcon.Mode.Normal, QIcon.State.Off) 25 | Form.setWindowIcon(icon) 26 | self.verticalLayout = QVBoxLayout(Form) 27 | self.verticalLayout.setObjectName("verticalLayout") 28 | 29 | self.textEdit = QTextEdit(Form) 30 | self.textEdit.setObjectName("textEdit") 31 | self.textEdit.setPlainText(" ") 32 | self.textCursor = QTextCursor(self.textEdit.document()) 33 | self.verticalLayout.addWidget(self.textEdit) 34 | 35 | self.isTextEmpty = True 36 | 37 | self.retranslateUi(Form) 38 | QMetaObject.connectSlotsByName(Form) 39 | 40 | def retranslateUi(self, Form): 41 | Form.setWindowTitle(QApplication.translate("Form", "Form")) 42 | 43 | 44 | if __name__ == "__main__": 45 | import sys 46 | 47 | app = QApplication(sys.argv) 48 | Form = QWidget() 49 | ui = Ui_Form() 50 | ui.setupUi(Form) 51 | Form.show() 52 | sys.exit(app.exec()) 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![alt text](https://github.com/turkishviking/Python-Robocode/blob/master/Python-Robocode/robotImages/robotTitre.png?raw=true "Python-Robocode") 2 | =============== 3 | 4 | 5 | 6 | ### A Fork of Robocode for python programming 7 | 8 | This is the new and maintained version developed with PyQt 9 | 10 | ### Need help to start? Check the [wiki](https://github.com/turkishviking/Python-Robocode/wiki) 11 | 12 | Any help is welcome! This is a beta version, tell me if you notice any bugs 13 | 14 | ### If you want to contribute, I do not have the time yet for developing some base robot( wall runner, coins camper, random move etc...). If you do this, I will be happy to include it into the source code. (Post an issue or make a pull request in this case). 15 | 16 | ### What's New & Task list: 17 | 18 | - [x] move() 19 | - [x] turn() 20 | - [x] gunTurn() 21 | - [x] radarTurn() 22 | - [x] getPostion 23 | - [x] radarDetection() 24 | - [x] getTargetPosition() 25 | - [x] getTargetName() 26 | - [x] bulletPower 27 | - [x] on_hit_by_bullet() 28 | - [x] bulletSize 29 | - [x] WallCollision 30 | - [x] MapSize 31 | - [x] Number_Of_Enmies_Left() 32 | - [x] GameSpeed 33 | - [x] on_Robot_Death() 34 | - [x] reset() --> too stop all move at any time 35 | - [x] stop() --> too allow to make moves sequences 36 | - [x] RobotPrint() --> too allow the robot to print in a textBox 37 | - [x] RobotMenu with lifeBar 38 | - [x] Battle Series 39 | - [ ] Batlles Statistics 40 | - [ ] .exe 41 | - [ ] .deb 42 | - [x] Qt Integration 43 | - [ ] Qt IDE (syntax highlighter, auto completion, Base Robot) --> Not Done but I have an old project of IDE to do it 44 | - [ ] Add Classe Reference in the wiki 45 | - [ ] To prevent bot's to use Sockets, urllib2, and Sub/Multi Processing Module (more safe for users) 46 | - [x] Window resizable 47 | - [ ] Write Calculus in cython (to speed up the code) 48 | -------------------------------------------------------------------------------- /Python-Robocode/Objects/physics.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | 5 | 6 | class physics(): 7 | 8 | def __init__(self, animation): 9 | 10 | self.move = [] 11 | self.turn = [] 12 | self.gunTurn = [] 13 | self.radarTurn = [] 14 | self.fire = [] 15 | self.currentList = [] 16 | self.animation = animation 17 | 18 | self.step = 5 19 | 20 | 21 | def reverse(self): 22 | self.animation.list.reverse() 23 | 24 | def newAnimation(self): 25 | currentList = self.makeAnimation() 26 | if currentList != []: 27 | self.animation.list.append(currentList) 28 | self.clearAnimation() 29 | 30 | 31 | def makeAnimation(self , a = None): 32 | for i in range(max(len(self.move), len(self.turn), len(self.gunTurn), len(self.radarTurn), len(self.fire) )): 33 | try: 34 | m = self.move[i] 35 | except IndexError: 36 | m = 0 37 | try: 38 | t = self.turn[i] 39 | except IndexError: 40 | t = 0 41 | try: 42 | g = self.gunTurn[i] 43 | except IndexError: 44 | g = 0 45 | try: 46 | r = self.radarTurn[i] 47 | except IndexError: 48 | r = 0 49 | try: 50 | f = self.fire[i] 51 | except IndexError: 52 | f = 0 53 | self.currentList.append({"move": m, "turn": t, "gunTurn":g, "radarTurn":r, "fire":f}) 54 | self.currentList.reverse() 55 | return self.currentList 56 | 57 | def clearAnimation(self): 58 | self.move = [] 59 | self.turn = [] 60 | self.gunTurn = [] 61 | self.radarTurn = [] 62 | self.fire = [] 63 | self.currentList = [] 64 | 65 | def reset(self): 66 | self.clearAnimation() 67 | self.animation.list = [] 68 | -------------------------------------------------------------------------------- /Python-Robocode/Objects/bullet.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | import os 5 | import math 6 | 7 | from PyQt6.QtWidgets import QGraphicsPixmapItem 8 | from PyQt6.QtGui import QPixmap, QColor, QPainter 9 | from PyQt6.QtCore import Qt 10 | 11 | class Bullet(QGraphicsPixmapItem): 12 | 13 | def __init__(self, power, color, bot): 14 | QGraphicsPixmapItem.__init__(self) 15 | #graphics 16 | self.maskColor = QColor(255, 128, 0) 17 | self.pixmap = QPixmap(os.getcwd() + "/robotImages/blast.png") 18 | self.setPixmap(self.pixmap) 19 | self.setColour(color) 20 | self.isfired = False 21 | #physics 22 | self.width = self.boundingRect().width() 23 | self.height = self.boundingRect().height() 24 | if power <=0.5: 25 | power = 0.5 26 | elif power >= 10: 27 | power = 10 28 | self.power = power 29 | bsize = power 30 | if power < 3: 31 | bsize = 4 32 | self.pixmap = self.pixmap.scaled(bsize, bsize) 33 | self.setPixmap(self.pixmap) 34 | self.robot = bot 35 | 36 | def init(self, pos, angle, scene): 37 | 38 | self.angle = angle 39 | self.setPos(pos) 40 | self.scene = scene 41 | self.isfired = True 42 | 43 | 44 | def setColour(self, color): 45 | mask = self.pixmap.createMaskFromColor(self.maskColor, Qt.MaskMode.MaskOutColor) 46 | p = QPainter(self.pixmap) 47 | p.setPen(color) 48 | p.drawPixmap(self.pixmap.rect(), mask, mask.rect()) 49 | p.end() 50 | self.setPixmap(self.pixmap) 51 | self.maskColor = color 52 | 53 | def advance(self, i): 54 | if self.isfired: 55 | 56 | pos = self.pos() 57 | x = pos.x() 58 | y = pos.y() 59 | dx = - math.sin(math.radians(self.angle))*10.0 60 | dy = math.cos(math.radians(self.angle))*10.0 61 | self.setPos(x+dx, y+dy) 62 | if x < 0 or y < 0 or x > self.scene.width or y > self.scene.height: 63 | self.robot.onBulletMiss(id(self)) 64 | self.scene.removeItem(self) 65 | self.robot.removeMyProtectedItem(self) 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Python-Robocode/Robots/coin.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | from robot import Robot #Import a base Robot 5 | import math 6 | 7 | class Camper(Robot): #Create a Robot 8 | 9 | def init(self): #To initialyse your robot 10 | 11 | 12 | #Set the bot color in RGB 13 | self.setColor(250, 10, 20) 14 | self.setGunColor(0, 0, 0) 15 | self.setRadarColor(200, 100, 0) 16 | self.setBulletsColor(100, 150, 250) 17 | 18 | self.radarVisible(True) # if True the radar field is visible 19 | 20 | #get the map size 21 | size = self.getMapSize() 22 | 23 | self.lockRadar("gun") 24 | self.setRadarField("thin") 25 | self.inTheCorner = False 26 | 27 | 28 | 29 | def run(self): #main loop to command the bot 30 | 31 | pos = self.getPosition() 32 | if pos.x() > 50 or pos.y() > 50: 33 | angle = self.getHeading() 34 | a = 90 + math.degrees(math.acos(pos.x()/math.sqrt(pos.x()**2+pos.y()**2))) 35 | if angle < a: 36 | self.turn(a-angle) 37 | elif angle > a-0.5: 38 | self.turn(angle-a) 39 | self.stop() 40 | self.move(10) 41 | self.stop() 42 | else: 43 | angle = self.getGunHeading() 44 | if angle < 315: 45 | self.gunTurn(315 - angle) 46 | elif angle > 315: 47 | self.gunTurn(angle-315) 48 | self.inTheCorner = True 49 | 50 | def onHitWall(self): 51 | pass 52 | 53 | def sensors(self): 54 | pass 55 | 56 | def onRobotHit(self, robotId, robotName): # when My bot hit another 57 | pass 58 | 59 | def onHitByRobot(self, robotId, robotName): 60 | pass 61 | 62 | def onHitByBullet(self, bulletBotId, bulletBotName, bulletPower): #NECESARY FOR THE GAME 63 | pass 64 | 65 | def onBulletHit(self, botId, bulletId):#NECESARY FOR THE GAME 66 | pass 67 | 68 | def onBulletMiss(self, bulletId): 69 | pass 70 | 71 | def onRobotDeath(self): 72 | pass 73 | 74 | def onTargetSpotted(self, botId, botName, botPos):#NECESARY FOR THE GAME 75 | if self.inTheCorner: 76 | self.fire(2) 77 | self.gunTurn(2) 78 | self.stop() 79 | self.fire(2) 80 | self.gunTurn(-4) 81 | self.stop() 82 | self.fire(2) 83 | self.gunTurn(2) 84 | 85 | -------------------------------------------------------------------------------- /Python-Robocode/Robots/charlier.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | from robot import Robot #Import a base Robot 5 | 6 | class Charlier(Robot): #Create a Robot 7 | 8 | def init(self): #To initialyse your robot 9 | 10 | 11 | #Set the bot color in RGB 12 | self.setColor(0, 200, 100) 13 | self.setGunColor(200, 200, 0) 14 | self.setRadarColor(255, 60, 0) 15 | self.setBulletsColor(255, 150, 150) 16 | 17 | self.radarVisible(True) # if True the radar field is visible 18 | 19 | #get the map size 20 | size = self.getMapSize() 21 | 22 | self.lockRadar("gun") 23 | 24 | 25 | def run(self): #main loop to command the bot 26 | 27 | #self.move(90) # for moving (negative values go back) 28 | #self.stop() 29 | self.gunTurn(90) 30 | self.stop() 31 | 32 | 33 | def onHitWall(self): 34 | self.reset() #To reset the run fonction to the begining (auomatically called on hitWall, and robotHit event) 35 | self.pause(100) 36 | self.move(-100) 37 | self.rPrint('ouch! a wall !') 38 | 39 | def sensors(self): #NECESARY FOR THE GAME 40 | pass 41 | 42 | def onRobotHit(self, robotId, robotName): # when My bot hit another 43 | self.rPrint('collision with:' + str(robotId)) 44 | 45 | def onHitByRobot(self, robotId, robotName): 46 | self.rPrint("damn a bot collided me!") 47 | 48 | def onHitByBullet(self, bulletBotId, bulletBotName, bulletPower): #NECESARY FOR THE GAME 49 | """ When i'm hit by a bullet""" 50 | self.rPrint ("hit by " + str(bulletBotId) + "with power:" +str( bulletPower)) 51 | 52 | def onBulletHit(self, botId, bulletId):#NECESARY FOR THE GAME 53 | """when my bullet hit a bot""" 54 | self.rPrint ("fire done on " +str( botId)) 55 | 56 | def onBulletMiss(self, bulletId):#NECESARY FOR THE GAME 57 | """when my bullet hit a wall""" 58 | self.rPrint ("the bullet "+ str(bulletId) + " fail") 59 | 60 | def onRobotDeath(self):#NECESARY FOR THE GAME 61 | """When my bot die""" 62 | self.rPrint ("damn I'm Dead") 63 | 64 | def onTargetSpotted(self, botId, botName, botPos):#NECESARY FOR THE GAME 65 | "when the bot see another one" 66 | self.setRadarField("thin") 67 | self.rPrint("I see the bot:" + str(botId) + "on position: x:" + str(botPos.x()) + " , y:" + str(botPos.y())) 68 | self.gunTurn(5) 69 | 70 | self.stop() 71 | self.fire(5) 72 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/battle.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | Module implementing Battle. 5 | """ 6 | 7 | import os 8 | import pickle 9 | 10 | from PyQt6.QtWidgets import QDialog 11 | from PyQt6.QtCore import pyqtSlot 12 | 13 | from robot import Robot 14 | from Ui_battle import Ui_Dialog 15 | 16 | class Battle(QDialog, Ui_Dialog): 17 | """ 18 | Class documentation goes here. 19 | """ 20 | def __init__(self, parent = None): 21 | """ 22 | Constructor 23 | """ 24 | QDialog.__init__(self, parent) 25 | self.setupUi(self) 26 | self.window = parent 27 | botnames = [] 28 | self.listBots = {} 29 | botFiles = os.listdir(os.getcwd() + "/Robots") 30 | for botFile in botFiles: 31 | if botFile.endswith('.py'): 32 | botName = botPath = botFile[:botFile.rfind('.')] 33 | if botName not in botnames: 34 | botnames.append(botName) 35 | try: 36 | botModule = __import__(botPath) 37 | for name in dir(botModule): 38 | if getattr(botModule, name) in Robot.__subclasses__(): 39 | someBot = getattr(botModule, name) 40 | bot = someBot 41 | self.listBots[str(bot).replace("", "")] = bot 42 | break 43 | except Exception as e: 44 | print("Problem with bot file '{}': {}".format(botFile, str(e))) 45 | 46 | 47 | for key in self.listBots.keys(): 48 | self.listWidget.addItem(key) 49 | 50 | @pyqtSlot() 51 | def on_pushButton_clicked(self): 52 | """ 53 | Add Bot 54 | """ 55 | 56 | self.listWidget_2.addItem(self.listWidget.currentItem().text()) 57 | 58 | 59 | @pyqtSlot() 60 | def on_pushButton_2_clicked(self): 61 | """ 62 | Remove Bot 63 | """ 64 | item = self.listWidget_2.takeItem(self.listWidget_2.currentRow()) 65 | item = None 66 | 67 | @pyqtSlot() 68 | def on_pushButton_3_clicked(self): 69 | """ 70 | Start 71 | """ 72 | width = self.spinBox.value() 73 | height = self.spinBox_2.value() 74 | botList = [] 75 | for i in range(self.listWidget_2.count()): 76 | 77 | key = str(self.listWidget_2.item(i).text()) 78 | botList.append(self.listBots[key]) 79 | 80 | self.save(width, height, botList) 81 | self.window.setUpBattle(width, height, botList) 82 | 83 | 84 | 85 | def save(self, width, height, botList): 86 | dico = {} 87 | dico["width"] = width 88 | dico["height"] = height 89 | dico["botList"] = botList 90 | 91 | if not os.path.exists(os.getcwd() + "/.datas/"): 92 | os.makedirs(os.getcwd() + "/.datas/") 93 | 94 | with open(os.getcwd() + "/.datas/lastArena", 'wb') as file: 95 | pickler = pickle.Pickler(file) 96 | pickler.dump(dico) 97 | file.close() 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /Python-Robocode/Robots/wall_runner.py: -------------------------------------------------------------------------------- 1 | import math 2 | from robot import Robot 3 | 4 | 5 | MOVE_STEP = 5 6 | WALL_DISTANCE = 50 7 | BULLET_POWER = 2 8 | 9 | STATE_MOVING_UNKNOWN_DIRECTION = 0 10 | STATE_MOVING_UP = 1 11 | STATE_MOVING_RIGHT = 2 12 | STATE_MOVING_DOWN = 3 13 | STATE_MOVING_LEFT = 4 14 | 15 | class WallRunner(Robot): 16 | 17 | def init(self): 18 | self.setColor(180, 180, 180) 19 | self.setGunColor(200, 200, 200) 20 | self.setRadarColor(200, 100, 0) 21 | self.setBulletsColor(255, 255, 230) 22 | 23 | self.radarVisible(True) 24 | 25 | self.areaSize = self.getMapSize() 26 | 27 | self.lockRadar("gun") 28 | self.setRadarField("thin") 29 | 30 | self.state = STATE_MOVING_UNKNOWN_DIRECTION 31 | self.health = 100 32 | 33 | def myTurn(self, angle): 34 | self.turn(angle) 35 | self.gunTurn(angle) 36 | 37 | def run(self): 38 | pos = self.getPosition() 39 | angle = self.getHeading() % 360 40 | if self.state == STATE_MOVING_UNKNOWN_DIRECTION: 41 | self.myTurn(-angle) 42 | self.state = STATE_MOVING_DOWN 43 | elif self.state == STATE_MOVING_UP: 44 | if pos.y() < WALL_DISTANCE: 45 | self.stop() 46 | self.myTurn(90) 47 | self.state = STATE_MOVING_RIGHT 48 | else: 49 | self.move(MOVE_STEP) 50 | elif self.state == STATE_MOVING_DOWN: 51 | if self.areaSize.height() - WALL_DISTANCE < pos.y(): 52 | self.stop() 53 | self.myTurn(90) 54 | self.state = STATE_MOVING_LEFT 55 | else: 56 | self.move(MOVE_STEP) 57 | elif self.state == STATE_MOVING_LEFT: 58 | if pos.x() < WALL_DISTANCE: 59 | self.stop() 60 | self.myTurn(90) 61 | self.state = STATE_MOVING_UP 62 | else: 63 | self.move(MOVE_STEP) 64 | elif self.state == STATE_MOVING_RIGHT: 65 | if self.areaSize.width() - WALL_DISTANCE < pos.x(): 66 | self.stop() 67 | self.myTurn(90) 68 | self.state = STATE_MOVING_DOWN 69 | else: 70 | self.move(MOVE_STEP) 71 | 72 | def onHitWall(self): 73 | self.reset() 74 | self.move(-2 * MOVE_STEP) 75 | 76 | def sensors(self): 77 | pass 78 | 79 | def onRobotHit(self, robotId, robotName): 80 | pass 81 | 82 | def onHitByRobot(self, robotId, robotName): 83 | pass 84 | 85 | def onHitByBullet(self, bulletBotId, bulletBotName, bulletPower): 86 | pass 87 | 88 | def onBulletHit(self, botId, bulletId): 89 | pass 90 | 91 | def onBulletMiss(self, bulletId): 92 | pass 93 | 94 | def onRobotDeath(self): 95 | pass 96 | 97 | def onTargetSpotted(self, botId, botName, botPos): 98 | self.fire(BULLET_POWER) 99 | 100 | my_angle = self.getHeading() % 360 101 | da = 0 102 | if self.state == STATE_MOVING_UP: 103 | da = 180-my_angle 104 | elif self.state == STATE_MOVING_DOWN: 105 | da = -my_angle 106 | elif self.state == STATE_MOVING_LEFT: 107 | da = 90-my_angle 108 | elif self.state == STATE_MOVING_RIGHT: 109 | da = 270-my_angle 110 | 111 | if da == 0: 112 | self.move(MOVE_STEP) 113 | else: 114 | self.turn(da) 115 | 116 | -------------------------------------------------------------------------------- /Python-Robocode/Robots/demo.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | from robot import Robot #Import a base Robot 5 | 6 | 7 | class Demo(Robot): #Create a Robot 8 | 9 | def init(self):# NECESARY FOR THE GAME To initialyse your robot 10 | 11 | 12 | #Set the bot color in RGB 13 | self.setColor(0, 200, 100) 14 | self.setGunColor(200, 200, 0) 15 | self.setRadarColor(255, 60, 0) 16 | self.setBulletsColor(0, 200, 100) 17 | 18 | #get the map size 19 | size = self.getMapSize() #get the map size 20 | self.radarVisible(True) # show the radarField 21 | 22 | 23 | def run(self): #NECESARY FOR THE GAME main loop to command the bot 24 | 25 | 26 | self.move(90) # for moving (negative values go back) 27 | self.turn(360) #for turning (negative values turn counter-clockwise) 28 | self.stop() 29 | """ 30 | the stop command is used to make moving sequences: here the robot will move 90steps and turn 360° at the same time 31 | and next, fire 32 | """ 33 | 34 | 35 | self.fire(3) # To Fire (power between 1 and 10) 36 | 37 | self.move(100) 38 | self.turn(50) 39 | self.stop() 40 | bulletId = self.fire(2) #to let you you manage if the bullet hit or fail 41 | self.move(180) 42 | self.turn(180) 43 | self.gunTurn(90) #to turn the gun (negative values turn counter-clockwise) 44 | self.stop() 45 | self.fire(1) # To Fire (power between 1 and 10) 46 | self.radarTurn(180) #to turn the radar (negative values turn counter-clockwise) 47 | self.stop() 48 | 49 | def sensors(self): #NECESARY FOR THE GAME 50 | """Tick each frame to have datas about the game""" 51 | 52 | pos = self.getPosition() #return the center of the bot 53 | x = pos.x() #get the x coordinate 54 | y = pos.y() #get the y coordinate 55 | 56 | angle = self.getGunHeading() #Returns the direction that the robot's gun is facing 57 | angle = self.getHeading() #Returns the direction that the robot is facing 58 | angle = self.getRadarHeading() #Returns the direction that the robot's radar is facing 59 | list = self.getEnemiesLeft() #return a list of the enemies alive in the battle 60 | for robot in list: 61 | id = robot["id"] 62 | name = robot["name"] 63 | # each element of the list is a dictionnary with the bot's id and the bot's name 64 | 65 | def onHitByRobot(self, robotId, robotName): 66 | self.rPrint("damn a bot collided me!") 67 | 68 | def onHitWall(self): 69 | self.reset() #To reset the run fonction to the begining (auomatically called on hitWall, and robotHit event) 70 | self.pause(100) 71 | self.move(-100) 72 | self.rPrint('ouch! a wall !') 73 | self.setRadarField("large") #Change the radar field form 74 | 75 | def onRobotHit(self, robotId, robotName): # when My bot hit another 76 | self.rPrint('collision with:' + str(robotName)) #Print information in the robotMenu (click on the righ panel to see it) 77 | 78 | def onHitByBullet(self, bulletBotId, bulletBotName, bulletPower): #NECESARY FOR THE GAME 79 | """ When i'm hit by a bullet""" 80 | self.reset() #To reset the run fonction to the begining (auomatically called on hitWall, and robotHit event) 81 | self.rPrint ("hit by " + str(bulletBotName) + "with power:" +str( bulletPower)) 82 | 83 | def onBulletHit(self, botId, bulletId):#NECESARY FOR THE GAME 84 | """when my bullet hit a bot""" 85 | self.rPrint ("fire done on " +str( botId)) 86 | 87 | 88 | def onBulletMiss(self, bulletId):#NECESARY FOR THE GAME 89 | """when my bullet hit a wall""" 90 | self.rPrint ("the bullet "+ str(bulletId) + " fail") 91 | self.pause(10) #wait 10 frames 92 | 93 | def onRobotDeath(self):#NECESARY FOR THE GAME 94 | """When my bot die""" 95 | self.rPrint ("damn I'm Dead") 96 | 97 | def onTargetSpotted(self, botId, botName, botPos):#NECESARY FOR THE GAME 98 | "when the bot see another one" 99 | self.fire(5) 100 | self.rPrint("I see the bot:" + str(botId) + "on position: x:" + str(botPos.x()) + " , y:" + str(botPos.y())) 101 | 102 | -------------------------------------------------------------------------------- /Python-Robocode/Objects/graph.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | import time, os, random 5 | 6 | from PyQt6.QtWidgets import QGraphicsScene, QMessageBox, QGraphicsRectItem 7 | from PyQt6.QtGui import QPixmap, QColor, QBrush 8 | from PyQt6.QtCore import Qt, QPointF, QRectF 9 | 10 | from robot import Robot 11 | from outPrint import outPrint 12 | 13 | class Graph(QGraphicsScene): 14 | 15 | def __init__(self, parent, width, height): 16 | QGraphicsScene.__init__(self, parent) 17 | self.setSceneRect(0, 0, width, height) 18 | self.Parent = parent 19 | 20 | #self.Parent.graphicsView.centerOn(250, 250) 21 | self.width = width 22 | self.height = height 23 | self.grid = self.getGrid() 24 | self.setTiles() 25 | 26 | 27 | def AddRobots(self, botList): 28 | 29 | """ 30 | """ 31 | self.aliveBots = [] 32 | self.deadBots = [] 33 | try: 34 | posList = random.sample(self.grid, len(botList)) 35 | for bot in botList: 36 | try: 37 | robot = bot(self.sceneRect().size(), self, str(bot)) 38 | self.aliveBots.append(robot) 39 | self.addItem(robot) 40 | robot.setPos(posList.pop()) 41 | self.Parent.addRobotInfo(robot) 42 | except Exception as e: 43 | print("Problem with bot file '{}': {}".format(bot, str(e))) 44 | 45 | self.Parent.battleMenu.close() 46 | except ValueError: 47 | QMessageBox.about(self.Parent, "Alert", "Too many Bots for the map's size!") 48 | except AttributeError: 49 | pass 50 | 51 | def battleFinished(self): 52 | print("battle terminated") 53 | try: 54 | self.deadBots.append(self.aliveBots[0]) 55 | self.removeItem(self.aliveBots[0]) 56 | except IndexError: 57 | pass 58 | j = len(self.deadBots) 59 | 60 | 61 | for i in range(j): 62 | print("N° {}:{}".format(j - i, self.deadBots[i])) 63 | if j-i == 1: #first place 64 | self.Parent.statisticDico[repr(self.deadBots[i])].first += 1 65 | if j-i == 2: #2nd place 66 | self.Parent.statisticDico[repr(self.deadBots[i])].second += 1 67 | if j-i ==3:#3rd place 68 | self.Parent.statisticDico[repr(self.deadBots[i])].third += 1 69 | 70 | self.Parent.statisticDico[repr(self.deadBots[i])].points += i 71 | 72 | self.Parent.chooseAction() 73 | 74 | 75 | def setTiles(self): 76 | #background 77 | brush = QBrush() 78 | pix = QPixmap(os.getcwd() + "/robotImages/tile.png") 79 | brush.setTexture(pix) 80 | brush.setStyle(Qt.BrushStyle.TexturePattern) 81 | self.setBackgroundBrush(brush) 82 | 83 | #wall 84 | #left 85 | left = QGraphicsRectItem() 86 | pix = QPixmap(os.getcwd() + "/robotImages/tileVert.png") 87 | left.setRect(QRectF(0, 0, pix.width(), self.height)) 88 | brush.setTexture(pix) 89 | brush.setStyle(Qt.BrushStyle.TexturePattern) 90 | left.setBrush(brush) 91 | left.name = 'left' 92 | self.addItem(left) 93 | #right 94 | right = QGraphicsRectItem() 95 | right.setRect(self.width - pix.width(), 0, pix.width(), self.height) 96 | right.setBrush(brush) 97 | right.name = 'right' 98 | self.addItem(right) 99 | #top 100 | top = QGraphicsRectItem() 101 | pix = QPixmap(os.getcwd() + "/robotImages/tileHori.png") 102 | top.setRect(QRectF(0, 0, self.width, pix.height())) 103 | brush.setTexture(pix) 104 | brush.setStyle(Qt.BrushStyle.TexturePattern) 105 | top.setBrush(brush) 106 | top.name = 'top' 107 | self.addItem(top) 108 | #bottom 109 | bottom = QGraphicsRectItem() 110 | bottom.setRect(0 ,self.height - pix.height() , self.width, pix.height()) 111 | bottom.setBrush(brush) 112 | bottom.name = 'bottom' 113 | self.addItem(bottom) 114 | 115 | def getGrid(self): 116 | w = int(self.width/80) 117 | h = int(self.height/80) 118 | l = [] 119 | for i in range(w): 120 | for j in range(h): 121 | l.append(QPointF((i+0.5)*80, (j+0.5)*80)) 122 | return l 123 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/Ui_RobotInfo.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Form implementation generated from reading ui file '/home/charlie/python/PyQt-Robocode/Python-Robocode/GUI/RobotInfo.ui' 4 | # 5 | # Created: Fri May 31 15:45:44 2013 6 | # by: PyQt4 UI code generator 4.9.3 7 | # Modified: Thu Oct 17 12:30:00JST 2019 8 | # by: hjmr 9 | # 10 | # WARNING! All changes made in this file will be lost! 11 | 12 | from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QToolButton, QProgressBar 13 | from PyQt6.QtWidgets import QSizePolicy, QVBoxLayout, QHBoxLayout 14 | from PyQt6.QtGui import QIcon, QPixmap 15 | from PyQt6.QtCore import Qt, QSize, QMetaObject 16 | 17 | class Ui_Form(object): 18 | def setupUi(self, Form): 19 | Form.setObjectName("Form") 20 | Form.resize(180, 70) 21 | sizePolicy = QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed) 22 | sizePolicy.setHorizontalStretch(0) 23 | sizePolicy.setVerticalStretch(0) 24 | sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth()) 25 | Form.setSizePolicy(sizePolicy) 26 | Form.setMinimumSize(QSize(180, 70)) 27 | Form.setMaximumSize(QSize(180, 80)) 28 | Form.setContextMenuPolicy(Qt.ContextMenuPolicy.DefaultContextMenu) 29 | self.verticalLayout = QVBoxLayout(Form) 30 | self.verticalLayout.setSpacing(0) 31 | self.verticalLayout.setContentsMargins(0,0,0,0) 32 | self.verticalLayout.setObjectName("verticalLayout") 33 | self.horizontalLayout = QHBoxLayout() 34 | self.horizontalLayout.setObjectName("horizontalLayout") 35 | self.toolButton_2 = QToolButton(Form) 36 | self.toolButton_2.setEnabled(False) 37 | self.toolButton_2.setMinimumSize(QSize(30, 30)) 38 | self.toolButton_2.setMaximumSize(QSize(30, 30)) 39 | self.toolButton_2.setStyleSheet("background-color: rgba(255, 255, 255, 0);") 40 | icon = QIcon() 41 | icon.addPixmap(QPixmap("robotImages/small.png"), QIcon.Mode.Normal, QIcon.State.Off) 42 | icon.addPixmap(QPixmap("robotImages/small.png"), QIcon.Mode.Disabled, QIcon.State.Off) 43 | self.toolButton_2.setIcon(icon) 44 | self.toolButton_2.setIconSize(QSize(30, 30)) 45 | self.toolButton_2.setCheckable(False) 46 | self.toolButton_2.setObjectName("toolButton_2") 47 | self.horizontalLayout.addWidget(self.toolButton_2) 48 | self.pushButton = QPushButton(Form) 49 | sizePolicy = QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed) 50 | sizePolicy.setHorizontalStretch(0) 51 | sizePolicy.setVerticalStretch(0) 52 | sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth()) 53 | self.pushButton.setSizePolicy(sizePolicy) 54 | self.pushButton.setMinimumSize(QSize(130, 0)) 55 | self.pushButton.setObjectName("pushButton") 56 | self.horizontalLayout.addWidget(self.pushButton) 57 | self.verticalLayout.addLayout(self.horizontalLayout) 58 | self.horizontalLayout_2 = QHBoxLayout() 59 | self.horizontalLayout_2.setObjectName("horizontalLayout_2") 60 | self.toolButton = QToolButton(Form) 61 | self.toolButton.setEnabled(False) 62 | self.toolButton.setMinimumSize(QSize(30, 30)) 63 | self.toolButton.setMaximumSize(QSize(30, 30)) 64 | self.toolButton.setStyleSheet("background-color: rgba(255, 255, 255, 0);") 65 | icon1 = QIcon() 66 | icon1.addPixmap(QPixmap("robotImages/smallRed.png"), QIcon.Mode.Normal, QIcon.State.Off) 67 | icon1.addPixmap(QPixmap("robotImages/smallRed.png"), QIcon.Mode.Disabled, QIcon.State.Off) 68 | self.toolButton.setIcon(icon1) 69 | self.toolButton.setIconSize(QSize(30, 30)) 70 | self.toolButton.setCheckable(False) 71 | self.toolButton.setObjectName("toolButton") 72 | self.horizontalLayout_2.addWidget(self.toolButton) 73 | self.progressBar = QProgressBar(Form) 74 | sizePolicy = QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed) 75 | sizePolicy.setHorizontalStretch(0) 76 | sizePolicy.setVerticalStretch(0) 77 | sizePolicy.setHeightForWidth(self.progressBar.sizePolicy().hasHeightForWidth()) 78 | self.progressBar.setSizePolicy(sizePolicy) 79 | self.progressBar.setMinimumSize(QSize(130, 0)) 80 | self.progressBar.setProperty("value", 24) 81 | self.progressBar.setObjectName("progressBar") 82 | self.horizontalLayout_2.addWidget(self.progressBar) 83 | self.verticalLayout.addLayout(self.horizontalLayout_2) 84 | 85 | self.retranslateUi(Form) 86 | QMetaObject.connectSlotsByName(Form) 87 | 88 | def retranslateUi(self, Form): 89 | Form.setWindowTitle(QApplication.translate("Form", "Form")) 90 | self.toolButton_2.setText(QApplication.translate("Form", "...")) 91 | self.pushButton.setText(QApplication.translate("Form", "PushButton")) 92 | self.toolButton.setText(QApplication.translate("Form", "...")) 93 | 94 | 95 | if __name__ == "__main__": 96 | import sys 97 | app = QApplication(sys.argv) 98 | Form = QWidget() 99 | ui = Ui_Form() 100 | ui.setupUi(Form) 101 | Form.show() 102 | sys.exit(app.exec()) 103 | 104 | -------------------------------------------------------------------------------- /Python-Robocode/Robots/wall_tt.py: -------------------------------------------------------------------------------- 1 | import math 2 | import random 3 | from robot import Robot 4 | 5 | WALL_DISTANCE = 50 6 | FIRE_DISTANCE = 500 7 | 8 | MOVE_STEP = 10 9 | SCAN_STEP = 5 10 | 11 | STATE_MOVING_UNKNOWN_DIRECTION = -1 12 | STATE_MOVING_DOWN = 0 13 | STATE_MOVING_LEFT = 1 14 | STATE_MOVING_UP = 2 15 | STATE_MOVING_RIGHT = 3 16 | STATE_MOVING_ANGLE = (0, 90, 180, 270) 17 | 18 | class WallTargetTracker(Robot): 19 | 20 | def init(self): 21 | self.setColor(180, 180, 180) 22 | self.setGunColor(200, 200, 200) 23 | self.setRadarColor(200, 100, 0) 24 | self.setBulletsColor(255, 255, 230) 25 | 26 | self.radarVisible(True) 27 | 28 | self.areaSize = self.getMapSize() 29 | 30 | self.lockRadar("gun") 31 | self.setRadarField("thin") 32 | 33 | self.state = STATE_MOVING_UNKNOWN_DIRECTION 34 | self.health = 100 35 | 36 | self.scan_angle = 0 37 | self.scan_dir = 1 38 | self.scan_dir_can_change = True 39 | self.targetting = False 40 | 41 | def move_following_walls(self, turn_gun = True): 42 | angle = self.getHeading() % 360 43 | target_angle = STATE_MOVING_ANGLE[self.state] 44 | da = target_angle - angle 45 | while da < -180: 46 | da += 360 47 | while 180 < da: 48 | da -= 360 49 | self.turn(da / 5) 50 | if turn_gun: 51 | self.gunTurn(da / 5) 52 | if math.fabs(da) < 5: 53 | self.move(MOVE_STEP) 54 | 55 | def run(self): 56 | pos = self.getPosition() 57 | angle = self.getHeading() 58 | 59 | if self.state == STATE_MOVING_UNKNOWN_DIRECTION: 60 | dis_pos = ( 61 | self.areaSize.height() - pos.y(), 62 | pos.x(), 63 | pos.y(), 64 | self.areaSize.width() - pos.x()) 65 | self.state = dis_pos.index(min(dis_pos)) 66 | elif self.state == STATE_MOVING_UP: 67 | if pos.y() < WALL_DISTANCE: 68 | self.reset() 69 | self.state = STATE_MOVING_RIGHT 70 | elif self.state == STATE_MOVING_DOWN: 71 | if self.areaSize.height() - WALL_DISTANCE < pos.y(): 72 | self.reset() 73 | self.state = STATE_MOVING_LEFT 74 | elif self.state == STATE_MOVING_LEFT: 75 | if pos.x() < WALL_DISTANCE: 76 | self.reset() 77 | self.state = STATE_MOVING_UP 78 | elif self.state == STATE_MOVING_RIGHT: 79 | if self.areaSize.width() - WALL_DISTANCE < pos.x(): 80 | self.reset() 81 | self.state = STATE_MOVING_DOWN 82 | self.move_following_walls() 83 | 84 | scan_angle = self.getGunHeading() - angle 85 | if (scan_angle < 0 or 180 < scan_angle) and self.scan_dir_can_change: 86 | self.scan_dir *= -1 87 | self.scan_dir_can_change = False 88 | scan_step = SCAN_STEP if self.scan_dir == 1 else 2 * SCAN_STEP 89 | self.gunTurn(self.scan_dir * scan_step) 90 | if 0 < scan_angle and scan_angle < 180: 91 | self.scan_dir_can_change = True 92 | 93 | def onHitWall(self): 94 | pos = self.getPosition() 95 | dx = self.areaSize.width() / 2 - pos.x() 96 | dy = self.areaSize.height() / 2 - pos.y() 97 | angle = self.getHeading() % 360 98 | target_angle = math.degrees(math.atan2(dy, dx)) - 90 99 | da = target_angle - angle 100 | while da < -180: 101 | da += 360 102 | while 180 < da: 103 | da -= 360 104 | self.turn(da) 105 | self.move(MOVE_STEP) 106 | 107 | def sensors(self): 108 | pass 109 | 110 | def onRobotHit(self, robotId, robotName): 111 | self.health -= 1 112 | 113 | def onHitByRobot(self, robotId, robotName): 114 | self.health -= 1 115 | 116 | def onHitByBullet(self, bulletBotId, bulletBotName, bulletPower): 117 | self.health -= 3 * bulletPower 118 | 119 | def onBulletHit(self, botId, bulletId): 120 | self.health += 2 * BULLET_POWER 121 | 122 | def onBulletMiss(self, bulletId): 123 | pass 124 | 125 | def onRobotDeath(self): 126 | pass 127 | 128 | def onTargetSpotted(self, botId, botName, botPos): 129 | pos = self.getPosition() 130 | dx = botPos.x() - pos.x() 131 | dy = botPos.y() - pos.y() 132 | gun_angle = self.getGunHeading() % 360 133 | enemy_angle = math.degrees(math.atan2(dy, dx)) - 90 134 | a = enemy_angle - gun_angle 135 | while a < -180: 136 | a += 360 137 | while 180 < a: 138 | a -= 360 139 | self.gunTurn(a) 140 | 141 | dist = math.sqrt(dx**2 + dy**2) 142 | if dist < FIRE_DISTANCE: 143 | bullet_power = 1 144 | if 50 < self.health: 145 | bullet_power = 5 146 | elif 25 < self.health: 147 | bullet_power = 3 148 | else: 149 | bullet_power = 1 150 | self.health -= bullet_power 151 | self.fire(bullet_power) 152 | 153 | self.move_following_walls(turn_gun = False) 154 | 155 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/RobotInfo.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Form 4 | 5 | 6 | 7 | 0 8 | 0 9 | 180 10 | 70 11 | 12 | 13 | 14 | 15 | 0 16 | 0 17 | 18 | 19 | 20 | 21 | 180 22 | 70 23 | 24 | 25 | 26 | 27 | 180 28 | 80 29 | 30 | 31 | 32 | Qt::DefaultContextMenu 33 | 34 | 35 | Form 36 | 37 | 38 | 39 | 0 40 | 41 | 42 | 0 43 | 44 | 45 | 46 | 47 | 48 | 49 | false 50 | 51 | 52 | 53 | 30 54 | 30 55 | 56 | 57 | 58 | 59 | 30 60 | 30 61 | 62 | 63 | 64 | background-color: rgba(255, 255, 255, 0); 65 | 66 | 67 | ... 68 | 69 | 70 | 71 | robotImages/small.png 72 | robotImages/small.pngrobotImages/small.png 73 | 74 | 75 | 76 | 30 77 | 30 78 | 79 | 80 | 81 | false 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 0 90 | 0 91 | 92 | 93 | 94 | 95 | 130 96 | 0 97 | 98 | 99 | 100 | PushButton 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | false 112 | 113 | 114 | 115 | 30 116 | 30 117 | 118 | 119 | 120 | 121 | 30 122 | 30 123 | 124 | 125 | 126 | background-color: rgba(255, 255, 255, 0); 127 | 128 | 129 | ... 130 | 131 | 132 | 133 | robotImages/smallRed.png 134 | robotImages/smallRed.pngrobotImages/smallRed.png 135 | 136 | 137 | 138 | 30 139 | 30 140 | 141 | 142 | 143 | false 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 0 152 | 0 153 | 154 | 155 | 156 | 157 | 130 158 | 0 159 | 160 | 161 | 162 | 24 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/window.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | Module implementing MainWindow. 5 | """ 6 | 7 | import os 8 | import pickle 9 | import re 10 | 11 | from importlib import reload 12 | 13 | from PyQt6.QtWidgets import QMainWindow, QGraphicsScene, QHeaderView, QTableWidgetItem 14 | from PyQt6.QtCore import pyqtSlot, QTimer 15 | 16 | from graph import Graph 17 | from Ui_window import Ui_MainWindow 18 | from battle import Battle 19 | from robot import Robot 20 | from RobotInfo import RobotInfo 21 | from statistic import statistic 22 | 23 | 24 | class MainWindow(QMainWindow, Ui_MainWindow): 25 | """ 26 | Class documentation goes here. 27 | """ 28 | def __init__(self, parent = None): 29 | """ 30 | Constructor 31 | """ 32 | QMainWindow.__init__(self, parent) 33 | self.setupUi(self) 34 | self.countBattle = 0 35 | self.timer = QTimer() 36 | self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch) 37 | self.tableWidget.hide() 38 | 39 | def reimport_class(self, cls): 40 | """ 41 | Reload and reimport class "cls". 42 | """ 43 | 44 | mod = __import__(cls.__module__, fromlist=[cls.__name__]) 45 | reload(mod) 46 | 47 | return getattr(mod, cls.__name__) 48 | 49 | @pyqtSlot() 50 | def on_pushButton_clicked(self): 51 | """ 52 | Start the last battle 53 | """ 54 | 55 | if os.path.exists(os.getcwd() + "/.datas/lastArena"): 56 | with open(os.getcwd() + "/.datas/lastArena", 'rb') as file: 57 | unpickler = pickle.Unpickler(file) 58 | dico = unpickler.load() 59 | file.close() 60 | else: 61 | print("No last arena found.") 62 | 63 | botList = [self.reimport_class(bot) for bot in dico["botList"]] 64 | self.setUpBattle(dico["width"], dico["height"], botList) 65 | 66 | def setUpBattle(self, width, height, botList): 67 | self.tableWidget.clearContents() 68 | self.tableWidget.hide() 69 | self.graphicsView.show() 70 | self.width = width 71 | self.height = height 72 | self.botList = botList 73 | self.statisticDico={} 74 | for bot in botList: 75 | self.statisticDico[self.repres(bot)] = statistic() 76 | self.startBattle() 77 | 78 | def startBattle(self): 79 | 80 | try: 81 | self.timer.timeout.disconnect(self.scene.advance) 82 | del self.timer 83 | del self.scene 84 | del self.sceneMenu 85 | except: 86 | pass 87 | 88 | self.timer = QTimer() 89 | self.countBattle += 1 90 | self.sceneMenu = QGraphicsScene() 91 | self.graphicsView_2.setScene(self.sceneMenu) 92 | self.scene = Graph(self, self.width, self.height) 93 | self.graphicsView.setScene(self.scene) 94 | self.scene.AddRobots(self.botList) 95 | self.timer.timeout.connect(self.scene.advance) 96 | self.timer.start((self.horizontalSlider.value()**2)//100) 97 | self.resizeEvent() 98 | 99 | @pyqtSlot(int) 100 | def on_horizontalSlider_valueChanged(self, value): 101 | """ 102 | Slot documentation goes here. 103 | """ 104 | self.timer.setInterval((value**2)//100) 105 | 106 | @pyqtSlot() 107 | def on_actionNew_triggered(self): 108 | """ 109 | Battle Menu 110 | """ 111 | self.battleMenu = Battle(self) 112 | self.battleMenu.show() 113 | 114 | @pyqtSlot() 115 | def on_actionNew_2_triggered(self): 116 | """ 117 | Slot documentation goes here. 118 | """ 119 | # TODO: not implemented yet 120 | print("Not Implemented Yet") 121 | 122 | @pyqtSlot() 123 | def on_actionOpen_triggered(self): 124 | """ 125 | Slot documentation goes here. 126 | """ 127 | # TODO: not implemented yet 128 | print("Not Implemented Yet") 129 | 130 | def resizeEvent(self, evt=None): 131 | try: 132 | self.graphicsView.fitInView(self.scene.sceneRect(), 4) 133 | except : 134 | pass 135 | 136 | def addRobotInfo(self, robot): 137 | self.sceneMenu.setSceneRect(0, 0, 170, 800) 138 | rb = RobotInfo() 139 | rb.pushButton.setText(str(robot)) 140 | rb.progressBar.setValue(100) 141 | rb.robot = robot 142 | robot.info = rb 143 | robot.progressBar = rb.progressBar 144 | robot.icon = rb.toolButton 145 | robot.icon2 = rb.toolButton_2 146 | p = self.sceneMenu.addWidget(rb) 147 | l = (len(self.scene.aliveBots) ) 148 | self.sceneMenu.setSceneRect(0, 0, 170, l*80) 149 | p.setPos(0, (l -1)*80) 150 | 151 | def chooseAction(self): 152 | if self.countBattle >= self.spinBox.value(): 153 | "Menu Statistic" 154 | self.graphicsView.hide() 155 | self.tableWidget.show() 156 | self.tableWidget.setRowCount(len(self.statisticDico)) 157 | i = 0 158 | for key, value in self.statisticDico.items(): 159 | self.tableWidget.setItem(i, 0, QTableWidgetItem(key)) 160 | self.tableWidget.setItem(i, 1, QTableWidgetItem(str(value.first))) 161 | self.tableWidget.setItem(i, 2, QTableWidgetItem(str(value.second))) 162 | self.tableWidget.setItem(i, 3, QTableWidgetItem(str(value.third))) 163 | self.tableWidget.setItem(i, 4, QTableWidgetItem(str(value.points))) 164 | 165 | i += 1 166 | 167 | 168 | self.countBattle = 0 169 | self.timer.stop() 170 | else: 171 | self.startBattle() 172 | 173 | def repres(self, bot): 174 | repres = repr(bot).split(".") 175 | return repres[1].replace("'>", "") 176 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/window.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 792 10 | 464 11 | 12 | 13 | 14 | Python Robocode 15 | 16 | 17 | 18 | robotImages/smallRed.pngrobotImages/smallRed.png 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | false 30 | 31 | 32 | true 33 | 34 | 35 | false 36 | 37 | 38 | 39 | Name 40 | 41 | 42 | 43 | 44 | 1st 45 | 46 | 47 | 48 | 49 | 2nd 50 | 51 | 52 | 53 | 54 | 3rd 55 | 56 | 57 | 58 | 59 | Points 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | true 68 | 69 | 70 | background-color: rgba(206, 206, 206, 162); 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | Start Last Battle 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | Battle's Number 91 | 92 | 93 | 94 | 95 | 96 | 97 | 10000 98 | 99 | 100 | 10 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | Qt::Horizontal 112 | 113 | 114 | 115 | 40 116 | 20 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | Qt::LeftToRight 127 | 128 | 129 | 130 | 131 | 132 | Game Speed 133 | 134 | 135 | Qt::AlignCenter 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 0 144 | 0 145 | 146 | 147 | 148 | 149 | 200 150 | 0 151 | 152 | 153 | 154 | Qt::RightToLeft 155 | 156 | 157 | 120 158 | 159 | 160 | 60 161 | 162 | 163 | Qt::Horizontal 164 | 165 | 166 | false 167 | 168 | 169 | true 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 0 184 | 0 185 | 186 | 187 | 188 | 189 | 200 190 | 0 191 | 192 | 193 | 194 | 195 | 200 196 | 16777215 197 | 198 | 199 | 200 | background-color: rgba(194, 194, 194, 167); 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 0 212 | 0 213 | 792 214 | 23 215 | 216 | 217 | 218 | 219 | Battle 220 | 221 | 222 | 223 | 224 | 225 | Robot 226 | 227 | 228 | 229 | 230 | 231 | 232 | Help 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | New 246 | 247 | 248 | 249 | 250 | New 251 | 252 | 253 | 254 | 255 | Open 256 | 257 | 258 | 259 | 260 | Class Reference 261 | 262 | 263 | 264 | 265 | About 266 | 267 | 268 | 269 | 270 | 271 | 272 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/Ui_battle.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Form implementation generated from reading ui file '/home/charlie/python/PyQt-Robocode/Python-Robocode/GUI/battle.ui' 4 | # 5 | # Created: Thu May 30 02:21:40 2013 6 | # by: PyQt4 UI code generator 4.9.3 7 | # Modified: Thu Oct 17 12:30:00JST 2019 8 | # by: hjmr 9 | # 10 | # WARNING! All changes made in this file will be lost! 11 | 12 | from PyQt6.QtWidgets import QApplication, QDialog, QLabel, QSpacerItem, QListWidget, QPushButton, QSpinBox 13 | from PyQt6.QtWidgets import QSizePolicy, QVBoxLayout, QHBoxLayout 14 | from PyQt6.QtGui import QIcon, QPixmap, QFont 15 | from PyQt6.QtCore import QSize, QMetaObject 16 | 17 | class Ui_Dialog(object): 18 | def setupUi(self, Dialog): 19 | Dialog.setObjectName("Dialog") 20 | Dialog.resize(572, 399) 21 | icon = QIcon() 22 | icon.addPixmap(QPixmap("robotImages/smallgrey.png"), QIcon.Mode.Normal, QIcon.State.Off) 23 | Dialog.setWindowIcon(icon) 24 | Dialog.setModal(False) 25 | self.verticalLayout_5 = QVBoxLayout(Dialog) 26 | self.verticalLayout_5.setObjectName("verticalLayout_5") 27 | self.horizontalLayout_6 = QHBoxLayout() 28 | self.horizontalLayout_6.setObjectName("horizontalLayout_6") 29 | self.verticalLayout_3 = QVBoxLayout() 30 | self.verticalLayout_3.setObjectName("verticalLayout_3") 31 | self.horizontalLayout = QHBoxLayout() 32 | self.horizontalLayout.setObjectName("horizontalLayout") 33 | spacerItem = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 34 | self.horizontalLayout.addItem(spacerItem) 35 | self.label = QLabel(Dialog) 36 | font = QFont() 37 | font.setPointSize(10) 38 | font.setBold(True) 39 | font.setWeight(75) 40 | self.label.setFont(font) 41 | self.label.setObjectName("label") 42 | self.horizontalLayout.addWidget(self.label) 43 | spacerItem1 = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 44 | self.horizontalLayout.addItem(spacerItem1) 45 | self.verticalLayout_3.addLayout(self.horizontalLayout) 46 | self.listWidget = QListWidget(Dialog) 47 | self.listWidget.setObjectName("listWidget") 48 | self.verticalLayout_3.addWidget(self.listWidget) 49 | self.horizontalLayout_6.addLayout(self.verticalLayout_3) 50 | self.verticalLayout = QVBoxLayout() 51 | self.verticalLayout.setObjectName("verticalLayout") 52 | self.pushButton = QPushButton(Dialog) 53 | sizePolicy = QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed) 54 | sizePolicy.setHorizontalStretch(0) 55 | sizePolicy.setVerticalStretch(0) 56 | sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth()) 57 | self.pushButton.setSizePolicy(sizePolicy) 58 | self.pushButton.setObjectName("pushButton") 59 | self.verticalLayout.addWidget(self.pushButton) 60 | self.pushButton_2 = QPushButton(Dialog) 61 | sizePolicy = QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed) 62 | sizePolicy.setHorizontalStretch(0) 63 | sizePolicy.setVerticalStretch(0) 64 | sizePolicy.setHeightForWidth(self.pushButton_2.sizePolicy().hasHeightForWidth()) 65 | self.pushButton_2.setSizePolicy(sizePolicy) 66 | self.pushButton_2.setObjectName("pushButton_2") 67 | self.verticalLayout.addWidget(self.pushButton_2) 68 | self.horizontalLayout_6.addLayout(self.verticalLayout) 69 | self.verticalLayout_4 = QVBoxLayout() 70 | self.verticalLayout_4.setObjectName("verticalLayout_4") 71 | self.horizontalLayout_5 = QHBoxLayout() 72 | self.horizontalLayout_5.setObjectName("horizontalLayout_5") 73 | spacerItem2 = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 74 | self.horizontalLayout_5.addItem(spacerItem2) 75 | self.label_2 = QLabel(Dialog) 76 | font = QFont() 77 | font.setPointSize(10) 78 | font.setBold(True) 79 | font.setWeight(75) 80 | self.label_2.setFont(font) 81 | self.label_2.setObjectName("label_2") 82 | self.horizontalLayout_5.addWidget(self.label_2) 83 | spacerItem3 = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 84 | self.horizontalLayout_5.addItem(spacerItem3) 85 | self.verticalLayout_4.addLayout(self.horizontalLayout_5) 86 | self.listWidget_2 = QListWidget(Dialog) 87 | self.listWidget_2.setObjectName("listWidget_2") 88 | self.verticalLayout_4.addWidget(self.listWidget_2) 89 | self.horizontalLayout_6.addLayout(self.verticalLayout_4) 90 | self.verticalLayout_5.addLayout(self.horizontalLayout_6) 91 | self.verticalLayout_2 = QVBoxLayout() 92 | self.verticalLayout_2.setObjectName("verticalLayout_2") 93 | self.horizontalLayout_4 = QHBoxLayout() 94 | self.horizontalLayout_4.setObjectName("horizontalLayout_4") 95 | spacerItem4 = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 96 | self.horizontalLayout_4.addItem(spacerItem4) 97 | self.label_3 = QLabel(Dialog) 98 | font = QFont() 99 | font.setPointSize(10) 100 | font.setBold(True) 101 | font.setWeight(75) 102 | self.label_3.setFont(font) 103 | self.label_3.setObjectName("label_3") 104 | self.horizontalLayout_4.addWidget(self.label_3) 105 | spacerItem5 = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 106 | self.horizontalLayout_4.addItem(spacerItem5) 107 | self.verticalLayout_2.addLayout(self.horizontalLayout_4) 108 | self.horizontalLayout_3 = QHBoxLayout() 109 | self.horizontalLayout_3.setObjectName("horizontalLayout_3") 110 | spacerItem6 = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 111 | self.horizontalLayout_3.addItem(spacerItem6) 112 | self.horizontalLayout_2 = QHBoxLayout() 113 | self.horizontalLayout_2.setObjectName("horizontalLayout_2") 114 | self.label_5 = QLabel(Dialog) 115 | self.label_5.setObjectName("label_5") 116 | self.horizontalLayout_2.addWidget(self.label_5) 117 | self.spinBox = QSpinBox(Dialog) 118 | self.spinBox.setMinimum(300) 119 | self.spinBox.setMaximum(100000) 120 | self.spinBox.setProperty("value", 700) 121 | self.spinBox.setObjectName("spinBox") 122 | self.horizontalLayout_2.addWidget(self.spinBox) 123 | self.label_4 = QLabel(Dialog) 124 | font = QFont() 125 | font.setPointSize(10) 126 | font.setBold(True) 127 | font.setWeight(75) 128 | self.label_4.setFont(font) 129 | self.label_4.setObjectName("label_4") 130 | self.horizontalLayout_2.addWidget(self.label_4) 131 | self.spinBox_2 = QSpinBox(Dialog) 132 | self.spinBox_2.setMinimum(300) 133 | self.spinBox_2.setMaximum(100000) 134 | self.spinBox_2.setProperty("value", 500) 135 | self.spinBox_2.setObjectName("spinBox_2") 136 | self.horizontalLayout_2.addWidget(self.spinBox_2) 137 | self.horizontalLayout_3.addLayout(self.horizontalLayout_2) 138 | self.label_6 = QLabel(Dialog) 139 | self.label_6.setObjectName("label_6") 140 | self.horizontalLayout_3.addWidget(self.label_6) 141 | spacerItem7 = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 142 | self.horizontalLayout_3.addItem(spacerItem7) 143 | self.verticalLayout_2.addLayout(self.horizontalLayout_3) 144 | self.verticalLayout_5.addLayout(self.verticalLayout_2) 145 | self.horizontalLayout_7 = QHBoxLayout() 146 | self.horizontalLayout_7.setObjectName("horizontalLayout_7") 147 | spacerItem8 = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 148 | self.horizontalLayout_7.addItem(spacerItem8) 149 | self.pushButton_3 = QPushButton(Dialog) 150 | sizePolicy = QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed) 151 | sizePolicy.setHorizontalStretch(0) 152 | sizePolicy.setVerticalStretch(0) 153 | sizePolicy.setHeightForWidth(self.pushButton_3.sizePolicy().hasHeightForWidth()) 154 | self.pushButton_3.setSizePolicy(sizePolicy) 155 | self.pushButton_3.setMinimumSize(QSize(150, 0)) 156 | font = QFont() 157 | font.setBold(True) 158 | font.setWeight(75) 159 | self.pushButton_3.setFont(font) 160 | self.pushButton_3.setObjectName("pushButton_3") 161 | self.horizontalLayout_7.addWidget(self.pushButton_3) 162 | spacerItem9 = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 163 | self.horizontalLayout_7.addItem(spacerItem9) 164 | self.verticalLayout_5.addLayout(self.horizontalLayout_7) 165 | 166 | self.retranslateUi(Dialog) 167 | QMetaObject.connectSlotsByName(Dialog) 168 | 169 | def retranslateUi(self, Dialog): 170 | Dialog.setWindowTitle(QApplication.translate("Dialog", "Battle Selection")) 171 | self.label.setText(QApplication.translate("Dialog", "Available Bots:")) 172 | self.pushButton.setText(QApplication.translate("Dialog", ">>")) 173 | self.pushButton_2.setText(QApplication.translate("Dialog", "<<")) 174 | self.label_2.setText(QApplication.translate("Dialog", "Selected Bots")) 175 | self.label_3.setText(QApplication.translate("Dialog", "Arena Size")) 176 | self.label_5.setText(QApplication.translate("Dialog", "Width")) 177 | self.spinBox.setSuffix(QApplication.translate("Dialog", "px")) 178 | self.label_4.setText(QApplication.translate("Dialog", "X")) 179 | self.spinBox_2.setSuffix(QApplication.translate("Dialog", "px")) 180 | self.label_6.setText(QApplication.translate("Dialog", "Height")) 181 | self.pushButton_3.setText(QApplication.translate("Dialog", "-- Start --")) 182 | 183 | 184 | if __name__ == "__main__": 185 | import sys 186 | app = QApplication(sys.argv) 187 | Dialog = QDialog() 188 | ui = Ui_Dialog() 189 | ui.setupUi(Dialog) 190 | Dialog.show() 191 | sys.exit(app.exec()) 192 | 193 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/Ui_window.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Form implementation generated from reading ui file '/home/charlie/Documents/Python/RobotCode/PyQt-Robocode/Python-Robocode/GUI/window.ui' 4 | # 5 | # Created: Fri Dec 20 17:46:14 2013 6 | # by: PyQt4 UI code generator 4.10 7 | # Modified: Thu Oct 17 12:30:00JST 2019 8 | # by: hjmr 9 | # 10 | # WARNING! All changes made in this file will be lost! 11 | 12 | 13 | from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QTableWidget, QGraphicsView 14 | from PyQt6.QtWidgets import QTableWidget, QTableWidgetItem, QPushButton, QSlider, QLabel 15 | from PyQt6.QtWidgets import QSpinBox, QSpacerItem, QMenuBar, QMenu, QStatusBar 16 | from PyQt6.QtWidgets import QSizePolicy, QVBoxLayout, QHBoxLayout 17 | from PyQt6.QtGui import QIcon, QPixmap, QAction 18 | from PyQt6.QtCore import Qt, QSize, QRect, QMetaObject 19 | 20 | 21 | class Ui_MainWindow(object): 22 | def setupUi(self, MainWindow): 23 | MainWindow.setObjectName("MainWindow") 24 | MainWindow.resize(792, 464) 25 | icon = QIcon() 26 | icon.addPixmap(QPixmap("robotImages/smallRed.png"), QIcon.Mode.Normal, QIcon.State.Off) 27 | MainWindow.setWindowIcon(icon) 28 | self.centralwidget = QWidget(MainWindow) 29 | self.centralwidget.setObjectName("centralwidget") 30 | self.verticalLayout_4 = QVBoxLayout(self.centralwidget) 31 | self.verticalLayout_4.setObjectName("verticalLayout_4") 32 | self.horizontalLayout_3 = QHBoxLayout() 33 | self.horizontalLayout_3.setObjectName("horizontalLayout_3") 34 | self.verticalLayout_3 = QVBoxLayout() 35 | self.verticalLayout_3.setObjectName("verticalLayout_3") 36 | self.tableWidget = QTableWidget(self.centralwidget) 37 | self.tableWidget.setAutoFillBackground(False) 38 | self.tableWidget.setObjectName("tableWidget") 39 | self.tableWidget.setColumnCount(5) 40 | self.tableWidget.setRowCount(0) 41 | item = QTableWidgetItem() 42 | self.tableWidget.setHorizontalHeaderItem(0, item) 43 | item = QTableWidgetItem() 44 | self.tableWidget.setHorizontalHeaderItem(1, item) 45 | item = QTableWidgetItem() 46 | self.tableWidget.setHorizontalHeaderItem(2, item) 47 | item = QTableWidgetItem() 48 | self.tableWidget.setHorizontalHeaderItem(3, item) 49 | item = QTableWidgetItem() 50 | self.tableWidget.setHorizontalHeaderItem(4, item) 51 | self.tableWidget.horizontalHeader().setStretchLastSection(False) 52 | self.verticalLayout_3.addWidget(self.tableWidget) 53 | self.graphicsView = QGraphicsView(self.centralwidget) 54 | self.graphicsView.setEnabled(True) 55 | self.graphicsView.setStyleSheet("background-color: rgba(206, 206, 206, 162);") 56 | self.graphicsView.setObjectName("graphicsView") 57 | self.verticalLayout_3.addWidget(self.graphicsView) 58 | self.horizontalLayout_2 = QHBoxLayout() 59 | self.horizontalLayout_2.setObjectName("horizontalLayout_2") 60 | self.verticalLayout = QVBoxLayout() 61 | self.verticalLayout.setObjectName("verticalLayout") 62 | self.pushButton = QPushButton(self.centralwidget) 63 | self.pushButton.setObjectName("pushButton") 64 | self.verticalLayout.addWidget(self.pushButton) 65 | self.horizontalLayout = QHBoxLayout() 66 | self.horizontalLayout.setObjectName("horizontalLayout") 67 | self.label_2 = QLabel(self.centralwidget) 68 | self.label_2.setObjectName("label_2") 69 | self.horizontalLayout.addWidget(self.label_2) 70 | self.spinBox = QSpinBox(self.centralwidget) 71 | self.spinBox.setMaximum(10000) 72 | self.spinBox.setProperty("value", 10) 73 | self.spinBox.setObjectName("spinBox") 74 | self.horizontalLayout.addWidget(self.spinBox) 75 | self.verticalLayout.addLayout(self.horizontalLayout) 76 | self.horizontalLayout_2.addLayout(self.verticalLayout) 77 | spacerItem = QSpacerItem(40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 78 | self.horizontalLayout_2.addItem(spacerItem) 79 | self.verticalLayout_2 = QVBoxLayout() 80 | self.verticalLayout_2.setObjectName("verticalLayout_2") 81 | self.label = QLabel(self.centralwidget) 82 | self.label.setLayoutDirection(Qt.LayoutDirection.LeftToRight) 83 | self.label.setStyleSheet("") 84 | self.label.setAlignment(Qt.AlignmentFlag.AlignCenter) 85 | self.label.setObjectName("label") 86 | self.verticalLayout_2.addWidget(self.label) 87 | self.horizontalSlider = QSlider(self.centralwidget) 88 | sizePolicy = QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed) 89 | sizePolicy.setHorizontalStretch(0) 90 | sizePolicy.setVerticalStretch(0) 91 | sizePolicy.setHeightForWidth(self.horizontalSlider.sizePolicy().hasHeightForWidth()) 92 | self.horizontalSlider.setSizePolicy(sizePolicy) 93 | self.horizontalSlider.setMinimumSize(QSize(200, 0)) 94 | self.horizontalSlider.setLayoutDirection(Qt.LayoutDirection.RightToLeft) 95 | self.horizontalSlider.setMaximum(120) 96 | self.horizontalSlider.setProperty("value", 60) 97 | self.horizontalSlider.setOrientation(Qt.Orientation.Horizontal) 98 | self.horizontalSlider.setInvertedAppearance(False) 99 | self.horizontalSlider.setInvertedControls(True) 100 | self.horizontalSlider.setObjectName("horizontalSlider") 101 | self.verticalLayout_2.addWidget(self.horizontalSlider) 102 | self.horizontalLayout_2.addLayout(self.verticalLayout_2) 103 | self.verticalLayout_3.addLayout(self.horizontalLayout_2) 104 | self.horizontalLayout_3.addLayout(self.verticalLayout_3) 105 | self.graphicsView_2 = QGraphicsView(self.centralwidget) 106 | sizePolicy = QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Expanding) 107 | sizePolicy.setHorizontalStretch(0) 108 | sizePolicy.setVerticalStretch(0) 109 | sizePolicy.setHeightForWidth(self.graphicsView_2.sizePolicy().hasHeightForWidth()) 110 | self.graphicsView_2.setSizePolicy(sizePolicy) 111 | self.graphicsView_2.setMinimumSize(QSize(200, 0)) 112 | self.graphicsView_2.setMaximumSize(QSize(200, 16777215)) 113 | self.graphicsView_2.setStyleSheet("background-color: rgba(194, 194, 194, 167);") 114 | self.graphicsView_2.setObjectName("graphicsView_2") 115 | self.horizontalLayout_3.addWidget(self.graphicsView_2) 116 | self.verticalLayout_4.addLayout(self.horizontalLayout_3) 117 | MainWindow.setCentralWidget(self.centralwidget) 118 | self.menubar = QMenuBar(MainWindow) 119 | self.menubar.setGeometry(QRect(0, 0, 792, 23)) 120 | self.menubar.setObjectName("menubar") 121 | self.menuBattle = QMenu(self.menubar) 122 | self.menuBattle.setObjectName("menuBattle") 123 | self.menuRobot = QMenu(self.menubar) 124 | self.menuRobot.setObjectName("menuRobot") 125 | self.menuHelp = QMenu(self.menubar) 126 | self.menuHelp.setObjectName("menuHelp") 127 | MainWindow.setMenuBar(self.menubar) 128 | self.statusbar = QStatusBar(MainWindow) 129 | self.statusbar.setObjectName("statusbar") 130 | MainWindow.setStatusBar(self.statusbar) 131 | self.actionNew = QAction(MainWindow) 132 | self.actionNew.setObjectName("actionNew") 133 | self.actionNew_2 = QAction(MainWindow) 134 | self.actionNew_2.setObjectName("actionNew_2") 135 | self.actionOpen = QAction(MainWindow) 136 | self.actionOpen.setObjectName("actionOpen") 137 | self.actionClass_Reference = QAction(MainWindow) 138 | self.actionClass_Reference.setObjectName("actionClass_Reference") 139 | self.actionAbout = QAction(MainWindow) 140 | self.actionAbout.setObjectName("actionAbout") 141 | self.menuBattle.addAction(self.actionNew) 142 | self.menuRobot.addAction(self.actionNew_2) 143 | self.menuRobot.addAction(self.actionOpen) 144 | self.menuHelp.addSeparator() 145 | self.menuHelp.addAction(self.actionClass_Reference) 146 | self.menuHelp.addAction(self.actionAbout) 147 | self.menubar.addAction(self.menuBattle.menuAction()) 148 | self.menubar.addAction(self.menuRobot.menuAction()) 149 | self.menubar.addAction(self.menuHelp.menuAction()) 150 | 151 | self.retranslateUi(MainWindow) 152 | QMetaObject.connectSlotsByName(MainWindow) 153 | 154 | def retranslateUi(self, MainWindow): 155 | MainWindow.setWindowTitle(QApplication.translate("MainWindow", "Python Robocode")) 156 | self.tableWidget.setSortingEnabled(True) 157 | item = self.tableWidget.horizontalHeaderItem(0) 158 | item.setText(QApplication.translate("MainWindow", "Name")) 159 | item = self.tableWidget.horizontalHeaderItem(1) 160 | item.setText(QApplication.translate("MainWindow", "1st")) 161 | item = self.tableWidget.horizontalHeaderItem(2) 162 | item.setText(QApplication.translate("MainWindow", "2nd")) 163 | item = self.tableWidget.horizontalHeaderItem(3) 164 | item.setText(QApplication.translate("MainWindow", "3rd")) 165 | item = self.tableWidget.horizontalHeaderItem(4) 166 | item.setText(QApplication.translate("MainWindow", "Points")) 167 | self.pushButton.setText(QApplication.translate("MainWindow", "Start Last Battle")) 168 | self.label_2.setText(QApplication.translate("MainWindow", "Battle\'s Number")) 169 | self.label.setText(QApplication.translate("MainWindow", "Game Speed")) 170 | self.menuBattle.setTitle(QApplication.translate("MainWindow", "Battle")) 171 | self.menuRobot.setTitle(QApplication.translate("MainWindow", "Robot")) 172 | self.menuHelp.setTitle(QApplication.translate("MainWindow", "Help")) 173 | self.actionNew.setText(QApplication.translate("MainWindow", "New")) 174 | self.actionNew_2.setText(QApplication.translate("MainWindow", "New")) 175 | self.actionOpen.setText(QApplication.translate("MainWindow", "Open")) 176 | self.actionClass_Reference.setText(QApplication.translate("MainWindow", "Class Reference")) 177 | self.actionAbout.setText(QApplication.translate("MainWindow", "About")) 178 | 179 | 180 | if __name__ == "__main__": 181 | import sys 182 | app = QApplication(sys.argv) 183 | MainWindow = QMainWindow() 184 | ui = Ui_MainWindow() 185 | ui.setupUi(MainWindow) 186 | MainWindow.show() 187 | sys.exit(app.exec()) 188 | 189 | -------------------------------------------------------------------------------- /Python-Robocode/GUI/battle.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Dialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 572 10 | 399 11 | 12 | 13 | 14 | Battle Selection 15 | 16 | 17 | 18 | robotImages/smallgrey.pngrobotImages/smallgrey.png 19 | 20 | 21 | false 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | Qt::Horizontal 34 | 35 | 36 | 37 | 40 38 | 20 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 10 48 | 75 49 | true 50 | 51 | 52 | 53 | Available Bots: 54 | 55 | 56 | 57 | 58 | 59 | 60 | Qt::Horizontal 61 | 62 | 63 | 64 | 40 65 | 20 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 0 84 | 0 85 | 86 | 87 | 88 | >> 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 0 97 | 0 98 | 99 | 100 | 101 | << 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | Qt::Horizontal 115 | 116 | 117 | 118 | 40 119 | 20 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 10 129 | 75 130 | true 131 | 132 | 133 | 134 | Selected Bots 135 | 136 | 137 | 138 | 139 | 140 | 141 | Qt::Horizontal 142 | 143 | 144 | 145 | 40 146 | 20 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | Qt::Horizontal 168 | 169 | 170 | 171 | 40 172 | 20 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 10 182 | 75 183 | true 184 | 185 | 186 | 187 | Arena Size 188 | 189 | 190 | 191 | 192 | 193 | 194 | Qt::Horizontal 195 | 196 | 197 | 198 | 40 199 | 20 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | Qt::Horizontal 212 | 213 | 214 | 215 | 40 216 | 20 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | Width 227 | 228 | 229 | 230 | 231 | 232 | 233 | px 234 | 235 | 236 | 300 237 | 238 | 239 | 100000 240 | 241 | 242 | 700 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 10 251 | 75 252 | true 253 | 254 | 255 | 256 | X 257 | 258 | 259 | 260 | 261 | 262 | 263 | px 264 | 265 | 266 | 300 267 | 268 | 269 | 100000 270 | 271 | 272 | 500 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | Height 282 | 283 | 284 | 285 | 286 | 287 | 288 | Qt::Horizontal 289 | 290 | 291 | 292 | 40 293 | 20 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | Qt::Horizontal 308 | 309 | 310 | 311 | 40 312 | 20 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 0 322 | 0 323 | 324 | 325 | 326 | 327 | 150 328 | 0 329 | 330 | 331 | 332 | 333 | 75 334 | true 335 | 336 | 337 | 338 | -- Start -- 339 | 340 | 341 | 342 | 343 | 344 | 345 | Qt::Horizontal 346 | 347 | 348 | 349 | 40 350 | 20 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | -------------------------------------------------------------------------------- /Python-Robocode/Robots/T800.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from math import cos, sin, radians 5 | from robot import Robot # Import a base Robot 6 | import math 7 | 8 | # 9 | # Algorithm: 10 | # STATE_INIT: 11 | # Robot will first run away to nearest corner (C0X,C0Y), using his radar to search 12 | # the field to know initial enemies position. 13 | # STATE_RUN_AWAY (C1 and C2) 14 | # As soon as he knows one enemy position, he starts running away to furthest corners 15 | # and going back and forth between C1 and C2 corners (staying put is dangerous) 16 | # It will use his radar to continue scanning known enemy positions and record enemies moves. 17 | # it will update C1 and C2 corners when enemies move. 18 | # It will wait until the field hosts only one enemy and start shooting. 19 | # shooting stops when enemy moves to lower chances of bullet miss 20 | # shooting power is tuned with distance to enemy to maximize chances of rapid killing 21 | # 22 | # robot uses delays to fire when enemy doesn't move. 23 | # FIXME! 24 | # - could avoid running through enemy ? 25 | # - when there is only one opponent, could turn around the enemy and fire ! 26 | # - could team with same class robots to maximize win chances 27 | # 28 | 29 | MOVE_STEP = 10 30 | MOVE_LIMIT = 50 # never get closer than 30 from the walls (hitting wall loose health) 31 | 32 | STATE_INIT = 0 33 | STATE_RUNNING_C0 = 1 34 | STATE_RUNNING_C1 = 2 35 | STATE_RUNNING_C2 = 3 36 | 37 | 38 | class T800(Robot): # Create a Robot 39 | 40 | def init(self): # To initialise your robot 41 | 42 | # Set the bot color in RGB 43 | self.setColor(0, 255, 0) 44 | self.setGunColor(0, 255, 0) 45 | self.setRadarColor(255, 0, 0) 46 | self.setBulletsColor(0, 255, 0) 47 | 48 | # get game informations 49 | self.MapX = self.getMapSize().width() 50 | self.MapY = self.getMapSize().height() 51 | 52 | # initialiase some variables 53 | # self.move_step = MOVE_STEP 54 | self.state = STATE_INIT 55 | self.runcounter = 0 #used to record time based on game turns for our bot 56 | self.last_time = 0 #used to measure delays in "game turns" 57 | 58 | #these ugly variables keep track of corners of the gameplay we will travel to 59 | #repeatedly to never stay put. These are calculated based on other enemies position. 60 | self.C0X = -1 # will store destination C0 X we want to reach 61 | self.C0Y = -1 # will store destination C0 Y we want to reach 62 | self.C1X = -1 # will store destination C1 X we want to reach 63 | self.C1Y = -1 # will store destination C1 Y we want to reach 64 | self.C2X = -1 # will store destination C2 X we want to reach 65 | self.C2Y = -1 # will store destination C2 Y we want to reach 66 | 67 | self.radarVisible(True) # if True the radar field is visible 68 | self.lockRadar("gun") # might be "free","base" or "gun" 69 | self.radarGoingAngle = 5 # step angle for radar rotation 70 | self.lookingForBot = 0 # botId we are looking for 71 | self.angleMinBot = 0 # botId of further bot when radar rotating ccw 72 | self.angleMaxBot = 0 # botId of further bot when radar rotating cw 73 | 74 | # self.enemies is a list of existing opponents and their last known location 75 | # onTargetSpotted() is used to update enemy list and their position 76 | # sensor() is used to delete missing opponents (dead) 77 | self.enemies = {} 78 | 79 | def MyMove(self, step: int): 80 | # MyMove takes care of not loosing health by not hitting walls. 81 | 82 | angle = self.getHeading() # Returns the direction that the robot is facing 83 | position = self.getPosition() 84 | myX = position.x() 85 | myY = position.y() 86 | deltaY = step * cos(radians(angle)) 87 | deltaX = - step * sin(radians(angle)) 88 | 89 | move_ok = True 90 | 91 | if (deltaX > 0) and (myX + deltaX > self.MapX - MOVE_LIMIT): 92 | move_ok = False 93 | if (deltaX < 0) and (myX + deltaX < MOVE_LIMIT): 94 | move_ok = False 95 | if (deltaY > 0) and (myY + deltaY > self.MapY - MOVE_LIMIT): 96 | move_ok = False 97 | if (deltaY < 0) and (myY + deltaY < MOVE_LIMIT): 98 | move_ok = False 99 | 100 | if move_ok: 101 | self.move(step) 102 | else: 103 | # simulate wall hitting to launch appropriate actions 104 | self.rPrint("simulating wall hit, but stay calm, we stopped before !") 105 | self.onHitWall() 106 | 107 | def MyComputeDestAway(self): 108 | # compute enemy position center and deduce corners of gameplay we will run to. 109 | x = y = r = 0 110 | for robot in self.enemies: 111 | r += 1 112 | x += self.enemies[robot]["x"] 113 | y += self.enemies[robot]["y"] 114 | x = x // r 115 | y = y // r 116 | position = self.getPosition() 117 | myX = position.x() 118 | myY = position.y() 119 | 120 | if myX > x: 121 | self.C1X = self.MapX - MOVE_LIMIT * 1.5 122 | else: 123 | self.C1X = MOVE_LIMIT * 1.5 124 | if myY > y: 125 | self.C1Y = self.MapY - MOVE_LIMIT * 1.5 126 | else: 127 | self.C1Y = MOVE_LIMIT * 1.5 128 | 129 | if abs(self.C1X - x) > abs(self.C1Y - y): 130 | self.C2X = self.C1X 131 | self.C2Y = self.MapY - self.C1Y 132 | else: 133 | self.C2Y = self.C1Y 134 | self.C2X = self.MapX - self.C1X 135 | 136 | def MyGoto(self, x, y, step, urgency_flag) -> bool: 137 | """ 138 | MyGoto move the robot to coordinates x,y moving step by step 139 | if urgency_flag is True, robot will start moving immediately, 140 | otherwise it will first turn to the right direction first and then start moving ahead 141 | 142 | @type step: bool 143 | """ 144 | position = self.getPosition() 145 | myX = int(position.x()) 146 | myY = int(position.y()) 147 | 148 | # need MOVE_LIMIT precision 149 | x = x // MOVE_LIMIT 150 | y = y // MOVE_LIMIT 151 | myX = myX // MOVE_LIMIT 152 | myY = myY // MOVE_LIMIT 153 | 154 | if myX == x and myY == y: 155 | return True # arrived at destination 156 | 157 | angle = self.getHeading() % 360 # Returns the direction that the robot is facing 158 | 159 | new_angle = -1 160 | if x > myX and y > myY: new_angle = 315 161 | if x > myX and y < myY: new_angle = 225 162 | if x < myX and y < myY: new_angle = 135 163 | if x < myX and y > myY: new_angle = 45 164 | if x > myX and y == myY: new_angle = 270 165 | if x < myX and y == myY: new_angle = 90 166 | if x == myX and y < myY: new_angle = 180 167 | if x == myX and y > myY: new_angle = 0 168 | 169 | delta_angle = new_angle - angle 170 | 171 | #when turning on itself, the bot stays put and is an ideal target 172 | #we prefer running backward (reverse) and use a lower rotation angle when it makes sens 173 | if delta_angle > 90: 174 | delta_angle = delta_angle - 180 175 | step = - step 176 | if delta_angle < -90: 177 | delta_angle = delta_angle + 180 178 | step = - step 179 | 180 | if abs(delta_angle) > 5: 181 | turn_step = 5 182 | else: 183 | turn_step = 1 184 | 185 | if delta_angle < 0: 186 | turn_step = -turn_step 187 | self.turn(turn_step) 188 | if delta_angle > 0: 189 | self.turn(turn_step) 190 | if delta_angle == 0: 191 | pass 192 | 193 | #if moving is an emergency, start immediately 194 | #otherwise, wait until almost set to the right direction 195 | if urgency_flag or abs(delta_angle) < 30: 196 | self.MyMove(step) 197 | 198 | return False 199 | 200 | def MyComputeBotSearch(self, botSpotted): 201 | # when we know all enemies positions, we compute radar seeking range 202 | # based on known enemy positions to avoid scanning empty space. 203 | 204 | #angles will store all enemies position for us 205 | angles = {} 206 | 207 | e1 = len(self.getEnemiesLeft()) - 1 # we are counted in enemiesLeft !! 208 | e2 = len(self.enemies) 209 | if e1 == e2: 210 | # we know all enemies position, optimise radar moves 211 | pos = self.getPosition() 212 | 213 | my_radar_angle = self.getRadarHeading() % 360 214 | 215 | for botId in self.enemies: 216 | dx = self.enemies[botId]["x"] - pos.x() 217 | dy = self.enemies[botId]["y"] - pos.y() 218 | enemy_angle = math.degrees(math.atan2(dy, dx)) - 90 219 | a = enemy_angle - my_radar_angle 220 | if a < -180: 221 | a += 360 222 | elif 180 < a: 223 | a -= 360 224 | angles[a] = botId 225 | 226 | amin = min(angles.keys()) 227 | amax = max(angles.keys()) 228 | self.angleMinBot = angles[amin] 229 | self.angleMaxBot = angles[amax] 230 | 231 | if len(self.enemies) == 1: # tracking single bot mode 232 | if amin > 0: 233 | self.radarGoingAngle = min([5, amin]) 234 | elif amin < 0: 235 | self.radarGoingAngle = -min([5, -amin]) 236 | else: 237 | self.radarGoingAngle = 1 238 | 239 | #called with some bot spotted ! try to shoot ... 240 | if botSpotted!=0 and abs(self.radarGoingAngle)<1 and self.runcounter>self.last_time: 241 | dx=self.enemies[angles[amin]]["x"]-pos.x() 242 | dy=self.enemies[angles[amin]]["y"]-pos.y() 243 | dist=math.sqrt(dx**2+dy**2) 244 | 245 | if self.runcounter-self.enemies[angles[amin]]["move"] > 2: #shoot only if not moving since 2 rounds 246 | #fire with more power if near the robot, lower power with distance 247 | self.fire(int(1000/dist)+1) 248 | #slow down fire rate with distance 249 | self.last_time=self.runcounter+int(dist/150) 250 | 251 | elif self.lookingForBot == botSpotted: 252 | #bot spotted is the one we were looking for. 253 | #there are multiple enemies, go back and forth between enemies 254 | 255 | if self.lookingForBot == self.angleMinBot: 256 | self.lookingForBot = self.angleMaxBot 257 | if (self.radarGoingAngle<0): 258 | self.radarGoingAngle = -self.radarGoingAngle 259 | else: 260 | self.lookingForBot = self.angleMinBot 261 | if (self.radarGoingAngle>0): 262 | self.radarGoingAngle = -self.radarGoingAngle 263 | 264 | elif self.lookingForBot not in self.enemies: 265 | # lookingForBot not defined or lookingForBot is dead now 266 | # start seeking another one 267 | if self.radarGoingAngle > 0: 268 | self.lookingForBot = self.angleMaxBot 269 | # self.radarGoingAngle = (amax / abs(amax)) * min([5, abs(amax)]) 270 | else: 271 | self.lookingForBot = self.angleMinBot 272 | # self.radarGoingAngle = (amin / abs(amin)) * min([5, abs(amin)]) 273 | 274 | def run(self): # main loop to command the bot 275 | self.runcounter += 1 276 | if self.state == STATE_INIT: 277 | position = self.getPosition() 278 | myX = position.x() 279 | myY = position.y() 280 | if myX < self.MapX//2: 281 | self.C0X = MOVE_LIMIT 282 | self.radarGoingAngle = -5 # step angle for radar rotation 283 | else: 284 | self.C0X = self.MapX - MOVE_LIMIT 285 | if myY < self.MapY//2: 286 | self.C0Y = MOVE_LIMIT 287 | else: 288 | self.C0Y = self.MapY - MOVE_LIMIT 289 | 290 | self.setRadarField("round") # might be "normal", "large, "thin", "round" 291 | self.state = STATE_RUNNING_C0 292 | self.MyGoto(self.C0X, self.C0Y, MOVE_STEP, True) 293 | 294 | if self.state == STATE_RUNNING_C0: 295 | if self.runcounter > self.last_time + 5: #change after first 5 rounds 296 | self.setRadarField("thin") # might be "normal", "large, "thin", "round" 297 | self.MyComputeBotSearch(0) 298 | self.gunTurn(self.radarGoingAngle) 299 | self.MyGoto(self.C0X, self.C0Y, MOVE_STEP, True) 300 | if self.C1X != -1: 301 | self.state = STATE_RUNNING_C1 302 | 303 | if self.state == STATE_RUNNING_C1: 304 | self.setRadarField("thin") # might be "normal", "large, "thin", "round" 305 | self.MyComputeBotSearch(0) 306 | self.gunTurn(self.radarGoingAngle) 307 | if self.MyGoto(self.C1X, self.C1Y, MOVE_STEP, False): 308 | self.state = STATE_RUNNING_C2 309 | 310 | if self.state == STATE_RUNNING_C2: 311 | self.setRadarField("thin") # might be "normal", "large, "thin", "round" 312 | self.MyComputeBotSearch(0) 313 | self.gunTurn(self.radarGoingAngle) 314 | if self.MyGoto(self.C2X, self.C2Y, MOVE_STEP, False): 315 | self.state = STATE_RUNNING_C1 316 | 317 | def onHitWall(self): 318 | self.rPrint("ouch! a wall !") 319 | 320 | def sensors(self): 321 | # get rid of dead oppponents in our tracking list 322 | list = self.getEnemiesLeft() # return a list of the enemies alive in the battle 323 | alive = [] 324 | for robot in list: 325 | alive.append(robot["id"]) 326 | missing = [] 327 | for robot in self.enemies: 328 | if robot not in alive: 329 | missing.append(robot) 330 | for robot in missing: 331 | del self.enemies[robot] 332 | 333 | 334 | def onRobotHit(self, robotId, robotName): # when My bot hit another 335 | pass 336 | 337 | def onHitByRobot(self, robotId, robotName): 338 | pass 339 | 340 | def onHitByBullet(self, bulletBotId, bulletBotName, bulletPower): # NECESARY FOR THE GAME 341 | pass 342 | 343 | def onBulletHit(self, botId, bulletId): # NECESARY FOR THE GAME 344 | pass 345 | 346 | def onBulletMiss(self, bulletId): 347 | pass 348 | 349 | def onRobotDeath(self): 350 | pass 351 | 352 | def onTargetSpotted(self, botId, botName, botPos): # NECESARY FOR THE GAME 353 | #keep a list of spotted enemies with enemy coordinates recorded. 354 | #compute destination if new bot spotted or if known bot is moving 355 | # store x and y position and "date" of last seen (runcounter) 356 | if botId not in self.enemies: 357 | self.enemies[botId] = {} 358 | self.enemies[botId]["x"] = botPos.x() 359 | self.enemies[botId]["y"] = botPos.y() 360 | self.enemies[botId]["move"] = self.runcounter 361 | self.MyComputeDestAway() 362 | else: 363 | if self.enemies[botId]["x"] != botPos.x() or self.enemies[botId]["y"] != botPos.y(): 364 | self.enemies[botId]["x"] = botPos.x() 365 | self.enemies[botId]["y"] = botPos.y() 366 | self.enemies[botId]["move"] = self.runcounter 367 | self.MyComputeDestAway() 368 | 369 | #compute radar next moves with information of botId currently aimed 370 | self.MyComputeBotSearch(botId) 371 | -------------------------------------------------------------------------------- /Python-Robocode/Objects/robot.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | 4 | import time, os, math 5 | import traceback 6 | 7 | from PyQt6.QtWidgets import QGraphicsItemGroup, QGraphicsPixmapItem, QGraphicsRectItem 8 | from PyQt6.QtGui import QPixmap, QColor, QPainter, QIcon 9 | from PyQt6.QtCore import QPointF, Qt 10 | 11 | from physics import physics 12 | from bullet import Bullet 13 | from radarField import radarField 14 | from animation import animation 15 | 16 | class Robot(QGraphicsItemGroup): 17 | 18 | def __init__(self, mapSize, parent, repr): 19 | QGraphicsItemGroup.__init__(self) 20 | #Attributes 21 | self.__mapSize = mapSize 22 | self.__parent = parent 23 | self.__health = 100 24 | self.__repr = repr 25 | self.__gunLock = "free" 26 | self.__radarLock = "Free" 27 | 28 | self.info = None # RobotInfo (should be set in window.py::addRobotInfo()) 29 | 30 | #animation 31 | self.__runAnimation = animation("run") 32 | self.__targetAnimation = animation("target") 33 | self.__physics = physics(self.__runAnimation) 34 | 35 | 36 | #graphics 37 | self.maskColor = QColor(0, 255, 255) 38 | self.gunMaskColor = QColor(0, 255, 255) 39 | self.radarMaskColor = QColor(0, 255, 255) 40 | 41 | #load img 42 | self.__base = QGraphicsPixmapItem() 43 | self.__base.pixmap = QPixmap(os.getcwd() + "/robotImages/baseGrey.png") 44 | self.__base.setPixmap(self.__base.pixmap) 45 | self.addToGroup(self.__base) 46 | self.__baseWidth = self.__base.boundingRect().width() 47 | self.__baseHeight = self.__base.boundingRect().height() 48 | 49 | #load gun img 50 | self.__gun = QGraphicsPixmapItem() 51 | self.__gun.pixmap = QPixmap(os.getcwd() + "/robotImages/gunGrey.png") 52 | self.__gun.setPixmap(self.__gun.pixmap) 53 | self.addToGroup(self.__gun) 54 | self.__gunWidth = self.__gun.boundingRect().width() 55 | self.__gunHeight = self.__gun.boundingRect().height() 56 | #gun position 57 | x = self.__base.boundingRect().center().x() 58 | y = self.__base.boundingRect().center().y() 59 | self.__gun.setPos(x - self.__gunWidth/2.0 , y - self.__gunHeight /2.0) 60 | 61 | #load radar img 62 | self.__radar = QGraphicsPixmapItem() 63 | self.__radar.pixmap = QPixmap(os.getcwd() + "/robotImages/radar.png") 64 | self.__radar.setPixmap(self.__radar.pixmap) 65 | self.addToGroup(self.__radar) 66 | self.__radarWidth = self.__radar.boundingRect().width() 67 | self.__radarHeight = self.__radar.boundingRect().height() 68 | #radar position 69 | self.__radar.setPos(x - self.__radarWidth/2.0 , y - self.__radarHeight /2.0) 70 | 71 | #load radarField 72 | firstPoint = QPointF(x - self.__radarWidth/2, y) 73 | secondPoint = QPointF(x + self.__radarWidth/2, y) 74 | thirdPoint = QPointF(x + 4*self.__radarWidth, y + 700) 75 | fourthPoint = QPointF(x - 4*self.__radarWidth, y+ 700) 76 | qPointListe = [] 77 | qPointListe.append(firstPoint) 78 | qPointListe.append(secondPoint) 79 | qPointListe.append(thirdPoint) 80 | qPointListe.append(fourthPoint) 81 | self.__radarField = radarField(qPointListe, self, "poly") 82 | 83 | #__largeRadarField 84 | qPointListe.remove(fourthPoint) 85 | qPointListe.remove(thirdPoint) 86 | thirdPoint = QPointF(x + 10*self.__radarWidth, y + 400) 87 | fourthPoint = QPointF(x - 10*self.__radarWidth, y+ 400) 88 | qPointListe.append(thirdPoint) 89 | qPointListe.append(fourthPoint) 90 | self.__largeRadarField = radarField(qPointListe, self, "poly") 91 | 92 | #thinRadarField 93 | qPointListe.remove(fourthPoint) 94 | qPointListe.remove(thirdPoint) 95 | thirdPoint = QPointF(x + 0.4*self.__radarWidth, y + 900) 96 | fourthPoint = QPointF(x - 0.4*self.__radarWidth, y+ 900) 97 | qPointListe.append(thirdPoint) 98 | qPointListe.append(fourthPoint) 99 | self.__thinRadarField = radarField(qPointListe, self, "poly") 100 | 101 | #roundRadarField 102 | self.__roundRadarField = radarField([0, 0, 300, 300], self, "round") 103 | self.addToGroup(self.__roundRadarField) 104 | self.__roundRadarField.setPos(x - self.__roundRadarField.boundingRect().width()/2.0 , y - self.__roundRadarField.boundingRect().height() /2.0) 105 | 106 | #add to group 107 | self.addToGroup(self.__radarField) 108 | self.addToGroup(self.__largeRadarField) 109 | self.addToGroup(self.__thinRadarField) 110 | 111 | self.__largeRadarField.hide() 112 | self.__thinRadarField.hide() 113 | self.__roundRadarField.hide() 114 | 115 | 116 | 117 | #Set the bot color in RGB 118 | self.setColor(0, 200, 100) 119 | self.setGunColor(0, 200, 100) 120 | self.setRadarColor(0, 200, 100) 121 | self.setBulletsColor(0, 200, 100) 122 | 123 | #set the Origin point for Transformation: 124 | #radarField 125 | self.__radarField.setTransformOriginPoint(x, y) 126 | self.__largeRadarField.setTransformOriginPoint(x, y) 127 | self.__thinRadarField.setTransformOriginPoint(x, y) 128 | #base 129 | x = self.__baseWidth/2 130 | y = self.__baseHeight/2 131 | self.__base.setTransformOriginPoint(x, y) 132 | #gun 133 | x = self.__gunWidth/2 134 | y = self.__gunHeight /2 135 | self.__gun.setTransformOriginPoint(x, y) 136 | #radar 137 | x = self.__radarWidth/2 138 | y = self.__radarHeight /2 139 | self.__radar.setTransformOriginPoint(x, y) 140 | 141 | 142 | #add self items in items to avoid collisions 143 | self.__items = set([self, self.__base, self.__gun, self.__radar, self.__radarField, self.__largeRadarField, self.__thinRadarField, self.__roundRadarField]) 144 | 145 | #init the subclassed Bot 146 | self.init() 147 | 148 | self.__currentAnimation = [] 149 | 150 | #self.a = time.time() 151 | 152 | 153 | def advance(self, i): 154 | """ 155 | if i ==1: 156 | print(time.time() - self.a) 157 | self.a = time.time() 158 | """ 159 | if self.__health <= 0: 160 | self.__death() 161 | 162 | if self.__currentAnimation == []: 163 | try: 164 | self.__currentAnimation = self.__physics.animation.list.pop() 165 | 166 | except IndexError: 167 | if self.__physics.animation.name == "target": 168 | try: 169 | self.__physics.animation = self.__runAnimation 170 | self.__currentAnimation = self.__physics.animation.list.pop() 171 | except IndexError: 172 | pass 173 | else: 174 | self.stop() 175 | try: 176 | self.run() 177 | except: 178 | traceback.print_exc() 179 | exit(-1) 180 | self.__physics.reverse() 181 | try: 182 | self.__currentAnimation = self.__physics.animation.list.pop() 183 | except: 184 | pass 185 | 186 | if i ==1: 187 | try: 188 | command = self.__currentAnimation.pop() #load animation 189 | 190 | #translation 191 | dx, dy= self.__getTranslation(command["move"]) 192 | self.setPos(dx, dy) 193 | #rotation 194 | angle = self.__getRotation(command["turn"]) 195 | self.__base.setRotation(angle) 196 | if self.__gunLock.lower() == 'base': 197 | self.__gun.setRotation(angle) 198 | if self.__radarLock.lower() == 'base': 199 | self.__setRadarRotation(angle) 200 | #gun Rotation 201 | angle = self.__getGunRotation(command["gunTurn"]) 202 | self.__gun.setRotation(angle) 203 | if self.__radarLock.lower() == 'gun': 204 | self.__setRadarRotation(angle) 205 | #radar Rotation 206 | angle = self.__getRadarRotation(command["radarTurn"]) 207 | self.__setRadarRotation(angle) 208 | #asynchronous fire 209 | if command["fire"] != 0: 210 | self.makeBullet(command["fire"] ) 211 | except: 212 | pass 213 | 214 | else: 215 | 216 | self.sensors() 217 | 218 | 219 | #collisions 220 | for item in set(self.__base.collidingItems(Qt.ItemSelectionMode.IntersectsItemShape)) - self.__items: 221 | if isinstance(item, QGraphicsRectItem): 222 | #wall Collision 223 | self.__wallRebound(item) 224 | elif isinstance(item, Robot): 225 | if item.__base.collidesWithItem(self.__base): 226 | #robot Collision 227 | self.__robotRebound(item) 228 | elif isinstance(item, Bullet): 229 | #bullet colision 230 | self.__bulletRebound(item) 231 | elif isinstance(item, radarField): 232 | if item.robot.__physics.animation.name != "target": 233 | #targetSpotted 234 | self.__targetSeen(item) 235 | 236 | ### THESE ARE THE FUNCTIONS ACCESSABLE FROM OUTSIDE ### 237 | 238 | #-----------------------------------------------------------Gun------------------------------------------------------ 239 | def gunTurn(self, angle): 240 | s = 1 241 | if angle < 0: 242 | s = -1 243 | steps = int(s*angle/self.__physics.step) 244 | a = angle%self.__physics.step 245 | if a != 0: 246 | self.__physics.gunTurn.append(s*a) 247 | for i in range(steps): 248 | self.__physics.gunTurn.append(s*self.__physics.step) 249 | 250 | def lockGun(self, part): 251 | self.__gunLock = part 252 | 253 | def setGunColor(self, r, g, b): 254 | color = QColor(r, g, b) 255 | mask = self.__gun.pixmap.createMaskFromColor(self.gunMaskColor, Qt.MaskMode.MaskOutColor) 256 | p = QPainter(self.__gun.pixmap) 257 | p.setPen(QColor(r, g, b)) 258 | p.drawPixmap(self.__gun.pixmap.rect(), mask, mask.rect()) 259 | p.end() 260 | self.__gun.setPixmap(self.__gun.pixmap) 261 | self.gunMaskColor = QColor(r, g, b) 262 | 263 | #----------------------------------------------------------Base----------------------------------------------------- 264 | 265 | def move(self, distance): 266 | s = 1 267 | if distance < 0: 268 | s = -1 269 | steps = int(s*distance/self.__physics.step) 270 | d = distance%self.__physics.step 271 | if d != 0: 272 | self.__physics.move.append(s*d) 273 | for i in range(steps): 274 | self.__physics.move.append(s*self.__physics.step) 275 | 276 | def turn(self, angle): 277 | s = 1 278 | if angle < 0: 279 | s = -1 280 | steps = int(s*angle/self.__physics.step) 281 | a = angle%self.__physics.step 282 | if a != 0: 283 | self.__physics.turn.append(s*a) 284 | for i in range(steps): 285 | self.__physics.turn.append(s*self.__physics.step) 286 | 287 | def setColor(self, r, g, b): 288 | color = QColor(r, g, b) 289 | mask = self.__base.pixmap.createMaskFromColor(self.maskColor, Qt.MaskMode.MaskOutColor) 290 | p = QPainter(self.__base.pixmap) 291 | p.setPen(QColor(r, g, b)) 292 | p.drawPixmap(self.__base.pixmap.rect(), mask, mask.rect()) 293 | p.end() 294 | self.__base.setPixmap(self.__base.pixmap) 295 | self.maskColor = QColor(r, g, b) 296 | 297 | #---------------------------------------------RADAR------------------------------------------------ 298 | 299 | def setRadarField(self, form): 300 | if form.lower() == "normal": 301 | self.__radarField.show() 302 | self.__largeRadarField.hide() 303 | self.__thinRadarField.hide() 304 | self.__roundRadarField.hide() 305 | if form.lower() == "large": 306 | self.__radarField.hide() 307 | self.__largeRadarField.show() 308 | self.__thinRadarField.hide() 309 | self.__roundRadarField.hide() 310 | if form.lower() == "thin": 311 | self.__radarField.hide() 312 | self.__largeRadarField.hide() 313 | self.__thinRadarField.show() 314 | self.__roundRadarField.hide() 315 | if form.lower() == "round": 316 | self.__radarField.hide() 317 | self.__largeRadarField.hide() 318 | self.__thinRadarField.hide() 319 | self.__roundRadarField.show() 320 | 321 | 322 | def lockRadar(self, part): 323 | self.__radarLock = part 324 | 325 | def radarTurn(self, angle): 326 | s = 1 327 | if angle < 0: 328 | s = -1 329 | steps = int(s*angle/self.__physics.step) 330 | a = angle%self.__physics.step 331 | if a != 0: 332 | self.__physics.radarTurn.append(s*a) 333 | for i in range(steps): 334 | self.__physics.radarTurn.append(s*self.__physics.step) 335 | 336 | def setRadarColor(self, r, g, b): 337 | color = QColor(r, g, b) 338 | mask = self.__radar.pixmap.createMaskFromColor(self.radarMaskColor, Qt.MaskMode.MaskOutColor) 339 | p = QPainter(self.__radar.pixmap) 340 | p.setPen(QColor(r, g, b)) 341 | p.drawPixmap(self.__radar.pixmap.rect(), mask, mask.rect()) 342 | p.end() 343 | self.__radar.setPixmap(self.__radar.pixmap) 344 | self.radarMaskColor = QColor(r, g, b) 345 | 346 | def radarVisible(self, bol): 347 | self.__radarField.setVisible(bol) 348 | self.__roundRadarField.setVisible(bol) 349 | self.__thinRadarField.setVisible(bol) 350 | self.__largeRadarField.setVisible(bol) 351 | 352 | #------------------------------------------------Bullets--------------------------------------- 353 | 354 | def fire(self, power): 355 | #asynchronous fire 356 | self.stop() 357 | bullet = Bullet(power, self.bulletColor, self) 358 | self.__physics.fire.append(bullet) 359 | self.__items.add(bullet) 360 | self.__parent.addItem(bullet) 361 | bullet.hide() 362 | return id(bullet) 363 | 364 | def makeBullet(self, bullet): 365 | bullet.show() 366 | pos = self.pos() 367 | angle = self.__gun.rotation() 368 | #to find the initial position 369 | x = pos.x() + self.__baseWidth/2.0 370 | y = pos.y() + self.__baseHeight/2.0 371 | dx = - math.sin(math.radians(angle))*self.__gunWidth/2.0 372 | dy = math.cos(math.radians(angle))*self.__gunHeight/2.0 373 | pos.setX(x+dx) 374 | pos.setY(y+dy) 375 | bot = self 376 | bullet.init(pos, angle, self.__parent) 377 | 378 | self.__changeHealth(self, -bullet.power) 379 | return id(bullet) 380 | 381 | def setBulletsColor(self, r, g, b): 382 | self.bulletColor = QColor(r, g, b) 383 | 384 | #---------------------------------------General Methods--------------------------------------- 385 | def stop(self): 386 | self.__physics.newAnimation() 387 | 388 | def getMapSize(self): 389 | return self.__mapSize 390 | 391 | def getPosition(self): 392 | p = self.pos() 393 | r = self.__base.boundingRect() 394 | return QPointF(p.x() + r.width()/2, p.y()+r.height()/2) 395 | 396 | def getGunHeading(self): 397 | angle = self.__gun.rotation() 398 | # if angle > 360: 399 | # a = int(angle) / 360 400 | # angle = angle - (360*a) 401 | return angle 402 | 403 | def getHeading(self): 404 | return self.__base.rotation() 405 | 406 | def getRadarHeading(self): 407 | return self.__radar.rotation() 408 | 409 | def reset(self): 410 | self.__physics.reset() 411 | self.__currentAnimation = [] 412 | 413 | def getEnemiesLeft(self): 414 | l = [] 415 | for bot in self.__parent.aliveBots: 416 | dic = {"id":id(bot), "name":bot.__repr__()} 417 | l.append(dic) 418 | return l 419 | 420 | def rPrint(self, msg): 421 | if self.info is not None: 422 | self.info.out.add(str(msg)) 423 | 424 | def pause(self, duration): 425 | self.stop() 426 | for i in range(int(duration)): 427 | self.__physics.move.append(0) 428 | self.stop() 429 | ###end of functions accessable from outside### 430 | 431 | # Calculus & Private Methods 432 | def __getTranslation(self, step): 433 | angle = self.__base.rotation() 434 | pos = self.pos() 435 | x = pos.x() 436 | y = pos.y() 437 | dx = - math.sin(math.radians(angle))*step 438 | dy = math.cos(math.radians(angle))*step 439 | # print(dx, dy) 440 | return x+dx, y+dy 441 | 442 | def __setRadarRotation(self, angle): 443 | self.__radar.setRotation(angle) 444 | self.__radarField.setRotation(angle) 445 | self.__largeRadarField.setRotation(angle) 446 | self.__thinRadarField.setRotation(angle) 447 | 448 | def __getRotation(self, alpha): 449 | return self.__base.rotation() + alpha 450 | 451 | def __getGunRotation(self, alpha): 452 | return self.__gun.rotation() + alpha 453 | 454 | def __getRadarRotation(self, alpha): 455 | return self.__radar.rotation() + alpha 456 | 457 | def __wallRebound(self, item): 458 | self.reset() 459 | if item.name == 'left': 460 | x = self.__physics.step*1.1 461 | y = 0 462 | elif item.name == 'right': 463 | x = - self.__physics.step*1.1 464 | y = 0 465 | elif item.name == 'top': 466 | x = 0 467 | y = self.__physics.step*1.1 468 | elif item.name == 'bottom': 469 | x = 0 470 | y = - self.__physics.step*1.1 471 | self.setPos(self.pos().x() + x, self.pos().y() + y) 472 | self.__changeHealth(self, -1) 473 | self.stop() 474 | try: 475 | self.onHitWall() 476 | except: 477 | traceback.print_exc() 478 | exit(-1) 479 | animation = self.__physics.makeAnimation() 480 | if animation != []: 481 | self.__currentAnimation = animation 482 | 483 | 484 | 485 | def __robotRebound(self, robot): 486 | try: 487 | self.reset() 488 | robot.reset() 489 | angle = self.__base.rotation() 490 | pos = self.pos() 491 | x = pos.x() 492 | y = pos.y() 493 | dx = - math.sin(math.radians(angle))*self.__physics.step*1.1 494 | dy = math.cos(math.radians(angle))*self.__physics.step*1.1 495 | self.setPos(x-dx, y-dy) 496 | pos = robot.pos() 497 | x = pos.x() 498 | y = pos.y() 499 | robot.setPos(x+dx, y+dy) 500 | self.__changeHealth(robot, -1) 501 | self.__changeHealth(self, -1) 502 | self.stop() 503 | self.onRobotHit(id(robot), robot.__repr__()) 504 | animation = self.__physics.makeAnimation() 505 | if animation != []: 506 | self.__currentAnimation = animation 507 | robot.stop() 508 | robot.onHitByRobot(id(self), self.__repr__()) 509 | animation = robot.__physics.makeAnimation() 510 | if animation != []: 511 | robot.__currentAnimation = animation 512 | except: 513 | traceback.print_exc() 514 | exit(-1) 515 | 516 | 517 | 518 | def __bulletRebound(self, bullet): 519 | self.__changeHealth(self, - 3*bullet.power) 520 | try: 521 | if bullet.robot in self.__parent.aliveBots: 522 | self.__changeHealth(bullet.robot, 2*bullet.power) 523 | self.stop() 524 | self.onHitByBullet(id(bullet.robot), bullet.robot.__repr__(), bullet.power) 525 | animation = self.__physics.makeAnimation() 526 | if animation != []: 527 | self.__currentAnimation = animation 528 | bullet.robot.stop() 529 | bullet.robot.onBulletHit(id(self), id(bullet)) 530 | animation = bullet.robot.__physics.makeAnimation() 531 | if animation != []: 532 | bullet.robot.__currentAnimation = animation 533 | self.__parent.removeItem(bullet) 534 | except: 535 | pass 536 | 537 | 538 | def __targetSeen(self, target): 539 | self.stop() 540 | anim = target.robot.__currentAnimation 541 | target.robot.__physics.animation = target.robot.__targetAnimation 542 | target.robot.__physics.reset() 543 | try: 544 | target.robot.onTargetSpotted(id(self), self.__repr__(), self.getPosition()) 545 | except: 546 | traceback.print_exc() 547 | exit(-1) 548 | target.robot.__physics.newAnimation() 549 | target.robot.__physics.reverse() 550 | try: 551 | target.robot.__currentAnimation = target.robot.__physics.animation.list.pop() 552 | except: 553 | target.robot.__physics.animation = target.robot.__runAnimation 554 | target.robot.__currentAnimation = anim 555 | 556 | 557 | def __changeHealth(self, bot, value): 558 | if bot.__health + value>=100: 559 | bot.__health = 100 560 | else: 561 | bot.__health = bot.__health + value 562 | try: 563 | bot.progressBar.setValue(bot.__health) 564 | except: 565 | pass 566 | 567 | def removeMyProtectedItem(self, item): 568 | self.__items.remove(item) 569 | 570 | def __death(self): 571 | 572 | try: 573 | self.icon.setIcon(QIcon(os.getcwd() + "/robotImages/dead.png")) 574 | self.icon2.setIcon(QIcon(os.getcwd() + "/robotImages/dead.png")) 575 | self.progressBar.setValue(0) 576 | except : 577 | pass 578 | self.__parent.deadBots.append(self) 579 | self.__parent.aliveBots.remove(self) 580 | try: 581 | self.onRobotDeath() 582 | except: 583 | traceback.print_exc() 584 | exit(-1) 585 | self.__parent.removeItem(self) 586 | if len(self.__parent.aliveBots) <= 1: 587 | self.__parent.battleFinished() 588 | 589 | def __repr__(self): 590 | repr = self.__repr.split(".") 591 | return repr[1].replace("'>", "") 592 | --------------------------------------------------------------------------------