Commit 06c97697 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+63 −0
Changes for Python Game Development for Beginners - Working Files/Chapter 1/My Approach to Learning/helpercode.py: 63 added lines, 0 removed lines.
Original line number Diff line number Diff line
import turtle

counterscreen = turtle.Screen()
counterscreen.bgcolor("palegoldenrod")

def checkpos(turtlelist, screen):
  """
  Takes a list of turtle objects and a Screen object.
  Sets the turtles to the opposite edge of the screen.
  """
  screen.tracer(0)
  for turtle in turtlelist:
    dim = 400
    turtle.penup()
    x = turtle.xcor()
    y = turtle.ycor()
    if x > dim:
      turtle.setx(-1 * dim)
    elif x < -1 * dim:
      turtle.setx(dim)
    if y > dim:
      turtle.sety(-1* dim)
    elif y < -1 * dim:
      turtle.sety(dim)
    turtle.pendown()
  screen.tracer(1)
  return

def intersect(object1, object2, radius = 15):
  """
  Takes two Turtle objects and an optional radius.
  Returns True if the objects are less than radius apart.
  """
  xbounds = (object1.xcor()-radius, object1.xcor() + radius)
  ybounds = (object1.ycor()-radius, object1.ycor() + radius)
  x, y = object2.pos()
  check_x = x > min(xbounds) and x < max(xbounds)
  check_y = y > min(ybounds) and y < max(ybounds)
  if (check_x and check_y):
    return True
  else:
    return False
    
class Counter(turtle.Turtle):
  def __init__(self, coordinates = [160, 170], screen = counterscreen):
    turtle.Turtle.__init__(self)
    self.count = 0
    self.hideturtle()
    self.speed(0)
    self.penup()
    self.goto(coordinates)
    self.screen = screen
  def show(self, message, alignment = "right", size = 18):
    self.screen.tracer(0)
    self.clear()
    self.write(message,font=("Arial",size),align=alignment)
    self.screen.tracer(1)
  def increment(self):
    self.count += 1
  def showcount(self, x = 0, y = 0):
    self.increment()
    self.show(self.count)
      
 No newline at end of file
+122 −0
Changes for Python Game Development for Beginners - Working Files/Chapter 1/My Approach to Learning/main.py: 122 added lines, 0 removed lines.
Original line number Diff line number Diff line
import turtle
from helpercode import checkpos, intersect, Counter
import random

screen = turtle.Screen()
screen.setworldcoordinates(-400,-400,400,400)

class Runner(turtle.Turtle):
  def __init__(self):
    turtle.Turtle.__init__(self)
    self.shape('turtle')
    self.penup()
    screen.tracer(10)
    self.setpos(-100,0)
    self.seth(0)
    self.pendown()
    screen.tracer(1)
  def reset(self):
    self.hideturtle()
    self.clear()
    self.__init__()

# A list of all of our chasers
chasers = []

