Wednesday, October 21, 2015

Python - OO/Class/Object

Code example1:

class Character:
    def __init__(self, name, initial_health):
        self.name = name
        self.health = initial_health
        self.inventory = []
        
    def __str__(self):
        s  = "Name: " + self.name
        s += " Health: " + str(self.health)
        s += " Inventory: " + str(self.inventory)
        return s
    
    def grab(self, item):
        self.inventory.append(item)
        
    def get_health(self):
        return self.health
    
def example():
    me = Character("Bob", 20)
    print str(me)
    me.grab("pencil")
    me.grab("paper")
    print str(me)
    print "Health:", me.get_health()
    
example()

Wednesday, October 14, 2015

绿卡

Perm:
查状态:
http://www.permtrackr.com/search#?company=MicroStrategy
https://icert.doleta.gov/
https://www.permchecker.com
http://www.permtrackr.com/search
http://dolstats.com/
https://icert.doleta.gov/index.cfm?event=ehLCJRExternal.dspLCRLanding

常识:
http://www.moonbbs.com/thread-520785-1-1.html

A-15189-xxxxx
A means PERM
15 means 2015
189 means July 8th, the 189th day in 2015
On permtrackr, there is a link to each case, you can check the school etc. there.


Monday, October 12, 2015

Python - Mouse Input in GUI

Mouse Input example:

# Examples of mouse input

import simplegui
import math

# intialize globals
WIDTH = 450
HEIGHT = 300
ball_pos = [WIDTH / 2, HEIGHT / 2]
BALL_RADIUS = 15
ball_color = "Red"

# helper function
def distance(p, q):
    return math.sqrt( (p[0] - q[0]) ** 2 + (p[1] - q[1]) ** 2)

# define event handler for mouse click, draw
def click(pos):
    global ball_pos, ball_color
    if distance(pos, ball_pos) < BALL_RADIUS:
        ball_color = "Green"
    else:
        ball_pos = list(pos)
        ball_color = "Red"

def draw(canvas):
    canvas.draw_circle(ball_pos, BALL_RADIUS, 1, "Black", ball_color)

# create frame
frame = simplegui.create_frame("Mouse selection", WIDTH, HEIGHT)
frame.set_canvas_background("White")

# register event handler
frame.set_mouseclick_handler(click)
frame.set_draw_handler(draw)

# start frame
frame.start()

Thursday, October 8, 2015

HTML 101

Example:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>


All HTML documents must start with a type declaration: <!DOCTYPE html>.
The HTML document itself begins with <html> and ends with </html>.

The visible part of the HTML document is between <body> and </body>.

HTML headings are defined with the <h1> to <h6> tags

HTML paragraphs are defined with the <p> tag

HTML links are defined with the <a> tag

HTML images are defined with the <img> tag.
The source file (src), alternative text (alt), and size (width and height) are provided as attributes

HTML elements are written with a start tag, with an end tag, with the content in between:
<tagname>content</tagname>
The HTML element is everything from the start tag to the end tag:
<p>My first HTML paragraph.</p>
<br> is an empty element without a closing tag (the <br> tag defines a line break).
Empty elements can be "closed" in the opening tag like this: <br />

  • HTML elements can have attributes
  • Attributes provide additional information about an element
  • Attributes are always specified in the start tag
  • Attributes come in name/value pairs like: name="value"

Friday, October 2, 2015

Python - Keyboard input in GUI

# Keyboard echo

import simplegui

# initialize state
current_key = ' '

# event handlers
def keydown(key):
    global current_key
    current_key = chr(key)
    
def keyup(key):
    global current_key
    current_key = ' '
    
def draw(c):
    # NOTE draw_text now throws an error on some non-printable characters
    # Since keydown event key codes do not all map directly to
    # the printable character via ord(), this example now restricts
    # keys to alphanumerics
    
    if current_key in "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789":
        c.draw_text(current_key, [10, 25], 20, "Red")    
        
# create frame             
f = simplegui.create_frame("Echo", 35, 35)

