import pygame import os import serial pygame.init() SIZE = WIDTH, HEIGHT = 230, 230 # sprite code based on code from https://www.simplifiedpython.net/pygame-sprite-animation-tutorial/ class MySprite(pygame.sprite.Sprite): def __init__(self): super(MySprite, self).__init__() #adding all the images to sprite array self.images = [] self.images.append(pygame.image.load('icecreamimages/icecream5.png')) self.images.append(pygame.image.load('icecreamimages/icecream10.png')) self.images.append(pygame.image.load('icecreamimages/icecream15.png')) self.images.append(pygame.image.load('icecreamimages/icecream20.png')) self.images.append(pygame.image.load('icecreamimages/icecream25.png')) self.images.append(pygame.image.load('icecreamimages/icecream30.png')) self.images.append(pygame.image.load('icecreamimages/icecream35.png')) self.images.append(pygame.image.load('icecreamimages/icecream40.png')) self.images.append(pygame.image.load('icecreamimages/icecream45.png')) self.images.append(pygame.image.load('icecreamimages/icecream50.png')) self.images.append(pygame.image.load('icecreamimages/icecream55.png')) self.images.append(pygame.image.load('icecreamimages/icecream60.png')) self.images.append(pygame.image.load('icecreamimages/icecream65.png')) self.images.append(pygame.image.load('icecreamimages/icecream70.png')) self.images.append(pygame.image.load('icecreamimages/icecream75.png')) self.images.append(pygame.image.load('icecreamimages/icecream80.png')) self.images.append(pygame.image.load('icecreamimages/icecream85.png')) self.images.append(pygame.image.load('icecreamimages/icecream90.png')) self.images.append(pygame.image.load('icecreamimages/icecream95.png')) #index value to get the image from the array #initially it is 0 self.index = 18 #now the image that we will display will be the index from the image array self.image = self.images[self.index] #creating a rect at position x,y (5,5) of size (150,198) which is the size of sprite self.rect = pygame.Rect(5, 5, 150, 198) def update(self): #when the update method is called, we will increment the index self.index = self.index - 1 #if the index is larger than the total images if self.index <= 1: #we will make the index to 0 again self.index = 1 #finally we will update the image that will be displayed self.image = self.images[self.index] # Set up the drawing window screen = pygame.display.set_mode([200, 200]) FPS = 10 #Frames per second screen = pygame.display.set_mode(SIZE) #creating our sprite object my_sprite = MySprite() #creating a group with our sprite my_group = pygame.sprite.Group(my_sprite) #getting the pygame clock for handling fps clock = pygame.time.Clock() # Run until the user asks to quit while True: with serial.Serial('/dev/cu.usbserial-AR0K45RZ', 9600, timeout=1) as ser: line = int(ser.readline()[13:15]) events = pygame.event.get() for event in events: if event.type == pygame.QUIT: quit() # if event.type == pygame.KEYDOWN: (this is from testing using keystrokes instead of serial) # if event.key == pygame.K_LEFT: # my_group.update() if line > 84: my_group.update() # Fill the background with white screen.fill((255, 255, 255)) my_group.draw(screen) # Flip the display pygame.display.update() clock.tick(10)