Last active
December 24, 2018 16:21
-
-
Save TheRealNOIG/cbb25dbbc72bd013270fd5147afd5caf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from tkinter import * | |
root = Tk(); | |
root.title = "Test4" | |
root.wm_attributes("-topmost", 1); | |
#CreateCanvas | |
canvas = Canvas(root, width=640, height=480); | |
canvas.pack(); | |
#HandleInput | |
keysDown = []; | |
def keyDown(event): | |
if (event.char not in keysDown): | |
keysDown.append(event.char); | |
def keyUp(event): | |
if (event.char in keysDown): | |
keysDown.remove(event.char); | |
def isKeyDown(key): | |
if (key in keysDown): | |
return True; | |
else: | |
return False; | |
root.bind("<KeyPress>", keyDown); | |
root.bind("<KeyRelease>", keyUp); | |
class Wall(): | |
def __init__(self): | |
self.x = 0; | |
self.y = 0; | |
self.x2 = 100; | |
self.y2 = 0; | |
class Player(): | |
def __init__(self, canvas, x, y): | |
self.canvas = canvas; | |
self.x = x; | |
self.y = y; | |
self.length = 25; | |
self.rotation = 0; | |
self.setAngles(); | |
self.ID = canvas.create_line(self.x, self.y, self.x + self.length * self.angleX, self.y + self.length * self.angleY); | |
self.wall = Wall(); | |
self.wallID = canvas.create_line(self.wall.x, self.wall.y, self.wall.x2, self.wall.y2); | |
def setAngles(self): | |
self.angle = self.rotation * math.pi / 180; | |
print(self.angle) | |
self.angleX = math.cos(self.angle); | |
self.angleY = math.sin(self.angle); | |
def moveForward(self, amount): | |
self.x = (self.x + self.angleY * amount); | |
self.y = (self.y + self.angleX * amount); | |
def update(self): | |
self.setAngles(); | |
self.input(); | |
self.canvas.coords(self.ID, 100, 100, 100, 75); | |
tx1 = self.x - self.wall.x; | |
ty1 = self.y - self.wall.y; | |
tx2 = self.x - self.wall.x2; | |
ty2 = self.y - self.wall.y2; | |
tz1 = tx1 * self.angleY + ty1 * self.angleX; | |
tz2 = tx2 * self.angleY + ty2 * self.angleX; | |
tx1 = tx1 * self.angleX - ty1 * self.angleY; | |
tx2 = tx2 * self.angleX - ty2 * self.angleY; | |
self.canvas.coords(self.wallID, tx1 + 100, tz1 + 100, tx2 + 100, tz2 + 100); | |
def wallToScreen(self, wall): | |
newWall = wall; | |
newWall.x -= self.x; | |
newWall.y -= self.y; | |
def input(self): | |
if (isKeyDown("a")): | |
self.rotation -= 0.1; | |
if (isKeyDown("d")): | |
self.rotation += 0.1; | |
if (isKeyDown("r")): | |
self.rotation = 45; | |
if (isKeyDown("s")): | |
self.moveForward( 0.1); | |
if (isKeyDown("w")): | |
self.moveForward( -0.1); | |
player = Player(canvas, 50, 50); | |
def update(): | |
player.update(); | |
root.after(1, update); | |
update(); | |
root.mainloop(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment