Python pacman

from tkinter import *
class Player():
def __init__(self, master, radius, xcoordinate=100, ycoordinate=100):
self.master=master
self.xcoordinate = xcoordinate
self.ycoordinate = ycoordinate
self.radius = radius
self.master.title(“World”)
self.master.bind(‘<Up>’, self.moveUp)
self.master.bind(‘<Down>’, self.moveDown)
self.master.bind(‘<Right>’, self.moveRight)
self.master.bind(‘<Left>’, self.moveLeft)
self.draw()

def moveUp(self, event):
self.canvas.move(self.oval_id, 0, -10)

def moveDown(self, event):
self.canvas.move(self.oval_id, 0, 10)

def moveRight(self, event):
self.canvas.move(self.oval_id, 10, 0)

def moveLeft(self, event):
self.canvas.move(self.oval_id, -10, 0)

def draw(self):
self.canvas = Canvas(self.master, width=500, height=500,
borderwidth=0,highlightthickness=0, bg=”black”)
self.canvas.grid()
self.oval_id=self.canvas.create_oval(self.xcoordinate-self.radius,
self.ycoordinate-self.radius,
self.xcoordinate+self.radius,
self.ycoordinate+self.radius,
fill=”yellow”)

class client(Tk):
def __init__(self):
super().__init__()
def draw_player(self, x, y, r, **kwargs):
return self.create_oval(x-r, y-r, x+r, y+r, **kwargs)

Canvas.draw_player = draw_player

master=Tk()
pacman = Player(master, 50)
master.mainloop()

from tkinter import *
import time, sched

class Player():
def __init__(self, master, radius, xcoordinate=100, ycoordinate=100):
self.master=master
self.xcoordinate = xcoordinate
self.ycoordinate = ycoordinate
self.dx=0
self.dy=0
self.radius = radius
self.master.title(“World”)
self.master.bind(”, self.moveUp)
self.master.bind(”, self.moveDown)
self.master.bind(”, self.moveRight)
self.master.bind(”, self.moveLeft)
self.draw()

def move(self):
print (str(self.xcoordinate))
if self.xcoordinate > 10 and self.xcoordinate < 490 and self.ycoordinate > 10 and self.ycoordinate < 490 :
self.canvas.move(self.oval_id, self.dx, self.dy)
else:
self.dx = self.dx*-1
self.dy = self.dy*-1
self.dx = self.dx*-1
self.dy = self.dy*-1
self.canvas.move(self.oval_id, self.dx, self.dy)

self.xcoordinate=self.xcoordinate+self.dx
self.ycoordinate=self.ycoordinate+self.dy

def moveUp(self, event):
self.dx=0
self.dy=-1

def moveDown(self, event):
self.dx=0
self.dy=1

def moveRight(self, event):
self.dx=1
self.dy=0

def moveLeft(self, event):
self.dx=-1
self.dy=0

def draw(self):
self.canvas = Canvas(self.master, width=500, height=500,
borderwidth=0,highlightthickness=0, bg=”black”)
self.canvas.grid()
self.oval_id=self.canvas.create_oval(self.xcoordinate-self.radius,
self.ycoordinate-self.radius,
self.xcoordinate+self.radius,
self.ycoordinate+self.radius,
fill=”yellow”)

class client(Tk):
def __init__(self):
super().__init__()
def draw_player(self, x, y, r, **kwargs):
return self.create_oval(x-r, y-r, x+r, y+r, **kwargs)

Canvas.draw_player = draw_player

master = Tk()
pacman = Player(master, 50)

def m():
pacman.move()
master.after(10, m)

master.after(2000, m)

master.mainloop()

RPI voor beginners Deel 1. Functions

Sequentie

print “Kwadraat berekenen”

n = int(raw_input(“Voer een getaal in: “))
nsquared = n*n

print “Het kwadraat van ” + str(n)+” is “+str(nsquared)

Herhalen

print “Kwadraat berekenen”

while 1:
n = int(raw_input(“Voer een getaal in: “))
nsquared = n*n

print “Het kwadraat van ” + str(n)+” is “+str(nsquared)

Herhalen 2

print “Kwadraat berekenen”

repeatAmount = int(raw_input(“Hoe vaak wil je een kwadraat uitrekenen? “))

c = 0
while c < repeatAmount:
c = c+1
n = int(raw_input(“Voer een getaal in: “))
nsquared = n*n

print “Het kwadraat van ” + str(n)+” is “+str(nsquared)

print “Eind programma”

Keuze

print “Kwadraat berekenen”

n = int(raw_input(“Voer een getaal tussen de 0 en 10 in: “))

while n > 10 or n < 0:
if n > 10:
n = int(raw_input(“Kan niet groter dan 10 zijn: “))
if n < 0:
n = int(raw_input(“Kan niet kleiner dan 0 zijn: “))

nsquared = n*n

print “Het kwadraat van ” + str(n)+” is “+str(nsquared)