added bckg lines and moved screen func to another file
This commit is contained in:
parent
b61c1ddb6e
commit
78a85cec87
BIN
__pycache__/screen.cpython-310.pyc
Normal file
BIN
__pycache__/screen.cpython-310.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
assets/barn.png
Normal file
BIN
assets/barn.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 42 KiB |
Before Width: | Height: | Size: 818 B After Width: | Height: | Size: 818 B |
Binary file not shown.
Before Width: | Height: | Size: 826 B |
Binary file not shown.
Before Width: | Height: | Size: 792 B |
Binary file not shown.
Before Width: | Height: | Size: 803 B |
34
main.py
34
main.py
@ -1,21 +1,13 @@
|
||||
import pygame
|
||||
import tractor
|
||||
import screen
|
||||
|
||||
#pygame initialization
|
||||
# pygame initialization
|
||||
pygame.init()
|
||||
|
||||
#set screen
|
||||
SCREEN = pygame.display.set_mode((1000, 1000))
|
||||
pygame.display.set_caption("Traktor_interaktor")
|
||||
|
||||
# creating agent
|
||||
myTractor = tractor.Tractor
|
||||
|
||||
#screen background
|
||||
def set_screen():
|
||||
SCREEN.fill((0,100,0))
|
||||
TRACTOR = SCREEN.blit(myTractor.ROTATION_IMG, (myTractor.x, myTractor.y))
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
running = True
|
||||
|
||||
@ -23,19 +15,9 @@ 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.ROTATION_IMG = myTractor.LEFT
|
||||
myTractor.x -= myTractor.speed
|
||||
if keys[pygame.K_RIGHT] and myTractor.x<1000-myTractor.width:
|
||||
myTractor.ROTATION_IMG = myTractor.RIGHT
|
||||
myTractor.x += myTractor.speed
|
||||
if keys[pygame.K_UP] and myTractor.y>0:
|
||||
myTractor.ROTATION_IMG = myTractor.UP
|
||||
myTractor.y -= myTractor.speed
|
||||
if keys[pygame.K_DOWN] and myTractor.y<1000-myTractor.height:
|
||||
myTractor.ROTATION_IMG = myTractor.DOWN
|
||||
myTractor.y += myTractor.speed
|
||||
set_screen()
|
||||
# defines agent movement
|
||||
tractor.movement(myTractor)
|
||||
# screen visualisation
|
||||
screen.set_screen(myTractor)
|
||||
|
||||
pygame.quit()
|
29
screen.py
Normal file
29
screen.py
Normal file
@ -0,0 +1,29 @@
|
||||
import pygame
|
||||
|
||||
# size in pixels of one tile = 36px/36px
|
||||
tile = (36, 36)
|
||||
|
||||
# later move it to another class "barn"?
|
||||
barn_img = pygame.image.load('assets/barn.png')
|
||||
barn = pygame.transform.scale(barn_img, tile)
|
||||
|
||||
# screen settings
|
||||
SIZE = (900, 900)
|
||||
SCREEN = pygame.display.set_mode(SIZE)
|
||||
pygame.display.set_caption("Traktor_interaktor")
|
||||
|
||||
# screen dispaly
|
||||
def set_screen(myTractor):
|
||||
# setting background color
|
||||
SCREEN.fill((90,50,20))
|
||||
|
||||
# dispaly agent icon
|
||||
TRACTOR = SCREEN.blit(myTractor.IMG, (myTractor.x, myTractor.y))
|
||||
# display barn
|
||||
SCREEN.blit(barn, (0, 863))
|
||||
# draw lines(horizontal, vertical) on the screen
|
||||
for line in range(25):
|
||||
pygame.draw.line(SCREEN, (0, 0, 0), (0, line * 36), (SIZE[0], line * 36))
|
||||
pygame.draw.line(SCREEN, (0, 0, 0), (line * 36, 0), (line * 36, SIZE[1]))
|
||||
|
||||
pygame.display.update()
|
44
tractor.py
44
tractor.py
@ -1,19 +1,33 @@
|
||||
import pygame
|
||||
|
||||
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
|
||||
|
||||
#tractor image rotation
|
||||
ROTATION_IMG = pygame.image.load('assets/tractor/tractor_UP.png')
|
||||
UP = pygame.image.load('assets/tractor/tractor_UP.png')
|
||||
DOWN = pygame.image.load('assets/tractor/tractor_DOWN.png')
|
||||
LEFT = pygame.image.load('assets/tractor/tractor_LEFT.png')
|
||||
RIGHT = pygame.image.load('assets/tractor/tractor_RIGHT.png')
|
||||
# this is where tractor spawns when program starts (center)
|
||||
x=432
|
||||
y=432
|
||||
# it's speed -> pixels it moves after pressing arrow
|
||||
speed = 36
|
||||
# tractor image
|
||||
tractor_img = pygame.image.load('assets/tractor/tractor.png')
|
||||
IMG = pygame.transform.scale(tractor_img, (36, 36))
|
||||
# tractor image rotation
|
||||
UP = pygame.transform.rotate(IMG, 0)
|
||||
DOWN = pygame.transform.rotate(IMG, 180)
|
||||
LEFT = pygame.transform.rotate(IMG, 90)
|
||||
RIGHT = pygame.transform.rotate(IMG, -90)
|
||||
|
||||
|
||||
def movement(myTractor):
|
||||
keys = pygame.key.get_pressed()
|
||||
|
||||
if keys[pygame.K_LEFT] and myTractor.x>0:
|
||||
myTractor.IMG = myTractor.LEFT
|
||||
myTractor.x -= myTractor.speed
|
||||
if keys[pygame.K_RIGHT] and myTractor.x<900-myTractor.speed:
|
||||
myTractor.IMG = myTractor.RIGHT
|
||||
myTractor.x += myTractor.speed
|
||||
if keys[pygame.K_UP] and myTractor.y>0:
|
||||
myTractor.IMG = myTractor.UP
|
||||
myTractor.y -= myTractor.speed
|
||||
if keys[pygame.K_DOWN] and myTractor.y<900-myTractor.speed:
|
||||
myTractor.IMG = myTractor.DOWN
|
||||
myTractor.y += myTractor.speed
|
||||
|
Loading…
Reference in New Issue
Block a user