Created Tractor class and changed main so pressing keys allow to move object

This commit is contained in:
JakubPaszke 2023-03-07 13:00:35 +01:00
parent 7c6bae790c
commit c81df3cd8e
3 changed files with 23 additions and 2 deletions

Binary file not shown.

16
main.py
View File

@ -1,4 +1,5 @@
import pygame import pygame
import tractor
#pygame initialization #pygame initialization
pygame.init() pygame.init()
@ -7,12 +8,14 @@ pygame.init()
SCREEN = pygame.display.set_mode((1000, 1000)) SCREEN = pygame.display.set_mode((1000, 1000))
pygame.display.set_caption("Traktor_interaktor") pygame.display.set_caption("Traktor_interaktor")
myTractor = tractor.Tractor
#screen background #screen background
def set_screen(): def set_screen():
SCREEN.fill((0,100,0)) SCREEN.fill((0,100,0))
pygame.display.update() 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__": if __name__ == "__main__":
running = True running = True
@ -21,6 +24,15 @@ if __name__ == "__main__":
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False 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() set_screen()
pygame.quit() pygame.quit()

9
tractor.py Normal file
View File

@ -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