ruch_traktora #5
44
Tractor.py
Normal file
44
Tractor.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import pygame
|
||||||
|
import Slot
|
||||||
|
import random
|
||||||
|
CUBE_SIZE=128
|
||||||
|
WIDTH = 1024
|
||||||
|
HEIGHT = 512
|
||||||
|
|
||||||
|
class Tractor:
|
||||||
|
def __init__(self,x_axis,y_axis,screen):
|
||||||
|
self.x_axis=x_axis
|
||||||
|
self.y_axis=y_axis
|
||||||
|
self.tractor_image = pygame.image.load('images/traktor.png')
|
||||||
|
self.tractor_image = pygame.transform.scale(self.tractor_image, (CUBE_SIZE, CUBE_SIZE))
|
||||||
|
self.screen=screen
|
||||||
|
self.slot=None
|
||||||
|
|
||||||
|
def draw_tractor(self):
|
||||||
|
self.screen.blit(self.tractor_image, (self.x_axis*CUBE_SIZE,self.y_axis*CUBE_SIZE))
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
def move_tractor(self,x):
|
||||||
|
if(x==0):
|
||||||
|
if(self.x_axis+1<=7):
|
||||||
|
print("Ruch w prawo")
|
||||||
|
self.x_axis=self.x_axis+1
|
||||||
|
if(x==1):
|
||||||
|
if(self.x_axis-1>=0):
|
||||||
|
print("Ruch w lewo")
|
||||||
|
self.x_axis=self.x_axis-1
|
||||||
|
if(x==2):
|
||||||
|
if(self.y_axis+1<=3):
|
||||||
|
print("Ruch w gore")
|
||||||
|
self.y_axis=self.y_axis+1
|
||||||
|
if(x==3):
|
||||||
|
if(self.y_axis-1>=0):
|
||||||
|
print("Ruch w dol")
|
||||||
|
self.y_axis=self.y_axis-1
|
||||||
|
self.draw_tractor()
|
||||||
|
|
||||||
|
|
||||||
|
def random_move(self):
|
||||||
|
x=random.randint(0,3)
|
||||||
|
self.move_tractor(x)
|
||||||
|
|
24
main.py
24
main.py
@ -1,8 +1,10 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import Slot
|
import Slot
|
||||||
|
import Tractor
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
|
|
||||||
@ -23,7 +25,8 @@ pygame.display.update()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#Tractor creation
|
||||||
|
traktor=Tractor.Tractor(0,0,screen)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -41,11 +44,7 @@ def draw_grid():
|
|||||||
tractor_image = pygame.transform.scale(tractor_image, (CUBE_SIZE, CUBE_SIZE))
|
tractor_image = pygame.transform.scale(tractor_image, (CUBE_SIZE, CUBE_SIZE))
|
||||||
screen.blit(tractor_image, (CUBE_SIZE - 128, CUBE_SIZE - 128))"""
|
screen.blit(tractor_image, (CUBE_SIZE - 128, CUBE_SIZE - 128))"""
|
||||||
|
|
||||||
def draw_tractor(): #TODO? I think we should move draw_tractor to tractor class in the future
|
|
||||||
tractor_image = pygame.image.load('images/traktor.png')
|
|
||||||
tractor_image = pygame.transform.scale(tractor_image, (CUBE_SIZE, CUBE_SIZE))
|
|
||||||
screen.blit(tractor_image, (CUBE_SIZE - 128, CUBE_SIZE - 128))
|
|
||||||
pygame.display.update()
|
|
||||||
#Draw grid and tractor (new one)
|
#Draw grid and tractor (new one)
|
||||||
def draw_grid():
|
def draw_grid():
|
||||||
for x in range(0,WIDTH//CUBE_SIZE): #We got 8 cubes in X axis so we use for from 0 to 7 do draw them all
|
for x in range(0,WIDTH//CUBE_SIZE): #We got 8 cubes in X axis so we use for from 0 to 7 do draw them all
|
||||||
@ -53,8 +52,8 @@ def draw_grid():
|
|||||||
new_slot=Slot.Slot(x,y,BROWN,screen) #Creation of empty slot
|
new_slot=Slot.Slot(x,y,BROWN,screen) #Creation of empty slot
|
||||||
SLOT_DICT[(x,y)]=new_slot #Adding slots to dict
|
SLOT_DICT[(x,y)]=new_slot #Adding slots to dict
|
||||||
for entity in SLOT_DICT:
|
for entity in SLOT_DICT:
|
||||||
SLOT_DICT[entity].draw() #For each slot in dictionary draw it on the screen
|
SLOT_DICT[entity].draw()
|
||||||
draw_tractor()
|
traktor.draw_tractor()
|
||||||
|
|
||||||
def change_color_of_slot(coordinates,color): #Coordinates must be tuple (x,y) x from range 0,7 and y in range 0,3 (left top slot has cord (0,0) ), color has to be from defined or custom in RGB value (R,G,B)
|
def change_color_of_slot(coordinates,color): #Coordinates must be tuple (x,y) x from range 0,7 and y in range 0,3 (left top slot has cord (0,0) ), color has to be from defined or custom in RGB value (R,G,B)
|
||||||
SLOT_DICT[coordinates].color_change(color)
|
SLOT_DICT[coordinates].color_change(color)
|
||||||
@ -78,16 +77,21 @@ def randomize_colors():
|
|||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
for coordinates in SLOT_DICT:
|
for coordinates in SLOT_DICT:
|
||||||
SLOT_DICT[coordinates].color_change(random_color())
|
SLOT_DICT[coordinates].color_change(random_color())
|
||||||
draw_tractor()
|
traktor.draw_tractor()
|
||||||
|
|
||||||
def init_demo(): #Demo purpose
|
def init_demo(): #Demo purpose
|
||||||
draw_grid()
|
draw_grid()
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
randomize_colors()
|
randomize_colors()
|
||||||
|
|
||||||
#Main program
|
|
||||||
|
def demo_move():
|
||||||
|
SLOT_DICT[(traktor.x_axis,traktor.y_axis)].draw()
|
||||||
|
traktor.random_move()
|
||||||
init_demo()
|
init_demo()
|
||||||
while True:
|
while True:
|
||||||
|
time.sleep(1)
|
||||||
|
demo_move()
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
quit()
|
quit()
|
||||||
|
Loading…
Reference in New Issue
Block a user