From c81df3cd8e839ab62e936840be15110239807c3e Mon Sep 17 00:00:00 2001 From: JakubPaszke Date: Tue, 7 Mar 2023 13:00:35 +0100 Subject: [PATCH] Created Tractor class and changed main so pressing keys allow to move object --- __pycache__/tractor.cpython-310.pyc | Bin 0 -> 357 bytes main.py | 16 ++++++++++++++-- tractor.py | 9 +++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 __pycache__/tractor.cpython-310.pyc create mode 100644 tractor.py diff --git a/__pycache__/tractor.cpython-310.pyc b/__pycache__/tractor.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..420e690715fbb58a3769a4549d6fff736a3f6730 GIT binary patch literal 357 zcmY*UJx{|h5Vf78iK=F)_yLG54CD_W)QznRoxE6S&my4>E{-GU41b1Sgyac{sXG%B z&j}K6(tG#icXu{i>)*TL9fD zNHfWaNkLpjKukg=6v+vYQVf0K)0M3@&e-M?%44|Zcmi4$na`BEuUf5?pDNWF>USZY zDfQ4-yPpn!^8DbFZm%`@<>14A)pKLjKw16fXMjJSr()J9X>`m9{cQayz^?%3zX*U8;4R&sQ>@~ literal 0 HcmV?d00001 diff --git a/main.py b/main.py index 7f4e56d..92a9e04 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import pygame +import tractor #pygame initialization pygame.init() @@ -7,12 +8,14 @@ pygame.init() SCREEN = pygame.display.set_mode((1000, 1000)) pygame.display.set_caption("Traktor_interaktor") +myTractor = tractor.Tractor + #screen background def set_screen(): SCREEN.fill((0,100,0)) pygame.display.update() - - + TRACTOR = pygame.draw.rect(SCREEN, (255, 255, 255), (myTractor.x, myTractor.y, myTractor.width, myTractor.height)) + pygame.display.update() if __name__ == "__main__": running = True @@ -21,6 +24,15 @@ if __name__ == "__main__": for event in pygame.event.get(): if event.type == pygame.QUIT: running = False + keys = pygame.key.get_pressed() + if keys[pygame.K_LEFT] and myTractor.x>0: + myTractor.x -= myTractor.speed + if keys[pygame.K_RIGHT] and myTractor.x<1000-myTractor.width: + myTractor.x += myTractor.speed + if keys[pygame.K_UP] and myTractor.y>0: + myTractor.y -= myTractor.speed + if keys[pygame.K_DOWN] and myTractor.y<1000-myTractor.height: + myTractor.y += myTractor.speed set_screen() pygame.quit() \ No newline at end of file diff --git a/tractor.py b/tractor.py new file mode 100644 index 0000000..306c088 --- /dev/null +++ b/tractor.py @@ -0,0 +1,9 @@ +class Tractor: + #this is where tractor spawns when program starts (center) + x=500 + y=500 + #it's speed -> pixels it moves after pressing arrow + speed = 10 + #it's size + width = 20 + height = 20 -- 2.20.1