Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ethan #2

Open
wants to merge 3 commits into
base: merge
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions Classes/Ball.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
59 changes: 2 additions & 57 deletions Classes/Globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down Expand Up @@ -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:
Expand All @@ -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]
#if event.type == pygame.KEYUP:
# if
40 changes: 1 addition & 39 deletions Classes/Goal.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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))
71 changes: 69 additions & 2 deletions Classes/Robot.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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


Binary file modified Classes/__pycache__/Ball.cpython-310.pyc
Binary file not shown.
Binary file added Classes/__pycache__/Ball.cpython-38.pyc
Binary file not shown.
Binary file modified Classes/__pycache__/Globals.cpython-310.pyc
Binary file not shown.
Binary file added Classes/__pycache__/Globals.cpython-38.pyc
Binary file not shown.
Binary file modified Classes/__pycache__/Goal.cpython-310.pyc
Binary file not shown.
Binary file added Classes/__pycache__/Goal.cpython-38.pyc
Binary file not shown.
Binary file added Classes/__pycache__/PicObj.cpython-38.pyc
Binary file not shown.
Binary file modified Classes/__pycache__/Robot.cpython-310.pyc
Binary file not shown.
Binary file added Classes/__pycache__/Robot.cpython-38.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# RobocupSim
sim for robocup
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