def __init__(self, setting: SettingGame.SettingGame): self.HEIGHT = setting.HEIGHT self.WIDTH = setting.WIDTH self.paddle_speed = setting.paddle_speed self.ball_speed = setting.ball_speed self.ball_size = setting.ball_size self.paddle_size = setting.paddle_size self.paddle_r_size = setting.paddle_r_size self.max_duration_game = setting.max_game_duration self.max_score = setting.max_score self.ai_number = setting.ai_number self.random_reflection = setting.random_reflection self.min_x = setting.LEFT_TOP_CORNER[0] self.min_y = setting.LEFT_TOP_CORNER[1] self.max_x = setting.RIGHT_BOT_CORNER[0] self.max_y = setting.RIGHT_BOT_CORNER[1] self.player_1_score = 0 self.player_2_score = 0 self.paddle_1 = Paddle.Paddle(self.min_x + 50, (self.min_y + self.max_y) // 2, self.paddle_speed, self.paddle_r_size, self.paddle_size) self.paddle_2 = Paddle.Paddle(self.max_x - 50, (self.min_y + self.max_y) // 2, self.paddle_speed, self.paddle_r_size, self.paddle_size) self.ball = Ball.Ball((self.min_x + self.max_x) // 2, (self.min_y + self.max_y) // 2, self.ball_speed, self.ball_speed, self.ball_size) self.collision_calculator = CollisionCalculator.CollisionCalculator( self.min_x, self.min_y, self.max_x, self.max_y, self.paddle_1, self.paddle_2, self.ball, self)
import random turtle.tracer(0) import math import sys turtle.bgpic("bg.gif") score = 0 RUNNING = True sleep = 0.0077 SCREEN_WIDTH = turtle.getcanvas().winfo_width() / 2 SCREEN_HEIGHT = turtle.getcanvas().winfo_height() / 2 MY_BALL = Ball(0, 1, 10, 30, 40, "blue") NUMBER_OF_BALLS = 8 MINIMUM_BALL_RADIUS = 10 MAXIMUM_BALL_RADIUS = 100 MINIMUM_BALL_DX = -5 MAXIMUM_BALL_DX = 5 MINIMUM_BALL_DY = -5 MAXIMUM_BALL_DY = 5 BALLS = [] for i in range(NUMBER_OF_BALLS): x = random.randint( int(-SCREEN_WIDTH) + int(MAXIMUM_BALL_RADIUS), int(SCREEN_WIDTH) - int(MAXIMUM_BALL_RADIUS))
def __init__(self, master, write_sockets, read_socket): """ write_sockets: list of 3 sockets already connected to the robots """ Frame.__init__(self, master) # game_state contains all the real time information about the game self.game_state = State(home_robots=[ Robot(rid=0, team=1, color="blue", sock=write_sockets[0]), Robot(rid=1, team=1, color="red", sock=write_sockets[1]), Robot(rid=2, team=1, color="pink", sock=write_sockets[2]) ], away_robots=[ Robot(rid=0, team=2), Robot(rid=1, team=2), Robot(rid=2, team=2) ], ball=Ball()) self.master = master self.read_socket = read_socket # Threads for listening data and updating ui # self.listener = Thread(target=self.listen_data) # self.listener.start() self.dato_0 = Label(master, text="1", fg="Black", font=("Helvetica", 30), padx=100) self.dato_0.grid(column=0) self.instrucciones_0 = [] self.instrucciones_0.append( Button(master, text="Turn off", command=lambda: self.sendInstruction( "0", [self.game_state.home_robots[0].socket]), font=('Helvetica', 16))) self.instrucciones_0.append( Button(master, text="Turn on", command=lambda: self.sendInstruction( "1", [self.game_state.home_robots[0].socket]), font=('Helvetica', 16))) for i in self.instrucciones_0: i.grid(column=0) self.dato_1 = Label(master, text="1", fg="Black", font=("Helvetica", 30), padx=100) self.dato_1.grid(column=1) self.instrucciones_1 = [] self.instrucciones_1.append( Button(master, text="Turn off", command=lambda: self.sendInstruction( "0", [self.game_state.home_robots[1].socket]), font=('Helvetica', 16))) self.instrucciones_1.append( Button(master, text="Turn on", command=lambda: self.sendInstruction( "1", [self.game_state.home_robots[1].socket]), font=('Helvetica', 16))) for i in self.instrucciones_1: i.grid(column=1) self.dato_2 = Label(master, text="2", fg="Black", font=("Helvetica", 30), padx=100) self.dato_2.grid(column=2) self.instrucciones_2 = [] self.instrucciones_2.append( Button(master, text="Turn off", command=lambda: self.sendInstruction( "0", [self.game_state.home_robots[2].socket]), font=('Helvetica', 16))) self.instrucciones_2.append( Button(master, text="Turn on", command=lambda: self.sendInstruction( "1", [self.game_state.home_robots[2].socket]), font=('Helvetica', 16))) for i in self.instrucciones_2: i.grid(column=2) # Sends to all robots self.dato_3 = Label(master, text="ALL", fg="red", font=("Helvetica", 30, "bold"), padx=100) self.dato_3.grid(column=3) self.instrucciones_3 = [] self.instrucciones_3.append( Button(master, text="Turn off", command=lambda: self.sendInstruction( "0", [robot.socket for robot in self.game_state.home_robots]), font=('Helvetica', 16))) self.instrucciones_3.append( Button(master, text="Turn on", command=lambda: self.sendInstruction( "1", [robot.socket for robot in self.game_state.home_robots]), font=('Helvetica', 16))) for i in self.instrucciones_3: i.grid(column=3) # Match details score = f"{self.game_state.home_goals} - {self.game_state.away_goals}" self.score = Label(master, text=score, fg="green", font=("Helvetica", 40, "bold"), padx=150) self.score.grid(column=4) self.update_data()
def next_step(self): for ar in self.global_state.away_robots: ar.x += np.random.randint(-3, 4) ar.y += np.random.randint(-3, 4) # Initial global state robots_home = [ Robot(0, 1, "blue", None, x=10, y=65, yaw=0), Robot(1, 1, "red", None, x=50, y=25, yaw=0), Robot(2, 1, "green", None, x=50, y=105, yaw=0) ] robots_away = [ Robot(0, 2, "red", None, x=140, y=65, yaw=180), Robot(1, 2, "pink", None, x=100, y=25, yaw=180), Robot(2, 2, "purple", None, x=100, y=105, yaw=180) ] ball = Ball(75, 65) global_state = State(robots_home, robots_away, ball) simulator = Simulator(global_state) for t in range(0, 100): print(f"TIME: {t}") simulator.global_state.print_field() simulator.next_step() ## Code to move home robots goes here ### #### time.sleep(.1)
def __init__(self, read_socket): super().__init__() style = ttk.Style() style.theme_use("clam") style.configure("Dashboard.TFrame", background=COLOR_LIGHT_BACKGROUND) style.configure("BackgroundRED.TFrame", background="red") style.configure("BackgroundBLUE.TFrame", background="blue") style.configure("BackgroundGREEN.TFrame", background="green") style.configure("BackgroundYELLOW.TFrame", background="yellow") style.configure("BackgroundPURPLE.TFrame", background="purple") style.configure("BackgroundWHITE.TFrame", background="white") style.configure("BackgroundORANGE.TFrame", background="orange") style.configure("BackgroundPINK.TFrame", background="pink") style.configure("Background.TFrame", background=COLOR_PRIMARY) style.configure("Dashboard.TLabel", background=COLOR_LIGHT_BACKGROUND, foreground=COLOR_DARK_TEXT, font="Courier 46") style.configure("LightText.TLabel", background=COLOR_PRIMARY, foreground=COLOR_LIGHT_TEXT, font=("TkDefaultFont", 11)) style.configure("Button.TButton", background=[COLOR_SECONDARY], foreground=COLOR_LIGHT_TEXT, font=("TkDefaultFont", 11)) style.configure("info_button.TButton", background="black", foreground="white", font=("TkDefaultFont", 11)) style.map("Button.TButton", background=[("active", COLOR_PRIMARY), ("disabled", COLOR_LIGHT_TEXT)]) SCREEN_WIDTH = self.winfo_screenwidth() SCREEN_HEIGHT = self.winfo_screenheight() self["background"] = COLOR_PRIMARY self.columnconfigure(0, weight=1) self.rowconfigure(0, weight=1) container = ttk.Frame(self) container["height"] = SCREEN_HEIGHT container["width"] = SCREEN_WIDTH container.grid() container.columnconfigure(0, weight=1) self.frames = {} self.is_powered_on = False settings_frame = Settings(container, self, lambda: self.show_frame(Dashboard), height=SCREEN_HEIGHT, width=SCREEN_WIDTH) dashboard_frame = Dashboard(container, self, lambda: self.show_frame(Settings), height=SCREEN_HEIGHT, width=SCREEN_WIDTH) settings_frame.grid(row=0, column=0, sticky="NESW") dashboard_frame.grid(row=0, column=0, sticky="NESW") self.frames[Settings] = settings_frame self.frames[Dashboard] = dashboard_frame self.show_frame(Dashboard) self.title('ALPHA SOCCER FC v1.0') self.geometry(f"{SCREEN_WIDTH}x{SCREEN_HEIGHT}") self.resizable(False, False) print(SCREEN_HEIGHT, SCREEN_WIDTH) self.global_state = State(home_robots=[ Robot(rid=0, team=1, color="blue", sock=None, x=0, y=0, yaw=0), Robot(rid=1, team=1, color="green", sock=None, x=0, y=0, yaw=0), Robot(rid=2, team=1, color="red", sock=None, x=0, y=0, yaw=0) ], away_robots=[ Robot(rid=0, team=2, color="blue", sock=None, x=0, y=0, yaw=0), Robot(rid=1, team=2, color="green", sock=None, x=0, y=0, yaw=0), Robot(rid=2, team=2, color="red", sock=None, x=0, y=0, yaw=0) ], ball=Ball()) self.read_socket = read_socket self.update()
change_ball_speed(touches) # Get the new position cursor compute_cursor(key_pressed) return ball, cursor, bricks, lifes # Use it to determine the main program, RESPECT THE SYNTAX if __name__ == '__main__': width_screen = 600 height_screen = 600 # Enable graphics engine engine = GameEngine(width_screen, height_screen) # Create ball ball = Ball(width_screen / 2, height_screen - 200) ball.dx = -6 ball.dy = -3 # Create cursor cursor = Cursor(width_screen / 2, height_screen - 30) # Create Bricks bricks = add_lines_brick(2, 3, width_screen) # Create lifes lifes = [ Life(5, height_screen - 100), Life(50, height_screen - 100), Life(100, height_screen - 100) ] print(bricks)
win = pygame.display.set_mode((750, 500)) pygame.display.set_caption('PONG') WHITE = (255, 255, 255) BLACK = (0, 0, 0) paddle1 = Paddle() paddle1.rect.x = 25 paddle1.rect.y = 225 paddle2 = Paddle() paddle2.rect.x = 715 paddle2.rect.y = 225 pong = Ball() pong.rect.x = 375 pong.rect.y = 250 all_sprites = pygame.sprite.Group() all_sprites.add(paddle1, paddle2, pong) clock = pygame.time.Clock() def redraw(): win.fill(BLACK) # -- Title Font font = pygame.font.SysFont('Halvetica', 30) text = font.render('PONG', False, WHITE) textRect = text.get_rect() textRect.center = (750 // 2, 25)