def play_rps(): frame = simplegui.create_frame("Home", 300, 200) frame.add_button("Rock", rock) frame.add_button("Paper", paper) frame.add_button("Scissors", scissors) frame.set_draw_handler(draw) frame.start()
def setupGUI(): """ Setup frame and other control objects for GUI """ global Frame, txtCurrentHeading, txtDesiredHeading, txtThrottle, txtRudder, btnCircle, btnJoystickControl #Frame Frame = gui.create_frame('Minnow Simulator', width, height) Frame.set_draw_handler(drawHandler) #========Text Boxes ===================================== txtCurrentHeading = Frame.add_input("Current Heading", txtHandler, 100) txtDesiredHeading = Frame.add_input("Desired Heading", txtHandler, 100) txtThrottle = Frame.add_input("Throttle Value", txtHandler, 100) txtRudder = Frame.add_input("Rudder Value", txtHandler, 100) #======= Buttons ============================================== btnNorth = Frame.add_button("North", northHandler, 50) btnSouth = Frame.add_button("South", southHandler, 50) btnEast = Frame.add_button("East", eastHandler, 50) btnWest = Frame.add_button("West", westHandler, 50) lblCircle = Frame.add_label('Move_Circle') btnCircleStart = Frame.add_button("start", circleStartHandler, 25) #====== Joystick Control ===================================================== lblJoystickControl = Frame.add_label('Joystick_Control') btnJoystickControl = Frame.add_button("ON", joyControlHandler, 50)
def intro_game(): #设置游戏介绍的框架# frame = simplegui.create_frame('华容道', 300, 300) frame.set_canvas_background('white') frame.set_draw_handler(draw_intro) frame.add_button('主菜单', main_game, 100) frame.start()
def setupGUI(): """ Setup frame and other control objects for GUI """ global Frame,txtCurrentHeading,txtDesiredHeading,txtThrottle,txtRudder,btnCircle,btnJoystickControl #Frame Frame = gui.create_frame('Minnow Simulator', width, height) Frame.set_draw_handler(drawHandler) #========Text Boxes ===================================== txtCurrentHeading = Frame.add_input("Current Heading",txtHandler,100) txtDesiredHeading = Frame.add_input("Desired Heading",txtHandler,100) txtThrottle = Frame.add_input("Throttle Value",txtHandler,100) txtRudder = Frame.add_input("Rudder Value",txtHandler,100) #======= Buttons ============================================== btnNorth = Frame.add_button("North",northHandler,50) btnSouth = Frame.add_button("South",southHandler,50) btnEast = Frame.add_button("East",eastHandler,50) btnWest = Frame.add_button("West",westHandler,50) lblCircle = Frame.add_label('Move_Circle') btnCircleStart = Frame.add_button("start",circleStartHandler,25) #====== Joystick Control ===================================================== lblJoystickControl = Frame.add_label('Joystick_Control') btnJoystickControl = Frame.add_button("ON",joyControlHandler,50)
def __init__(self, game): """ Initialize GUI """ self._game = game self._tiling_size = self._game.get_tiling_size() self.init_grid() canvas_width = 2 * EDGE_LENGTH + (3 * self._tiling_size * EDGE_LENGTH / 2) canvas_height = (self._tiling_size + 1) * HEX_HEIGHT self._frame = simplegui.create_frame("Tantrix Solitaire demo", canvas_width, canvas_height) self._frame.add_button("Is legal?", self.check_legal, 200) self._frame.add_button("Yellow loop of length 10?", self.yellow_loop, 200) self._frame.add_button("Red loop of length 10?", self.red_loop, 200) self._frame.add_button("Blue loop of length 10?", self.blue_loop, 200) self._frame.set_draw_handler(self.draw) self._frame.set_canvas_background("White") self._frame.set_mouseclick_handler(self.click) self._frame.set_mousedrag_handler(self.drag) self._frame.start() self._mouse_drag = False
def demo2(): # Create a frame and assign callbacks to event handlers frame = simplegui.create_frame("Home", 300, 200) frame.add_button("Click me", click) frame.set_draw_handler(draw) # Start the frame animation frame.start()
def win_game(): #设置游戏成功的框架# frame = simplegui.create_frame('华容道', 300, 300) frame.set_canvas_background('white') frame.set_draw_handler(draw_win) frame.add_button('主菜单', main_game, 100) frame.add_button('结束游戏', end_game, 100) frame.start()
def main_game(): #设置主菜单的框架# frame = simplegui.create_frame('华容道', -100, -100) frame.set_canvas_background('black') frame.add_button('开始游戏', play_game, 100) frame.add_button('游戏介绍', intro_game, 100) frame.add_button('结束游戏', end_game, 100) frame.start()
def __init__(self): """ Initializer to create frame, sets handlers and initialize list of balls to be empty """ self.simulation = Simulation() self._frame = simplegui.create_frame("ball/ball collision simulator", CANVAS_WIDTH, CANVAS_HEIGHT) self._frame.set_draw_handler(self.draw) self._frame.set_mouseclick_handler(self.click) self._prev_time = time.time() self._frame_rate = 60.0
def setup_frame(self): """ Create GUI frame and add handlers. """ self._frame = simplegui.create_frame("Tic-Tac-Toe", GUI_WIDTH, GUI_HEIGHT) self._frame.set_canvas_background('White') # Set handlers self._frame.set_draw_handler(self.draw) self._frame.set_mouseclick_handler(self.click) self._frame.add_button("New Game", self.newgame) self._label = self._frame.add_label("")
def __init__(self): """ Create a frame and load the cancer data """ self._frame = simplegui.create_frame("Cancer risk visualization", CANVAS_WIDTH, CANVAS_HEIGHT) self._frame.set_canvas_background("White") self._frame.set_draw_handler(self.draw) self._frame.add_input("Enter cutoff (scaled by 0.00001)", self.enter_cutoff, 200) self._frame.add_button("Print data table in CSV", self.print_table, 200) self._data_table = self.load_data() self.enter_cutoff("0.0") self._USA_Counties = simplegui.load_image(MAP_URL) self._frame.start()
def createWorld(self): world = gui.create_frame("Robot World",self.worldX,self.worldY) world.set_canvas_background(GUI.WORLD_COLOR) self.txtSetDist = world.add_input("Set Distance", self.txtDestDistance,50) self.btnSetDist = world.add_button("Set Distance",self.setDistHandler,50) self.btnMove = world.add_button("Move",self.moveHandler, 50) timer = gui.create_timer(500,self.timerHandler) world.set_draw_handler(self.renderingHandler) timer.start() world.start()
def __init__(self, wildfire): """ Create frame and timers, register event handlers """ self._fire = wildfire self._grid_height = self._fire.get_grid_height() self._grid_width = self._fire.get_grid_width() self._frame = simplegui.create_frame("Interactive BFS demo", self._grid_width * CELL_SIZE, self._grid_height * CELL_SIZE) self._frame.set_canvas_background("White") self._frame.add_button("Clear all", self.clear, 100) self._frame.add_button("Step", self.step, 100) self._frame.add_button("Ten steps", self.ten_steps, 100) self._frame.set_mouseclick_handler(self.add_cell_index) self._frame.set_draw_handler(self.draw)
def createFrame(): global FRAME_WIDTH, FRAME_HEIGHT FRAME_NAME = 'Stopwatch - THE GAME' #create frame frame = simplegui.create_frame(FRAME_NAME, FRAME_WIDTH, FRAME_HEIGHT) #add buttons to frame frame = addButtons(frame) #register frame event handlers frame = setEventHandlers(frame) return frame
def __init__(self, game): self._cols = game.get_grid_height() self._rows = game.get_grid_width() self._frame = simplegui.create_frame( "2048", self._rows * TILE_SIZE + 2 * BORDER_SIZE, self._cols * TILE_SIZE + 2 * BORDER_SIZE ) self._frame.add_button("New Game", self.start) self._frame.set_keydown_handler(self.keydown) self._frame.set_draw_handler(self.draw) self._frame.set_canvas_background("#A39480") self._frame.start() self._game = game url = "http://codeskulptor-assets.commondatastorage.googleapis.com/assets_2048.png" self._tiles = simplegui.load_image(url) self._directions = {"up": UP, "down": DOWN, "left": LEFT, "right": RIGHT}
def __init__(self, game): self._game = game self._rows = game.get_grid_height() self._cols = game.get_grid_width() self._frame = simplegui.create_frame('2048', self._cols * TILE_SIZE + 2 * BORDER_SIZE, self._rows * TILE_SIZE + 2 * BORDER_SIZE) self._frame.add_button('New Game', self.start) self._frame.set_keydown_handler(self.keydown) self._frame.set_draw_handler(self.draw) self._frame.set_canvas_background("#BCADA1") self._tiles = simplegui.load_image(IMAGE_URL) self._directions = {"up": UP, "down": DOWN, "left": LEFT, "right": RIGHT} self._frame.start()
def __init__(self): """ Initialize the simulation """ self._frame = simplegui.create_frame("Monty Hall problem", CANVAS_WIDTH, CANVAS_HEIGHT, CONTROL_WIDTH) self._frame.set_canvas_background("White") self._frame.add_button("Clear", self.clear, CONTROL_WIDTH) self._frame.add_button("Add door", self.add_door, CONTROL_WIDTH) self._frame.set_mouseclick_handler(self.click) self._frame.add_label("") self._win_label = self._frame.add_label("Wins = 0") self._lose_label = self._frame.add_label("Loses = 0") self.clear() self._frame.set_draw_handler(self.draw) self._frame.start()
def __init__(self, game): """ Create frame and timers, register event handlers """ self.game = game self._grid_height = self.game.get_grid_height() self._grid_width = self.game.get_grid_width() self._frame = simplegui.create_frame("Interactive Game of Life demo", self._grid_width * CELL_SIZE, self._grid_height * CELL_SIZE) self._frame.set_canvas_background("White") self._frame.add_button("Clear all", self.clear, 100) self._frame.add_button("Step", self.step, 100) self._frame.add_button("Ten steps", self.ten_steps, 100) self._frame.set_mousedrag_handler(self.add_cell_index) self._frame.set_draw_handler(self.draw)
def __init__(self, game): """ Initializer to create frame, sets handlers and initialize game """ self._frame = simplegui.create_frame("Mancala Solitaire", CANVAS_SIZE[0], CANVAS_SIZE[1]) self._frame.set_canvas_background("White") self._frame.set_draw_handler(self.draw) self._frame.add_button("New board", self.new_board, 200) self._frame.add_button("Restart board", self.restart_board, 200) self._frame.add_button("Make move", self.make_move, 200) self._frame.set_mouseclick_handler(self.click_move) # fire up game and frame self._game = game self.new_board()
def createWorld(self): world = gui.create_frame("Robot World", self.worldX, self.worldY) world.set_canvas_background(GUI.WORLD_COLOR) self.txtSetDist = world.add_input("Set Distance", self.txtDestDistance, 50) self.btnSetDist = world.add_button("Set Distance", self.setDistHandler, 50) self.btnMove = world.add_button("Move", self.moveHandler, 50) timer = gui.create_timer(500, self.timerHandler) world.set_draw_handler(self.renderingHandler) timer.start() world.start()
def __init__(self, game): self._rows = game.get_grid_height() self._cols = game.get_grid_width() self._frame = simplegui.create_frame('2048', self._cols * TILE_SIZE + 2 * BORDER_SIZE, self._rows * TILE_SIZE + 2 * BORDER_SIZE) self._frame.add_button('New Game', self.start) self._frame.set_keydown_handler(self.keydown) self._frame.set_draw_handler(self.draw) self._frame.set_canvas_background("#A39480") self._frame.start() self._game = game url = 'http://codeskulptor-assets.commondatastorage.googleapis.com/assets_2048.png' #print url self._tiles = simplegui.load_image(url) self._directions = {"up": UP, "down": DOWN, "left": LEFT, "right": RIGHT}
def __init__(self, simulation): """ Create frame and timers, register event handlers """ self._simulation = simulation self._grid_height = self._simulation.get_grid_height() self._grid_width = self._simulation.get_grid_width() self._frame = simplegui.create_frame("Zombie Apocalypse simulation", self._grid_width * CELL_SIZE, self._grid_height * CELL_SIZE) self._frame.set_canvas_background("White") self._frame.add_button("Clear all", self.clear, 200) self._item_type = OBSTACLE label = LABEL_STRING + self._item_type self._item_label = self._frame.add_button(label, self.toggle_item, 200) self._frame.add_button("Humans flee", self.flee, 200) self._frame.add_button("Zombies stalk", self.stalk, 200) self._frame.set_mouseclick_handler(self.add_item) self._frame.set_draw_handler(self.draw)
def __init__(self, game): self._rows = game.get_grid_height() self._cols = game.get_grid_width() self._game = game self._tiles = simplegui.load_image(IMAGEURL) self._directions = { "up": UP, "down": DOWN, "left": LEFT, "right": RIGHT } self._frame = simplegui.create_frame( '2048', self._cols * TILE_SIZE + 2 * BORDER_SIZE, self._rows * TILE_SIZE + 2 * BORDER_SIZE) self._frame.add_button('New Game', self.start) self._frame.set_keydown_handler(self.keydown) self._frame.set_draw_handler(self.draw) self._frame.set_canvas_background("#BCADA1") self._frame.start()
def __init__(self, simulation): """ Create frame and timers, register event handlers """ self._simulation = simulation self._grid_height = self._simulation.get_grid_height() self._grid_width = self._simulation.get_grid_width() self._frame = simplegui.create_frame("Zombie Apocalypse simulation", self._grid_width * CELL_SIZE, self._grid_height * CELL_SIZE) self._frame.set_canvas_background("White") self._frame.add_button("Clear all", self.clear, 200) self._item_type = OBSTACLE label = LABEL_STRING + NAME_MAP[self._item_type] self._item_label = self._frame.add_button(label, self.toggle_item, 200) self._frame.add_button("Humans flee", self.flee, 200) self._frame.add_button("Zombies stalk", self.stalk, 200) self._frame.set_mouseclick_handler(self.add_item) self._frame.set_draw_handler(self.draw)
def __init__(self, puzzle): """ Create frame and timers, register event handlers """ self._puzzle = puzzle self._puzzle_height = puzzle.get_height() self._puzzle_width = puzzle.get_width() self._frame = simplegui.create_frame("The Fifteen puzzle", self._puzzle_width * TILE_SIZE, self._puzzle_height * TILE_SIZE) self._solution = "" self._current_moves = "" self._frame.add_button("Solve", self.solve, 100) self._frame.add_input("Enter moves", self.enter_moves, 100) self._frame.add_button("Print moves", self.print_moves, 100) self._frame.set_draw_handler(self.draw) self._frame.set_keydown_handler(self.keydown) self._timer = simplegui.create_timer(50, self.tick) self._timer.start() self._frame.start()
def create_gui(): global mission_pub, input_start1, input_start2, input_start3, input_start4, input_start5 global HEIGHT, WIDTH # Creates the frame window im = Image.open(imagepath) WIDTH, HEIGHT = im.size frame = simplegui.create_frame("Graph Designer and Publisher", WIDTH, HEIGHT) frame.set_mouseclick_handler(mouseclick) frame.set_draw_handler(draw_handler) # Button, input and label controls for the frame window button4 = frame.add_button('Load nodes', load_nodes) button5 = frame.add_button('Load adjacency', load_adjacency) button1 = frame.add_button('Lock in the nodes', button_lock_nodes) button2 = frame.add_button('Lock in the graph', button_lock_graph) button3 = frame.add_button('Reset edge drawing', button_refresh_new_relation) label0 = frame.add_label(' ') label1 = frame.add_label( 'Task 1: Lift Block\nTask 2: Charge Bot\nTask 3: Drop Block') label12 = frame.add_label( 'Input format: \'Station<char/int> Stay<boolean> Task<int> Yaw<float>\'' ) input_start1 = frame.add_input('Station 1', input1, 50) input_start2 = frame.add_input('Station 2', input2, 50) input_start3 = frame.add_input('Station 3', input3, 50) input_start4 = frame.add_input('Station 4', input4, 50) input_start5 = frame.add_input('Station 5', input5, 50) button4 = frame.add_button('Publish Stations', publisher) mission_pub = rospy.Publisher('mission', String, queue_size=10) rospy.init_node('gui', anonymous=True) # Program starts here frame.start()
def __init__(self, game): """ Create frame and timers, register event handlers """ self.game = game self.frame = simplegui.create_frame("Word Wrangler", CANVAS_WIDTH, CANVAS_HEIGHT, 250) self.frame.set_canvas_background("Blue") self.enter_input = self.frame.add_input("Enter word for new game", self.enter_start_word, 250) labelmsg = "Stars correspond to hidden words formed using letters " labelmsg += "from the entered word. Hidden words are listed in alphabetical order" self.frame.add_label(labelmsg, 250) self.frame.add_label("", 250) self.guess_label = self.frame.add_input("Enter a word", self.enter_guess, 250) self.frame.add_label("For a hint, click on a starred word", 250) self.frame.set_mouseclick_handler(self.peek) self.frame.set_draw_handler(self.draw) self.enter_input.set_text("python") self.game.start_game("python")
start() def type2(): global game_type game_type = 2 start() def type3(): global game_type game_type = 3 start() def game_info(): info.clear() if game_type == 1: down(COUNT) if game_type == 2: up(COUNT) # initialize frame frame = simplegui.create_frame("Chinese ring puzzle", WIDTH, HEIGHT) button2 = frame.add_button('玩法一', type1, 100) button3 = frame.add_button('玩法二', type2, 100) button4 = frame.add_button('随机玩法', type3, 100) button4 = frame.add_button('游戏提示', game_info, 100) # register handlers frame.set_draw_handler(draw) frame.set_keydown_handler(key_down) frame.start()
paddle1_vel = -8 elif key == simplegui.KEY_MAP['s'] and paddle1_pos <= HEIGHT: paddle1_vel = 8 def keyup(key): global paddle1_vel, paddle2_vel if key == simplegui.KEY_MAP['up'] or key == simplegui.KEY_MAP['down']: paddle2_vel = 0 elif key == simplegui.KEY_MAP['w'] or key == simplegui.KEY_MAP['s']: paddle1_vel = 0 def restart(): new_game() # create frame frame = simplegui.create_frame("Pong", WIDTH, HEIGHT) frame.set_draw_handler(draw) frame.set_keydown_handler(keydown) frame.set_keyup_handler(keyup) frame.add_button("Reset", restart, 100) # start frame new_game() frame.start()
canvas.draw_text(message,(position[0],position[1]), 23, "Red") # define enter handler def enter(inp): global message message = inp # define timer handler def tick(): global position x = random.randint(0,width) y = random.randint(0,highest) position[0] = x position[1] = y # create frame frame = simplegui.create_frame("Test",width,highest) timer = simplegui.create_timer(1000, tick) # register draw handler frame.set_draw_handler(draw) frame.add_input("Enter Message",enter, 100) # start frame timer.start() frame.start()
a_rock.update() a_missile.update() # keyboard input handlers: def checkKeyUp(key): my_ship.keyUp(key) # timer handler that spawns a rock def rock_spawner(): pass # initialize frame frame = simplegui.create_frame("Asteroids", width, height) # initialize ship and two sprites my_ship = Ship([width / 2, height / 2], [0, 0], 0, ship_image, ship_info) a_rock = Sprite([width / 3, height / 3], [1, 1], 0, 0, asteroid_image, asteroid_info) a_missile = Sprite([2 * width / 3, 2 * height / 3], [-1, 1], 0, 0, missile_image, missile_info, missile_sound) # register handlers frame.set_draw_handler(draw) frame.set_keyup_handler(checkKeyUp) timer = simplegui.create_timer(1000.0, rock_spawner) # get things rolling timer.start()
hits = 0 timer.stop() # Event handler for timer with 0.1 sec interval def start_timer(): global started, time if started == True: time += 1 # Draw handler def draw(canvas): canvas.draw_text(format(time), [80, 170], 40, "Teal") canvas.draw_text(str(hits) + "/" + str(count), [225, 45], 20, "Magenta") # Create frame frame = simplegui.create_frame("Stopwatch Game", 300, 300) timer = simplegui.create_timer(100, start_timer) # Register event handlers frame.set_draw_handler(draw) frame.add_button("Stop", stop_button, 100) frame.add_button("Start", start_button, 100) frame.add_button("Reset", reset, 100) # start frame frame.start() timer.start()
def root(): """ root store by operand """ global store store= store ** (1.0/operand) output() def new_var(): """ sets new store and operand """ x=float(raw_input("set store")) ## y=float(raw_input("set operand")) ## global store store= x global operand operand = y output() # create frame f = simpleguitk.create_frame("Calculator",300,300) # register event handlers f.add_button("Print", output, 100) f.add_button("Swap", swap, 100) f.add_button("Add", add, 100) f.add_button("Sub", sub, 100) f.add_button("Mult", mult, 100) f.add_button("Div", div, 100) f.add_button("Exp", exp, 100) f.add_button("Root",root,100) f.add_button("NEW VARIABLES", new_var, 200) # get frame rolling f.start()
# draw handler def draw(canvas): # test to make sure that card.draw works, replace with your code below canvas.draw_text("Blackjack", [220, 50], 50 ,"Navy") canvas.draw_text(outcome, [10, 100], 30 ,"White") player_hand.draw(canvas, [100, 300]) dealer_hand.draw(canvas, [100, 150]) canvas.draw_text("Dealer: %s" % dealer_score, [10, 150], 20, "Black") canvas.draw_text("Player: %s" % player_score, [10, 300], 20, "Black") if in_play: canvas.draw_image(card_back, CARD_BACK_CENTER, CARD_BACK_SIZE, (136,198), CARD_BACK_SIZE) # initialization frame frame = simplegui.create_frame("Blackjack", 600, 400) frame.set_canvas_background("Green") #create buttons and canvas callback frame.add_button("Deal", deal, 100) frame.add_button("Hit", hit, 100) frame.add_button("Stand", stand, 100) frame.set_draw_handler(draw) # get things rolling deal() frame.start() # remember to review the grade rubric
#global rock_group if len(rock_group) < MAXIMUM_OF_ROCKS and started: # let's speed of asteroids grows max_speed = BASE_SPEED * (DIFFICULTY_BASE ** (score // DIFFICULTY_STEP)) rock_pos = [random.randrange(0, WIDTH), random.randrange(0, HEIGHT)] rock_vel = [random.random() * 2 * max_speed - max_speed, random.random() * 2 * max_speed - max_speed] rock_avel = random.random() * .2 - .1 a_rock = Sprite(rock_pos, rock_vel, 0, rock_avel, asteroid_image[random.randrange(len(asteroid_image))], asteroid_info) minimum_distance = a_rock.get_radius() + my_ship.get_radius() distance = dist(a_rock.get_position(), my_ship.get_position()) # spawn rock only if distance big enough if SAFE_DISTANCE_MULT * minimum_distance < distance and not is_group_collide(rock_group, a_rock): rock_group.add(a_rock) # initialize stuff frame = simplegui.create_frame("Asteroids", WIDTH, HEIGHT) # initialize ship and empty sets of rocks and missiles my_ship = Ship([WIDTH / 2, HEIGHT / 2], [0, 0], 0, ship_image, ship_info) #a_rock = Sprite([WIDTH / 3, HEIGHT / 3], [1, 1], 0, .1, asteroid_image, asteroid_info) rock_group = set() #Should call it BEATLES #a_missile = Sprite([2 * WIDTH / 3, 2 * HEIGHT / 3], [-1,1], 0, 0, missile_image, missile_info, missile_sound) missile_group = set() explosion_group = set() # register handlers frame.set_keyup_handler(keyup) frame.set_keydown_handler(keydown) frame.set_mouseclick_handler(click) frame.set_draw_handler(draw)
# Define helper functions # No helper functions for this program # Define classes # No classes # Define event handlers def print_message(canvas): global message canvas.draw_text(message, [50, 100], 20, "red", "monospace") # Create frame frame = simplegui.create_frame("Test Message", 600, 400) #("Title", canvas width, canvas height) # Note that the entire window is the frame and the output # screen is the canvas # As with most GUI's, the origin is in the upper left corner # The coordiantes[50, 100] will go 50 pixels right # and 100 pixels down # Register event handlers frame.set_draw_handler(print_message) # Start frame and timers frame.start()
if state == 1 and card_index == index: color = "White" if state == 2 and (card_index == index or card_index == previous_index): color = "White" if exposed[card_index]: color = "White" canvas.draw_polygon([[card_pos, 0], [card_pos+50, 0], [card_pos+50, 100], [card_pos, 100]], 1, 'Red', 'Green') canvas.draw_text(str(cards[card_index]), (card_pos, 75), 70, color) # create frame and add a button and labels frame = simplegui.create_frame("Memory", 900, 100) frame.set_canvas_background('White') frame.add_button("Reset", new_game) label = frame.add_label("Turns = %d" % turns) # register event handlers frame.set_mouseclick_handler(mouseclick) frame.set_draw_handler(draw) # get things rolling new_game() frame.start() # Always remember to review the grading rubric
print(tmp) number = tmp def draw(canvas): global lines global number for line in lines: canvas.draw_line(line[0], line[1], 2, 'Blue') canvas.draw_line(line[0][::-1], line[1][::-1], 2, 'Blue') for i in range(8): for j in range(8): if number[i * 8 + j][1] == FRONT: canvas.draw_text(number[i * 8 + j][0], (i * 50 + offset[0], j * 50 + offset[1]), 25, 'Green') # create frame and add a button and labels frame = sg.create_frame("Memory", 400, 400) frame.add_button("Reset", new_game, 60) label = frame.add_label("Turns: 0") # register event handlers frame.set_mouseclick_handler(mouseclick) frame.set_draw_handler(draw) # get things rolling new_game() frame.start()
v = [4, 5 / 60] # 0 x pixels / tick, 3 y pixels / tick # event handlers def draw(canvas): # update ball position ball_pos[0] = ball_pos[0] + v[0] ball_pos[1] = ball_pos[1] + v[1] # ollide of of left canvas if ball_pos[0] <= ball_radius: v[0] = -v[0] #right if ball_pos[0] >= (width - ball_radius): v[0] = -v[0] # draw ball canvas.draw_circle(ball_pos, ball_radius, 2, "Red", "White") # create frame frame = simplegui.create_frame("Motion", width, height) # register handlers frame.set_draw_handler(draw) frame.start() #p(t+1) = p(0) + (t+1)v #p(t+1) = p(t) + (1)(v(t))
print "You've run out of guesses! The secret number was", secret_number new_game() return #otherwise let the user know how they did if guess_num < secret_number: print "Higher" else: print "Lower" #and tell them how many more guesses they have print "You have", guesses_left, "guesses left" # create frame frame = simplegui.create_frame('Guess the Number', 200, 200) frame.add_button('Range: 0-100', range100) frame.add_button('Range: 0-1000', range1000) range_label = frame.add_label('Current game, range: 0-100') range = 100 # register event handlers for control elements and start frame inp = frame.add_input('Guess:', input_guess, 50) # call new_game new_game() frame.start() # always remember to check your completed program against the grading rubric
if r < 3 and c < 3: if board[r][c] is None: return else: current_square = board[r][c] if r - 1 >= 0 and board[r - 1][c] is None: board[r][c] = None board[r - 1][c] = current_square steps += 1 elif r + 1 <= 2 and board[r + 1][c] is None: board[r][c] = None board[r + 1][c] = current_square steps += 1 elif c - 1 >= 0 and board[r][c - 1] is None: board[r][c] = None board[r][c - 1] = current_square steps += 1 elif c + 1 <= 2 and board[r][c + 1] is None: board[r][c] = None board[r][c + 1] = current_square steps += 1 pick = simplegui.create_frame('图片华容道', wd, hg) pick.set_canvas_background('white') pick.set_draw_handler(draw) pick.add_button('重新开始', play_game, 60) pick.set_mouseclick_handler(mouse_click) play_game() pick.start()
def sumar(): global almacen, operador almacen = almacen + operador mostrar() def restar(): global almacen, operador almacen = almacen - operador mostrar() def ingresar(value): global operador #todo lo que ingresamos por el teclado viene como texto, hay que pasarlo a entero operador = float(value) mostrar() """ LOS RESULTADOS SE VEN EN LA CONSOLA """ marco = simplegui.create_frame("Calculadora",300,300) #EL 100 ES EL ANCHO DEL BOTON marco.add_button("Mostrar", mostrar, 100) marco.add_button("Invertir", invertir , 100) marco.add_button("Sumar", sumar , 100) marco.add_button("Restar", restar , 100) marco.add_input("Ingresar operando: ", ingresar,100) marco.start() mostrar() invertir()
global size if size > 0: size -= 1 label.set_text("Value: " + str(size)) def change_radius_handler(): '''changes the circle radius''' global radius radius = size radiuslabel.set_text("Radius: " + str(radius)) def draw_handler(canvas): '''draws the circle''' canvas.draw_circle((100, 100), radius, 5, "Red") # create a frame, assign callbacks to event handlers frame = simplegui.create_frame("Home", 200, 200) label = frame.add_label("Value: " + str(size)) frame.add_button("Increase", increase_button_handler) frame.add_button("Decrease", decrease_button_handler) radiuslabel = frame.add_label("Radius: " + str(radius)) frame.add_button("Change circle", change_radius_handler) frame.set_draw_handler(draw_handler) frame.start()
if key == simplegui.KEY_MAP['left']: selected_shape.rotate(-fine_tuning) if key == simplegui.KEY_MAP['right']: selected_shape.rotate(fine_tuning) if key == simplegui.KEY_MAP['up']: selected_shape.rotate(-coarse_tuning) if key == simplegui.KEY_MAP['down']: selected_shape.rotate(coarse_tuning) ##def keyup(key): ## global selected_shape ## if selected_shape is not None: ## if key in [simplegui.KEY_MAP['left'], simplegui.KEY_MAP['right'], simplegui.KEY_MAP['up'], simplegui.KEY_MAP['down']]: ## selected_shape.rotate(0) # 创建用户界面 frame = simplegui.create_frame("七巧板", WIDTH, HEIGHT) frame.set_canvas_background("White") frame.set_draw_handler(draw) frame.set_mouseclick_handler(click) frame.set_mousedrag_handler(drag) frame.set_keydown_handler(keydown) #frame.set_keyup_handler(keyup) # 创建按钮 frame.add_button("重新开始", init, 80) init() frame.start()
import random import simpleguitk as simplegui ## Code ## Define Global factor1 = 3 factor2 = 5 ## Define Helper Functions ## Define Classes ## Define Event Handlers def CalcProduct(): global factor1, factor2 product = factor1 * factor2 print(product) ## Create Frame frame = simplegui.create_frame("Product Calculator", 64, 64) ## Register Event Handlers PrintProduct = frame.add_button("Print Product", CalcProduct) ## Start Frame and Timers frame.start()
# CodeSkulptor runs Python programs in your browser. # Click the upper left button to run this simple demo. # CodeSkulptor runs in Chrome 18+, Firefox 11+, and Safari 6+. # Some features may work in other browsers, but do not expect # full functionality. It does NOT run in Internet Explorer. import simpleguitk as simplegui message = "Welcome!" # Handler for mouse click def click(): global message message = "Good job!" # Handler to draw on canvas def draw(canvas): canvas.draw_text(message, [50,112], 48, "Red") # Create a frame and assign callbacks to event handlers frame = simplegui.create_frame("Home", 300, 200) frame.add_button("Click me", click) frame.set_draw_handler(draw) # Start the frame animation frame.start()
tmp = copy.deepcopy(number) tmp.extend(number) random.shuffle(tmp) print(tmp) number = tmp def draw(canvas): global lines global number for line in lines: canvas.draw_line(line[0], line[1], 2, 'Blue') canvas.draw_line(line[0][::-1], line[1][::-1], 2, 'Blue') for i in range(8): for j in range(8): if number[i*8+j][1] == FRONT: canvas.draw_text(number[i*8+j][0], (i*50 + offset[0], j * 50 + offset[1]), 25, 'Green') # create frame and add a button and labels frame = sg.create_frame("Memory", 400,400) frame.add_button("Reset", new_game, 60) label = frame.add_label("Turns: 0") # register event handlers frame.set_mouseclick_handler(mouseclick) frame.set_draw_handler(draw) # get things rolling new_game() frame.start()
canvas.draw_text(f"Extended Dynamic Policy", (200, 500), 12, 'White') # Step button event, but notice the robot moves after any click in the canvas def step(): r1.update() def add_obs(): global g_state g_state = "Add Obs" #create simplegui controls f1 = simplegui.create_frame("Obs Avoidance", constants.FRAME_SIZE, constants.FRAME_SIZE) btn_start = f1.add_button("Set Start", set_start, 100) btn_goal = f1.add_button("Set Goal", set_goal, 100) btn_robot = f1.add_button("Set Robot", set_robot_pos, 100) txt_r_co = f1.add_input("Robot Co", alter_co, 100) btn_step = f1.add_button("Step", step, 100) btn_add_obs = f1.add_button("Add Obs", add_obs, 100) f1.set_draw_handler(draw) f1.set_mouseclick_handler(click) #start simplegui print("f1.start()") f1.start() # after this point the application is event oriented
# define event handler for timer with 0.1 sec interval def timerhandler(): global timer_counter timer_counter +=1 return # define draw handler def draw_handler(canvas): canvas.draw_text(format(timer_counter), (CANVAS_WIDTH//2, CANVAS_HEIGHT//2), FONT_SIZE, "Red") score="Score: "+ convert(success_counter)+"/"+ convert(stop_counter) canvas.draw_text(score, (10,20), FONT_SIZE_SCORE, "White") # create frame frame = simplegui.create_frame("Stopwatch", CANVAS_WIDTH, CANVAS_HEIGHT) # register event handlers timer = simplegui.create_timer(10, timerhandler) frame.set_draw_handler(draw_handler) start_button = frame.add_button("Start", start_handler, 100) stop_button = frame.add_button("Stop", stop_handler, 100) reset_button = frame.add_button("Reset", reset_handler, 100) # start frame frame.start() # Please remember to review the grading rubric
def multi(): global store, operand store = store * operand output() def div(): global store, operand store = store / operand output() def enter(inp): global operand operand = float(inp) output() frame = simplegui.create_frame("Calcute", 300, 200) frame.add_button("print", output) frame.add_button("Swap", swap) frame.add_button("add", add) frame.add_button("minus", minus) frame.add_button("multi", multi) frame.add_button("div", div) frame.add_input("Enter operand", enter, 100) frame.start()
if key == simplegui.KEY_MAP["s"]: downkey = False paddle1_vel = 0 if not upkey else -2 elif key == simplegui.KEY_MAP["w"]: upkey = False paddle1_vel = 0 if not downkey else 2 if key == simplegui.KEY_MAP["down"]: downkey = False paddle2_vel = 0 if not upkey else -2 elif key == simplegui.KEY_MAP["up"]: upkey = False paddle2_vel = 0 if not downkey else 2 def reset(): new_game() # create frame frame = simplegui.create_frame("Pong", WIDTH, HEIGHT) frame.set_draw_handler(draw) frame.set_keydown_handler(keydown) frame.set_keyup_handler(keyup) frame.add_button("Restart", reset) # start frame new_game() frame.start() reset()
Also computes the stops / winning stops """ global stops, wins if (timer.is_running()): stops += 1 if (counter % 10) == 0: wins += 1 timer.stop() def reset_button_handler(): """ Resets the timer, stops and winning stops """ global counter, wins, stops counter = 0 wins = 0 stops = 0 # The main code of the app frame = simplegui.create_frame("Stopwatch", 320, 240) frame.set_draw_handler(draw_handler) button_start = frame.add_button("Start", start_button_handler) button_stop = frame.add_button("Stop",stop_button_handler) button_reset = frame.add_button("Reset", reset_button_handler) timer = simplegui.create_timer(100,tick) frame.start()
compliments = [ 'beautiful', 'sexy', 'smart', 'hard working', 'ambitious', 'resourceful', 'intelligent', 'kind', 'brilliant', 'humble', 'compassionate', 'strong' ] girlfriend_name = input("Enter name: ") message = random.choice(compliments) # Handler for mouse click def click(): global message message = random.choice(compliments) # Handler to draw on canvas def draw(canvas): canvas.draw_text(girlfriend_name + ',', [30, 50], 48, "Red") canvas.draw_text("you are very", [30, 100], 30, "Red") canvas.draw_text(message, [30, 150], 48, "Green") # Create a frame and assign callbacks to event handlers frame = simpleguitk.create_frame("Logan's Encouragement App!", 300, 200) frame.add_button("Click me", click) frame.set_draw_handler(draw) # Start the frame animation frame.start()
paddle2_vel = 0 elif key == simplegui.KEY_MAP["down"]: paddle2_vel = 0 #end game def end(): global score1, score2, result1, result2, games1, games2 if score1 > score2: games1 += 1 elif score2 > score1: games2 += 1 new_game() # create frame frame = simplegui.create_frame("PongPang", width, height) frame.set_draw_handler(draw) frame.set_keydown_handler(keydown) frame.set_keyup_handler(keyup) frame.add_label('Start the game by pressing SPACE') frame.add_button("RESTART", new_game, 100) frame.add_label('End game by pressing Z') frame.add_button("END", end, 100) # start frame game() frame.start()
message2 = "Угадал!" print("Угадал!\n") init() else: message2 = "Ты проиграл" print("Ты не угадал.") print("Было загадано число ", secret_number, "\n") init() def draw(canvas): canvas.draw_text(message, (width / 2 - len(message) * 11 / 2, height / 3), 20, "Lime") canvas.draw_text(message2, (width / 2 - len(message2) * 11 / 2, height * 3 / 4), 20, "Lime") # create frame frame = simplegui.create_frame("Угадай число", width, height) # register event handlers for control elements frame.add_button("Диапазон [0, 100)", range100, 200) frame.add_button("Диапазон [0, 1000)", range1000, 200) frame.add_input("Введите число", get_input, 200) frame.set_draw_handler(draw) # start frame init() frame.start()