class Chaser(turtle.Turtle):
  def __init__(self, target, coordinates = [100,0]):
    turtle.Turtle.__init__(self)
    self.target = target
    self.start = coordinates
    self.shape('turtle')
    c1 = abs(coordinates[0])
    c2 = abs(coordinates[1])
    c3 = max(255, abs(sum(coordinates)))
    self.color(c1, c2, c3)
    self.penup()
    screen.tracer(0)
    self.setpos(self.start)
    self.seth(self.towards(target))
    self.pendown()
    screen.tracer(1)
    # Add Self to list of Chasers
    chasers.append(self)
  def reset(self):
    self.hideturtle()
    self.clear()
    self.__init__(self.target)
  def track(self, move=False):
    screen.tracer(0)
    self.seth(self.towards(self.target) + random.randint(-25,25))
    self.width(self.distance(self.target.pos()) //20)
    if intersect(self.target,self):
      x,y = self.pos()
      c1 = abs(x)
      c2 = abs(y)
      c3 = max(256, abs(sum([x,y])))
      self.color((c1, c2, c3))
      self.penup()
      self.goto(self.start)
      self.pendown()
    elif move:
      self.forward(9+random.randint(-4,0))
    else:
      self.forward(1)
    checkpos([self.target,self],screen)
    screen.tracer(1)

tina = Runner()
tina.write("Click and Drag!")
tina.backward(20)
tommy = Chaser(tina, [200,-50])
sally = Chaser(tina, [50,200])
johnny = Chaser(tina, [-150,-100])

# Reset the game to original state
def reset():
  tina.reset()
  tommy.reset()

# Define functions for each arrow key
def go_left():
  screen.tracer(0)
  tina.left(7)
  tommy.track()
  
def go_right():
  screen.tracer(0)
  tina.right(7)
  tommy.track()
  
def go_forward():
  screen.tracer(0)
  tina.forward(10)
  tommy.track(move=True)
  
def go_backward():
  screen.tracer(0)
  tina.backward(10)
  tommy.track(move=True)
  
# Tell the program which functions go with which keys
screen.onkey(go_left, 'Left')
screen.onkey(go_right, 'Right')
screen.onkey(go_forward, 'Up')
screen.onkey(go_backward, 'Down')

def drag_function(x,y):
  screen.tracer(0)
  tina.clear()
  tina.goto(x,y)
  for chaser in chasers:
    chaser.track(True)
  screen.tracer(1)

tina.ondrag(drag_function)

# Reset the game when the user presses 'r'
screen.onkey(reset,"r")

# Tell the screen to listen for key presses
screen.listen()
turtle.done()
 No newline at end of file
+1 −0
Changes for Python Game Development for Beginners - Working Files/Chapter 1/My Approach to Learning/turtle.cfg: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
colormode = 255
 No newline at end of file
+15 −0
Changes for Python Game Development for Beginners - Working Files/Chapter 2/Drawing App Sketch/helper_code.py: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
import turtle
import random

class GlowTurtle(turtle.Turtle):
  def __init__(self):
    turtle.Turtle.__init__(self)
    self.pencolor("red")
    self.pencolors = ["red","goldenrod", "purple", "pink"]
    self.fillcolors = ["yellow", "black"]
  def glow(self,x,y):
    self.fillcolor(self.fillcolors[0])
  def unglow(self,x,y):
    self.fillcolor(self.fillcolors[1])
  def new_color(self):
    self.color(random.choice(self.pencolors))
 No newline at end of file
+61 −0
Changes for Python Game Development for Beginners - Working Files/Chapter 2/Drawing App Sketch/main.py: 61 added lines, 0 removed lines.
Original line number Diff line number Diff line
# import the turtle module so we can use all the neat code it contains
import turtle
from helper_code import GlowTurtle

# Create a variable `tina` that is a Turtle() object. Set shape to 'turtle'
tina = GlowTurtle()
tina.shape('turtle')

# Create a variable `screen`, a Screen() object, that will handle keyss
screen = turtle.Screen()

# tina is a GlowTurtle (see helper_code.py), so we can
# call the glow and unglow methods on click and release
tina.onclick(tina.glow)     # clicking on turtle turns fillcolor red,
tina.onrelease(tina.unglow) # releasing turns it to transparent.

# Define a function for each arrow key
def go_left():
  tina.left(7)
  
def go_right():
  tina.right(7)
  
def go_forward():
  tina.forward(10)
  
def go_backward():
  tina.backward(10)
  
# Tell the program which functions go with which keys
screen.onkey(go_left, 'Left')
screen.onkey(go_right, 'Right')
screen.onkey(go_forward, 'Up')
screen.onkey(go_backward, 'Down')

# Call the new_color method when 'c' is presssed
screen.onkey(tina.new_color, 'c')

# Define a function for drags
def on_drag_function(x,y):
  screen.tracer(0)
  tina.goto(x,y)
  screen.tracer(1)

# Call that function when tina s dragged
tina.ondrag(on_drag_function)

# Define a simple function that tells our turtle to go to x,y coordinates
def on_screen_click(x, y):
  screen.tracer(0)
  tina.goto(x, y)
  screen.tracer(1)
  
# when the screen is clicked, run the `on_screen_click` function. 
# The x and y coordinates are passed along to the function.
screen.onclick(on_screen_click)

# Tell the screen to listen for key presses
screen.listen()

turtle.done()
 No newline at end of file
Loading