Lately I’ve been playing around with the idea of creating Pong in pythons tkinter library. I’ve got drawing blocks in tkinter down, so much so that I’ve made a derpy library for it which will undoubtedly change a lot as i get more practice. If you happen to be classy enough to have Python installed, feel free to load up this bit of code and play around with it. Also, looking back, refresh was a really strange word choice, o well. I still need to add some game logic, a main loop (probably while True: with a time.sleep() delay), and a front end. Who knows if I’ll finish this project, but I’m having fun.
from tkinter import *
import time
import os
#initialize vars
objList = []
def clear():
os.system(“cls”)
class Block:
def __init__(self,x,y,w,h,hue):
self.x = x
self.y = y
self.w = w
self.h = h
self.hue = hue
objList.append(self)
def createCanvas(h,w):
root = Tk()
return Canvas(root, width=w, height=h)
def drawBox(x,y,w,h,hue):
canvas.create_rectangle(x,y,x+w,y+h,fill=hue)
def refresh(bkgHue):
canvas.create_rectangle(0,0,200,200,fill=bkgHue)
for obj in objList:
drawBox(obj.x, obj.y, obj.w, obj.h, obj.hue)
canvas.pack()
#some code to get you started
canvas = createCanvas(200,200)
drawBox(10,10,10,10,”green”)
block1 = Block(10,10,10,10,”green”)
block2 = Block(30,30,10,10,”yellow”)
refresh(“blue”)