Update test3.py

pull/975/head
Alexander Petree 2023-07-01 13:44:23 -07:00 committed by GitHub
parent 09857a579b
commit a48bb8692e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 1 additions and 69 deletions

View File

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