Zaktualizuj 'Traktorek/ran.py'

This commit is contained in:
Kinga Jagodzińska 2020-04-28 19:08:11 +00:00
parent b9fbdaae94
commit 567b65ae92

View File

@ -1,156 +1,118 @@
import pygame import pygame
import sys import sys
import random import random
from Tractor import Tractor from Tractor import Tractor
# dodać growth rate
# Dodać co jeśli pusta
# Pamiętać o zmianie algorytmu po dodaniu growth rate class Game(object):
def __init__(self):
# Config
class Game(object): self.max_tps = 2.0
def __init__(self): self.res = (720, 720)
# Config
self.max_tps = 2.0 self.fields = []
self.res = (720, 720) self.randomize_field()
self.fields = [] # Initialization
self.randomize_field() pygame.init()
self.screen = pygame.display.set_mode(self.res)
# Initialization pygame.display.set_caption('Traktorek')
pygame.init() self.clock = pygame.time.Clock()
self.screen = pygame.display.set_mode(self.res) self.dt = 0.0
pygame.display.set_caption('Traktorek')
self.clock = pygame.time.Clock() self.player = Tractor(self)
self.dt = 0.0
while True:
self.player = Tractor(self) # Handle events
for event in pygame.event.get():
while True: if event.type == pygame.QUIT:
# Handle events sys.exit(0)
for event in pygame.event.get(): elif event.type == pygame.K_ESCAPE:
if event.type == pygame.QUIT: sys.exit(0)
sys.exit(0)
elif event.type == pygame.K_ESCAPE: # Ticking
sys.exit(0) self.dt += self.clock.tick() / 1000.0
while self.dt > 1 / self.max_tps:
# Ticking self.tick()
self.dt += self.clock.tick() / 1000.0 self.dt -= 1 / self.max_tps
while self.dt > 1 / self.max_tps:
self.tick() # Rendering
self.dt -= 1 / self.max_tps self.screen.fill((0, 0, 0))
self.draw()
# Rendering pygame.display.flip()
self.screen.fill((0, 0, 0))
self.draw() def tick(self):
pygame.display.flip() self.player.tick()
def tick(self): def draw(self):
self.player.tick() self.screen.fill((0, 0, 0))
self.draw_field()
def draw(self): self.draw_net()
self.screen.fill((0, 0, 0)) self.player.draw()
self.draw_field() pygame.display.update()
self.draw_net()
self.player.draw() def draw_net(self):
pygame.display.update() color = (255, 255, 255)
for i in range(1, 5):
def draw_net(self): krat = int(720 / 5 * i)
color = (255, 255, 255) # linia pozioma
for i in range(1, 5): pygame.draw.line(self.screen, color, (0, krat), (720, krat), 1)
krat = int(720 / 5 * i) # linia pionowa
# linia pozioma pygame.draw.line(self.screen, color, (krat, 0), (krat, 720), 1)
pygame.draw.line(self.screen, color, (0, krat), (720, krat), 1)
# linia pionowa def randomize_field(self):
pygame.draw.line(self.screen, color, (krat, 0), (krat, 720), 1) for x in range(25):
temp = []
def randomize_field(self): # nasiona
for x in range(25): temp.append(random.choice(["żyto", "jęczmień", "owies", "marchew", "rzodkiew", "pietruszka", "puste"]))
temp = [] # gleba
# nasiona temp.append(random.choice([True, False]))
temp.append(random.choice(["żyto", "jęczmień", "owies", "marchew", "rzodkiew", "pietruszka", "puste"])) # woda
# gleba temp.append(random.choice([True, False]))
temp.append(random.choice([True, False])) # # growth rate
# woda # temp.append(random)
temp.append(random.choice([True, False])) # # cost
# # growth rate if temp[0] == "żyto":
# temp.append(random) temp.append(10)
# # cost elif temp[0] == "jęczmień":
if temp[0] == "żyto": temp.append(12)
temp.append(10) elif temp[0] == "owies":
elif temp[0] == "jęczmień": temp.append(8)
temp.append(12) elif temp[0] == "marchew":
elif temp[0] == "owies": temp.append(14)
temp.append(8) elif temp[0] == "rzodkiew":
elif temp[0] == "marchew": temp.append(7)
temp.append(14) elif temp[0] == "pietruszka":
elif temp[0] == "rzodkiew": temp.append(6)
temp.append(7) elif temp[0] == "puste":
elif temp[0] == "pietruszka": temp.append(2)
temp.append(6) else:
elif temp[0] == "puste": temp.append(0)
temp.append(2)
else: self.fields.append(temp)
temp.append(0)
def draw_field(self):
self.fields.append(temp) for x in range(25):
self.screen.fill(self.color(x), (144 * (x % 5), 144 * (x // 5), 144 * (x % 5 + 1), 144 * (x // 5 + 1)))
def draw_field(self):
for x in range(25): def color(self, z):
self.screen.fill(self.color(x), (144 * (x % 5), 144 * (x // 5), 144 * (x % 5 + 1), 144 * (x // 5 + 1))) if self.fields[z][0] == 'owies':
return (255, 200, 55)
def color(self, z): elif self.fields[z][0] == 'jęczmień':
if self.fields[z][0] == 'owies': return (170, 150, 40)
return (255, 200, 55) elif self.fields[z][0] == 'żyto':
# if self.fields[z][1] == True: return (100, 215, 80)
# if self.fields[z][2] == True: elif self.fields[z][0] == 'marchew':
# return (255, 200, 55) return (224, 60, 14)
# elif self.fields[z][2] == False: elif self.fields[z][0] == 'rzodkiew':
# return (255, 200, 100) return (142, 24, 104)
# elif self.fields[z][1] == False: elif self.fields[z][0] == 'pietruszka':
# if self.fields[z][2] == True: return (254, 247, 246)
# return (255, 170, 55) else:
# elif self.fields[z][2] == False: return (0,0,0)
# return (255, 170, 100)
elif self.fields[z][0] == 'jęczmień':
return (170, 150, 40)
# if self.fields[z][1] == True: if __name__ == "__main__":
# if self.fields[z][2] == True: Game()
# return (170, 150, 40)
# elif self.fields[z][2] == False:
# return (170, 150, 85)
# elif self.fields[z][1] == False:
# if self.fields[z][2] == True:
# return (170, 120, 40)
# elif self.fields[z][2] == False:
# return (170, 120, 85)
elif self.fields[z][0] == 'żyto':
return (100, 215, 80)
# if self.fields[z][1] == True:
# if self.fields[z][2] == True:
# return (100, 215, 80)
# elif self.fields[z][2] == False:
# return (100, 215, 125)
# elif self.fields[z][1] == False:
# if self.fields[z][2] == True:
# return (100, 185, 80)
# elif self.fields[z][2] == False:
# return (100, 185, 125)
elif self.fields[z][0] == 'marchew':
return (224, 60, 14)
elif self.fields[z][0] == 'rzodkiew':
return (142, 24, 104)
elif self.fields[z][0] == 'pietruszka':
return (254, 247, 246)
else:
return (0,0,0)
if __name__ == "__main__":
Game()