# register event handlers
f.set_keydown_handler(keydown)
f.set_keyup_handler(keyup)
f.set_draw_handler(draw)

# start frame
f.start()


import simplegui

# Initialize globals
WIDTH = 600
HEIGHT = 400
BALL_RADIUS = 20

ball_pos = [WIDTH / 2, HEIGHT / 2]

# define event handlers
def draw(canvas):
    canvas.draw_circle(ball_pos, BALL_RADIUS, 2, "Red", "White")

def keydown(key):
    vel = 4
    if key == simplegui.KEY_MAP["left"]:
        ball_pos[0] -= vel
    elif key == simplegui.KEY_MAP["right"]:
        ball_pos[0] += vel
    elif key == simplegui.KEY_MAP["down"]:
        ball_pos[1] += vel
    elif key == simplegui.KEY_MAP["up"]:
        ball_pos[1] -= vel        
    
# create frame 
frame = simplegui.create_frame("Positional ball control", WIDTH, HEIGHT)

# register event handlers
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)

# start frame
frame.start()

Stop Watch game - Python

# template for "Stopwatch: The Game"
import simplegui

# define global variables
width = 300
height = 300
interval = 100
running = False
gameplayed = 0
gamewon = 0
time = 0
position = [80, 150]
text_position = [260, 20]

# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
    tenth = t % 10
    seconds = ((t - tenth)/10) % 60
    if seconds >= 10:
        seconds_in_str = str(seconds)
    else:
        seconds_in_str = "0" + str(seconds)
    minutes = t / 600
    return str(minutes) + ":" + seconds_in_str + "." + str(tenth)
    
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
    global running
    timer.start()
    running = True

def stop():
    global running, gameplayed, gamewon, time
    timer.stop()
    if running == True:
        gameplayed += 1
        if time%10 == 0:
            gamewon += 1
    running = False

def reset():
    global time, gameplayed, gamewon, running
    time = 0
    timer.stop()
    running = False
    gameplayed = 0
    gamewon = 0

# define event handler for timer with 0.1 sec interval
def tick():
    global time
    time += 1

# define draw handler
def draw(canvas):
    global gamewon, gameplayed
    canvas.draw_text(format(time), position, 48, "Red")
    canvas.draw_text(str(gamewon)+"/"+str(gameplayed), text_position, 20, "Green")
    
# create frame
frame = simplegui.create_frame("Stopwatch", width, height)

# register event handlers
frame.set_draw_handler(draw)
timer = simplegui.create_timer(100, tick)
frame.add_button("Start", start, 100)
frame.add_button("Stop", stop, 100)
frame.add_button("Reset", reset, 100)

# start frame
frame.start()
#timer.start()




# Please remember to review the grading rubric

Wednesday, September 30, 2015

Python GUI Debugging Tips

Code1:

#####################
# Example of event-driven code, buggy version

import simplegui

size = 10
radius = 10

# Define event handlers.

def incr_button_handler():
    """Increment the size."""
    global size
    size += 1
    label.set_text("Value: " + str(size))
    
def decr_button_handler():
    """Decrement the size."""
    global size
    # Insert check that size > 1, to make sure it stays positive
    # NOTE that this restriction has changed from the video
    # since draw_circle now throws an error if radius is zero
    if size > 1:
        size -= 1
        label.set_text("Value: " + str(size))

def change_circle_handler():
    """Change the circle radius."""
    global radius
    radius = size
    # Insert code to make radius label change.
    radiuslabel.set_text("Radius: " + str(radius))
    
def draw_handler(canvas):
    """Draw the circle."""
    canvas.draw_circle((100, 100), radius, 5, "Red")

# Create a frame and assign callbacks to event handlers.

frame = simplegui.create_frame("Home", 200, 200)
label = frame.add_label("Value: " + str(size))
frame.add_button("Increase", incr_button_handler)
frame.add_button("Decrease", decr_button_handler)
radiuslabel = frame.add_label("Radius: " + str(radius))
frame.add_button("Change circle", change_circle_handler)
frame.set_draw_handler(draw_handler)

# Start the frame animation

frame.start()