diff --git a/Classes/Ball.py b/Classes/Ball.py index 351f34a..39a3a53 100644 --- a/Classes/Ball.py +++ b/Classes/Ball.py @@ -21,20 +21,5 @@ def move(self): def setSpeed(self,v): self.v = v - - def getSpeed(self): - """Returns the speed of the ball as a list, index 0 is the velocity of the ball while index 1 is the direction of the ball (in degrees). - \nIf you want the velocity in one direction make sure you use math.sin or math.cos.\nTo get the x speed use math.sin, its weird but its correct""" - return self.v - - def getXSpeed(self): - return self.v[0]*math.sin(math.radians(self.v[1])) - - def getYSpeed(self): - return self.v[0]*math.cos(math.radians(self.v[1])) - - def resetToMiddle(self): - self.xpos = WIDTH //2 - self.ypos = HEIGHT //2 - + return self.v \ No newline at end of file diff --git a/Classes/Globals.py b/Classes/Globals.py index cc4a288..7a33298 100644 --- a/Classes/Globals.py +++ b/Classes/Globals.py @@ -19,7 +19,6 @@ GREY = (128,128,128) TURQUOISE = (64,224,208) -score = [0, 0] # index 0 is red team and index 1 is blue team @@ -54,10 +53,7 @@ def draw_robots(win, robot_list): def draw(win, robot_list): win.fill(WHITE) draw_robots(win, robot_list) - draw_score(win, score) pygame.display.update() - - def updateBall(robot, ball): if robot.hasball: @@ -84,58 +80,7 @@ def algorithm(robot2, robot3, ball, goal): if getRBD(robot2, goal)+1>=robot2.getDegree()>=getRBD(robot2, goal)-1: robot2.throwBall() -def control(robot, event, ball): - if event.type == pygame.TEXTINPUT: - if event.text =="r": - robot.up() - elif event.text == "s": - robot.down() - if event.text == "y": - robot.left() - elif event.text == "t": - robot.right() - if event.text == "a": - robot.turnLeft() - print(getBallDist(robot, ball)) - elif event.text == "d": - robot.turnRight() - if event.text == "w": - robot.foreward() - if event.text == "q": - if getBallDist(robot, ball)< 5: - robot.grabBall(ball) - elif event.text == "x": - robot.throwBall() - updateBall(robot, ball) #print(robot.front[2]) - -def draw_score(window, score): - # create a font object. - # 1st parameter is the font file - # which is present in pygame. - # 2nd parameter is size of the font - font = pygame.font.Font('freesansbold.ttf', 32) - - # create a text surface object, - # on which text is drawn on it. - textRed = font.render(f'Red Team: {score[0]}', True, RED, WHITE) - textBlue = font.render(f'Blue Team: {score[1]}', True, BLUE, WHITE) - - - # create a rectangular object for the - # text surface object - textRedRect = textRed.get_rect() - textRedRect.center = (WIDTH // 2 - 150, 100) - - textBlueRect = textBlue.get_rect() - textBlueRect.center = (WIDTH // 2 + 150, 100) - - # copying the text surface object - # to the display surface object - # at the center coordinate. - WIN.blit(textRed, textRedRect) - WIN.blit(textBlue, textBlueRect) - -def resetScore(): - score = [0,0] \ No newline at end of file + #if event.type == pygame.KEYUP: + # if diff --git a/Classes/Goal.py b/Classes/Goal.py index b60c7b0..c834759 100644 --- a/Classes/Goal.py +++ b/Classes/Goal.py @@ -1,7 +1,5 @@ import pygame -from Classes.Ball import Ball # you can also import * -from Classes.Globals import * class Goal: def __init__(self, xpos, ypos, width, height, team, color): self.xpos = xpos-(width/2) @@ -13,40 +11,4 @@ def __init__(self, xpos, ypos, width, height, team, color): def draw(self,win): #not tested - pygame.draw.rect(win, self.color, (self.xpos, self.ypos, self.width,self.height)) - - def scoreGoal(self, ball: Ball) -> bool: - # if goal is on the left - if self.xpos < 400: - # if inside the net - # ball is on the left side of the net AND ball is below the top of the net AND ball is also above the bottom of the net - if (ball.xpos <= self.xpos + self.width) and (ball.ypos > self.ypos and ball.ypos < self.ypos + self.height): - # if ball is going towards the net - # idk if it should be less than or EQUAL to but why not - if ball.getXSpeed() <= 0: - score [1] +=1 - ball.resetToMiddle() - ball.setSpeed([0,0]) - return True - - - # if goal is on the right - else: - - # if inside the net - # if ball is on the right side of the net AND ball is below the top of the net AND ball is also above the bottom of the net - if (ball.xpos >= self.xpos) and (ball.ypos > self.ypos and ball.ypos < self.ypos + self.height): - # idk if it should be greater than or EQUAL to but why not - if ball.getXSpeed() >= 0: - score [0] +=1 - ball.resetToMiddle() - ball.setSpeed([0,0]) - return True - return False - - - - - - - + pygame.draw.rect(win, self.color, (self.xpos, self.ypos, self.width,self.height)) \ No newline at end of file diff --git a/Classes/Robot.py b/Classes/Robot.py index 15ecee6..6906ccf 100644 --- a/Classes/Robot.py +++ b/Classes/Robot.py @@ -1,7 +1,8 @@ import pygame import math +from Classes.Globals import * # to import all global variables class Robot: - def __init__(self, speed,turn_speed, xpos, ypos, color,radius): + def __init__(self, speed,turn_speed, xpos, ypos, color,radius, directions): self.turn_speed = turn_speed #deg per frame self.xpos = xpos self.ypos = ypos @@ -14,7 +15,8 @@ def __init__(self, speed,turn_speed, xpos, ypos, color,radius): self.accel = 1 self.front = [xpos,ypos+radius, 0]#initially looks down at 0 deg self.power = 0 - + self.directions = directions + def get_turn_speed(self): return self.turn_speed def up(self): @@ -78,5 +80,70 @@ def draw(self, win): #try give claw to indicate front #Spygame.draw.rect(win, self.color,()) + def control(robot, event, ball): # control checks which keys are pressed and updates the robot's movement booleans + # 0 forward + # 1 backward + # 2 left + # 3 right + # 4 turn left + # 5 turn right + # 6 up + # 7 down + # F and B are respective to the front of the robot, while all others are headless + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_f: + robot.directions[0] = True + elif event.key == pygame.K_b: + robot.directions[1] = True + if event.key == pygame.K_a: + robot.directions[2] = True + elif event.key == pygame.K_d: + robot.directions[3] = True + if event.key == pygame.K_z: + robot.directions[4] = True + elif event.key == pygame.K_x: + robot.directions[5] = True + if event.key == pygame.K_w: + robot.directions[6] = True + elif event.key == pygame.K_s: + robot.directions[7] = True + if event.type == pygame.KEYUP: + if event.key == pygame.K_f: + robot.directions[0] = False + elif event.key == pygame.K_b: + robot.directions[1] = False + if event.key == pygame.K_a: + robot.directions[2] = False + elif event.key == pygame.K_d: + robot.directions[3] = False + if event.key == pygame.K_z: + robot.directions[4] = False + elif event.key == pygame.K_x: + robot.directions[5] = False + if event.key == pygame.K_w: + robot.directions[6] = False + elif event.key == pygame.K_s: + robot.directions[7] = False + def drive(robot): + if (robot.directions[0] == True): + robot.foreward() + if (robot.directions[1] == True): + robot.backward() + if (robot.directions[2] == True): + robot.left() + if (robot.directions[3] == True): + robot.right() + if (robot.directions[4] == True): + robot.turnLeft() + if (robot.directions[5] == True): + robot.turnRight() + if (robot.directions[6] == True): + robot.up() + if (robot.directions[7] == True): + robot.down() + + def __lt__(self, other): return False + + \ No newline at end of file diff --git a/Classes/__pycache__/Ball.cpython-310.pyc b/Classes/__pycache__/Ball.cpython-310.pyc index 374a9aa..401bdfa 100644 Binary files a/Classes/__pycache__/Ball.cpython-310.pyc and b/Classes/__pycache__/Ball.cpython-310.pyc differ diff --git a/Classes/__pycache__/Ball.cpython-38.pyc b/Classes/__pycache__/Ball.cpython-38.pyc new file mode 100644 index 0000000..6ce470c Binary files /dev/null and b/Classes/__pycache__/Ball.cpython-38.pyc differ diff --git a/Classes/__pycache__/Globals.cpython-310.pyc b/Classes/__pycache__/Globals.cpython-310.pyc index 75a49e3..40a6e7b 100644 Binary files a/Classes/__pycache__/Globals.cpython-310.pyc and b/Classes/__pycache__/Globals.cpython-310.pyc differ diff --git a/Classes/__pycache__/Globals.cpython-38.pyc b/Classes/__pycache__/Globals.cpython-38.pyc new file mode 100644 index 0000000..ac6ec25 Binary files /dev/null and b/Classes/__pycache__/Globals.cpython-38.pyc differ diff --git a/Classes/__pycache__/Goal.cpython-310.pyc b/Classes/__pycache__/Goal.cpython-310.pyc index 6c82950..bcedec3 100644 Binary files a/Classes/__pycache__/Goal.cpython-310.pyc and b/Classes/__pycache__/Goal.cpython-310.pyc differ diff --git a/Classes/__pycache__/Goal.cpython-38.pyc b/Classes/__pycache__/Goal.cpython-38.pyc new file mode 100644 index 0000000..edac398 Binary files /dev/null and b/Classes/__pycache__/Goal.cpython-38.pyc differ diff --git a/Classes/__pycache__/PicObj.cpython-38.pyc b/Classes/__pycache__/PicObj.cpython-38.pyc new file mode 100644 index 0000000..88fcdce Binary files /dev/null and b/Classes/__pycache__/PicObj.cpython-38.pyc differ diff --git a/Classes/__pycache__/Robot.cpython-310.pyc b/Classes/__pycache__/Robot.cpython-310.pyc index a8411b9..655c4c4 100644 Binary files a/Classes/__pycache__/Robot.cpython-310.pyc and b/Classes/__pycache__/Robot.cpython-310.pyc differ diff --git a/Classes/__pycache__/Robot.cpython-38.pyc b/Classes/__pycache__/Robot.cpython-38.pyc new file mode 100644 index 0000000..9068193 Binary files /dev/null and b/Classes/__pycache__/Robot.cpython-38.pyc differ diff --git a/README.md b/README.md index 18b0a18..f8c4eec 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # RobocupSim -sim for robocup \ No newline at end of file +My branch focuses on the Robot Class, where I updated the movement system to allow for diagonal movement and simultainous movement/rotation operations. Work was also done on the Globals class, where methods such as control were changed and relocated to the Robot class