Python Forum

Full Version: Rev Counter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have to make a program that looks like car rev counter. When I press key on keyboard(later it will be accelerator pedal), i get feedback how long I have been holding specific key (w). Now I need to make the graphical part, that would show rev counter and would move needle depanding on duration of the press.
It should look something like this: https://previews.123rf.com/images/rastud...rgrund.jpg

"
import time
import pygame
import os
os.environ["SDL_VIDEO_CENTERED"] = "1"

screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Time")
clock = pygame.time.Clock()

pygame.init()

clock = pygame.time.Clock()

running = True       
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            break
        if event.type == pygame.KEYDOWN:
            
            if event.key == pygame.K_w: 
                t = time.time()
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w: 
                t = time.time() - t; t = str(t); t = t[:5]
                print("You are doing" ,t," rpm.")


        screen.fill((255, 255, 255))
        pygame.display.update()
        clock.tick(40)
        

import tkinter as tk
import math

class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y
"

Kind regards,
Mario
It would be similar to the following codes in regards to rotation. The background of the meter would be stationary, while the needle rotates at the bottom. The needle would be limited to the degrees at which it rotates depending on the lowest/highest point of the meter. Then it would increment via time key pressed and reset by another key. Then to make it more presentable you would place a stationary half moon shaped object (same appearance as the background) drawn overtop of the needle at the bottom of the pivot point so you only see the top of the needle.


import pygame as pg
 
screen = pg.display.set_mode((800,600))
screen_rect = screen.get_rect()
clock = pg.time.Clock()
done = False
 
class Rotator:
    def __init__(self, screen_rect):
        self.screen_rect = screen_rect
        self.master_image = pg.Surface([100,100]).convert_alpha()
        self.master_image.fill((255,0,0))
        self.image = self.master_image.copy()
        self.rect = self.image.get_rect(center=self.screen_rect.center)
        self.delay = 10
        self.timer = 0.0
        self.angle = 0
 
    def new_angle(self):
        self.angle -= 1
        self.angle %= 360
 
    def rotate(self):
        self.new_angle()
        self.image = pg.transform.rotate(self.master_image, self.angle)
        self.rect = self.image.get_rect(center=self.screen_rect.center)
 
    def update(self):
        if pg.time.get_ticks()- self.timer > self.delay:
            self.timer = pg.time.get_ticks()
            self.rotate()
 
    def draw(self, surf):
        surf.blit(self.image, self.rect)
 
rotator = Rotator(screen_rect)
 
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    screen.fill((0,0,0))
    rotator.update()
    rotator.draw(screen)
    pg.display.update()
another rotation example
import pygame as pg
import math
 
class Rotator:
    def __init__(self, screen_rect):
        self.radar = screen_rect.center
        self.radar_len = 100
        self.angle = 0
        self.image = pg.Surface([25,25]).convert()
        self.get_pos()
        self.rect = self.image.get_rect(center=(self.x, self.y))
        self.image.fill((255,0,0))
        self.speed = 200
 
    def render(self, screen):
        screen.blit(self.image, self.rect)
        pg.draw.line(screen, (255,255,255), self.radar, (self.x,self.y), 1)
         
    def update(self, seconds):
        keys = pg.key.get_pressed()
        self.get_pos()
        self.rect.center = (self.x, self.y)
        if keys[pg.K_RIGHT] or keys[pg.K_d]:
            self.angle -= self.speed * seconds
        elif keys[pg.K_LEFT] or keys[pg.K_a]:
            self.angle += self.speed * seconds
         
    def get_pos(self):
        self.x = self.radar[0] + math.cos(math.radians(self.angle)) * self.radar_len
        self.y = self.radar[1] + math.sin(math.radians(self.angle)) * self.radar_len
 
if __name__ == '__main__':
    running = True
    pg.init()
    screen = pg.display.set_mode((600,400))
    screen_rect = screen.get_rect()
    rotator = Rotator(screen_rect)
    clock = pg.time.Clock()
    seconds = 0
 
    while running:
        screen.fill((0,0,0))
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            elif event.type == pg.MOUSEBUTTONDOWN:
                rotator.radar = pg.mouse.get_pos()
        rotator.update(seconds)
        rotator.render(screen)
        pg.display.set_caption('x,y:{} {}'.format(rotator.x, rotator.y))
        pg.display.update()
        milli = clock.tick(60)
        seconds = milli / 1000.0