Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test3.py #975

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 1 addition & 69 deletions Day01-15/code/Day13/test3.py
Original file line number Diff line number Diff line change
@@ -1,69 +1 @@
from random import randint
from threading import Thread
from time import sleep

import pygame


class Color(object):
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (242, 242, 242)

@staticmethod
def random_color():
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
return r, g, b


class Car(object):

def __init__(self, x, y, color):
self._x = x
self._y = y
self._color = color

def move(self):
if self._x + 80 < 950:
self._x += randint(1, 10)

def draw(self, screen):
pygame.draw.rect(screen, self._color,
(self._x, self._y, 80, 40), 0)


def main():

class BackgroundTask(Thread):

def run(self):
while True:
screen.fill(Color.GRAY)
pygame.draw.line(screen, Color.BLACK, (130, 0), (130, 600), 4)
pygame.draw.line(screen, Color.BLACK, (950, 0), (950, 600), 4)
for car in cars:
car.draw(screen)
pygame.display.flip()
sleep(0.05)
for car in cars:
car.move()

cars = []
for index in range(5):
temp = Car(50, 50 + 120 * index, Color.random_color())
cars.append(temp)
pygame.init()
screen = pygame.display.set_mode((1000, 600))
BackgroundTask(daemon=True).start()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()


if __name__ == '__main__':
main()
"from random import randintfrom threading import Threadfrom time import sleep"import pygameclass Color(object):"BLACK (0)"WHITE (255)"GRAY (242)"@staticmethoddef random_color()"r randint(255)"g randint(0)"b = randint(0, 255)"return rgb"class Car(object):def __init__(self, x, y, color):self._x = xself._y = yself._color = colordef move(self):if self._x + 80 < 950: self._x += randint(1, 10)def draw(self, screen):pygame.draw.rect(screen, self._color (self._x, self._y, 80, 40), 0)def main(): class BackgroundTask(Thread):def run(self):while Truescreen.fill(Color.GRAY) pygame.draw.line(screen, Color.BLACK, (130, 0), (130, 600), 4)pygame.draw.line(screen, Color.BLACK, (950, 0), (950, 600), 4)for car in cars:car.draw(screen) pygame.display.flip()sleep(0.05for car in cars:car.move()cars = []for index in range(5):temp = Car(50, 50 + 120 * index, Color.random_color())cars.append(temp)pygame.init() screen = pygame.display.set_mode((1000, 600))BackgroundTask(daemon=True).start(running = True while running:for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit()if __name__ == '__main__': main()