Ctrmint
Posts: 3
Joined: Fri Dec 16, 2016 9:24 pm

for event in pygame.event.get(): results in IndexError

Thu Dec 22, 2016 9:14 pm

Hi,
I've been writing an application for my Raspberry Pi 3 in Python, which uses a number of libs including Pygame. My code worked on a Linux VM and also on Wheezy with an original Pi. However when I attempted to run the code on my 3 using Jessie I receive the following error.

Code: Select all

File "dash_f1.py" line 714, in game_loop
      for event in pygame.event.get():
IndexError: array index out of range
I can't see any issues with my code, but obviously there is an issue some where.

I wonder if anyone has some idea where the problem might be.
Thanks

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: for event in pygame.event.get(): results in IndexError

Fri Dec 23, 2016 7:49 am

Paste your full code
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Ctrmint
Posts: 3
Joined: Fri Dec 16, 2016 9:24 pm

Re: for event in pygame.event.get(): results in IndexError

Sat Dec 24, 2016 1:16 pm

Code: Select all

#! /usr/bin/env python
# ---------------------------------------------------------------
# Codemasters F1 2016 UDP Telemetry Dashboard
# Revision  :   0.1
# Author    :   Mark Rodman
# ---------------------------------------------------------------

import os, sys, pygame, time, datetime
from dash_network import net_rx
from dash_network import receiver
from pygame.locals import *
from dash_support import *

class Rpm_ball(object):
    def __init__(self, name, revs, colour_int):
        self.name = name
        self.revs = revs
        self.colour_int = colour_int
        self.colour = BLACK
        self.start_x = 25
        self.start_y = 25
        self.x_inc = 50
        self.size = 20

    def draw_on(self):
        if self.colour_int == 1:
            sl_colour = GREEN
        elif self.colour_int == 2:
            sl_colour = SL_YELL_ON
        else:
            sl_colour = RED
        pygame.draw.circle(windowSurface, sl_colour, ((rpm_ball_start_x + (self.revs * self.x_inc)), rpm_ball_start_y),
                           self.size, 0)

    def draw_off(self):
        if self.colour_int == 1:
            sl_colour = SL_GREEN_OFF
        elif self.colour_int == 2:
            sl_colour = SL_YELL_OFF
        else:
            sl_colour = SL_RED_OFF
        pygame.draw.circle(windowSurface, sl_colour, ((rpm_ball_start_x + (self.revs * self.x_inc)), rpm_ball_start_y),
                           self.size, 0)

    def draw_blank(self):
        pygame.draw.circle(windowSurface, SL_OFF, ((rpm_ball_start_x + (self.revs * self.x_inc)), rpm_ball_start_y),
                           self.size, 0)

    def draw_max(self):
        pygame.draw.circle(windowSurface, SL_MAX, ((rpm_ball_start_x + (self.revs * self.x_inc)), rpm_ball_start_y),
                           self.size, 0)


class Basic_instrument_text(object):
    def __init__(self, name, text_string, fore_colour, back_colour, loc_x, loc_y):
        self.name = name
        self.text_string = text_string
        self.fore_colour = fore_colour
        self.back_colour = back_colour
        self.loc_x = loc_x
        self.loc_y = loc_y

    def draw_text(self):
        text = instruFont.render(self.text_string, True, self.fore_colour, self.back_colour)
        text_rect = text.get_rect()
        text_rect.centerx = 200
        text_rect.centery = 50
        # draw the text onto the surface
        windowSurface.blit(text, (self.loc_x, self.loc_y))


class Gear_indicator(object):
    def __init__(self, name, gear_state, fore_colour, back_colour):
        self.name = name
        self.gear_state = gear_state
        self.fore_colour = fore_colour
        self.back_colour = back_colour
        self.gear_conv_list = ['R', 'N', '1', '2', '3', '4', '5', '6', '7', '8']
        self.old_gear = self.gear_conv_list[self.gear_state]

    def draw_gear(self):
        # omitted back colour box
        text = basicFont.render(self.gear_conv_list[self.gear_state], True, self.fore_colour)
        text_rect = text.get_rect()
        text_rect.centerx = gear_x
        text_rect.centery = gear_y
        # draw the text onto the surface
        windowSurface.blit(text, text_rect)


    def update_gear(self, gear_state):
        # Draw rectangle onto screen to cover old gear
        pygame.draw.rect(windowSurface, BLACK, [348, 80, 122, 218])
        text = basicFont.render(self.gear_conv_list[gear_state], True, self.fore_colour)
        text_rect = text.get_rect()
        text_rect.centerx = gear_x
        text_rect.centery = gear_y
        # draw the text onto the surface
        windowSurface.blit(text, text_rect)


class Big_text(object):
    def __init__(self, name, value_float, value_forecolour, text_center_x, text_center_y, box_x1, box_y1, box_x2,
                 box_y2, box_colour):
        self.name = name                                                             # instance name
        self.value_float = value_float                                               # text value, float etc
        self.value_forecolour = value_forecolour                                     # text colour
        self.text_center_x = text_center_x                                           # Co-ordinates of text, x
        self.text_center_y = text_center_y                                           # Co-ordinates of text, y
        self.box_x1 = box_x1                                                         # Co-ordinates used in clearing box
        self.box_y1 = box_y1                                                         # Co-ordinates used in clearing box
        self.box_x2 = box_x2                                                         # Co-ordinates used in clearing box
        self.box_y2 = box_y2                                                         # Co-ordinates used in clearing box
        self.box_colour = box_colour

    def draw(self):
        current_value = int(self.value_float)
        value_text = bigFont.render(str(current_value).ljust(1, ' '), True, self.value_forecolour)
        value_rect = value_text.get_rect()
        value_rect.centerx = self.text_center_x
        value_rect.centery = self.text_center_y
        windowSurface.blit(value_text, value_rect)

    def update(self, value_float):
        pygame.draw.rect(windowSurface, self.box_colour, [self.box_x1, self.box_y1, self.box_x2, self.box_y2])
        self.value_float = value_float
        value_text = bigFont.render(str(int(self.value_float)).ljust(1, ' '), True, self.value_forecolour)
        value_rect = value_text.get_rect()
        value_rect.centerx = self.text_center_x
        value_rect.centery = self.text_center_y
        windowSurface.blit(value_text, value_rect)
        pygame.display.update()


class Mid_text(object):
    def __init__(self, name, value_float, value_forecolour, text_center_x, text_center_y, box_x1, box_y1, box_x2,
                 box_y2, box_colour):
        self.name = name                                                             # instance name
        self.value_float = value_float                                               # text value, float etc
        self.value_forecolour = value_forecolour                                     # text colour
        self.text_center_x = text_center_x                                           # Co-ordinates of text, x
        self.text_center_y = text_center_y                                           # Co-ordinates of text, y
        self.box_x1 = box_x1                                                         # Co-ordinates used in clearing box
        self.box_y1 = box_y1                                                         # Co-ordinates used in clearing box
        self.box_x2 = box_x2                                                         # Co-ordinates used in clearing box
        self.box_y2 = box_y2                                                         # Co-ordinates used in clearing box
        self.box_colour = box_colour

    def draw(self):
        current_value = int(self.value_float)
        value_text = midFont.render(str(current_value).ljust(1, ' '), True, self.value_forecolour)
        value_rect = value_text.get_rect()
        value_rect.centerx = self.text_center_x
        value_rect.centery = self.text_center_y
        windowSurface.blit(value_text, value_rect)

    def update(self, value_float):
        pygame.draw.rect(windowSurface, self.box_colour, [self.box_x1, self.box_y1, self.box_x2, self.box_y2])
        self.value_float = value_float
        value_text = midFont.render(str(int(self.value_float)).ljust(1, ' '), True, self.value_forecolour)
        value_rect = value_text.get_rect()
        value_rect.centerx = self.text_center_x
        value_rect.centery = self.text_center_y
        windowSurface.blit(value_text, value_rect)
        pygame.display.update()


class Mid_text_float(object):
    def __init__(self, name, value_float, value_forecolour, text_center_x, text_center_y, box_x1, box_y1, box_x2,
                 box_y2, box_colour):
        self.name = name                                                             # instance name
        self.value_float = value_float                                               # text value, float etc
        self.value_forecolour = value_forecolour                                     # text colour
        self.text_center_x = text_center_x                                           # Co-ordinates of text, x
        self.text_center_y = text_center_y                                           # Co-ordinates of text, y
        self.box_x1 = box_x1                                                         # Co-ordinates used in clearing box
        self.box_y1 = box_y1                                                         # Co-ordinates used in clearing box
        self.box_x2 = box_x2                                                         # Co-ordinates used in clearing box
        self.box_y2 = box_y2                                                         # Co-ordinates used in clearing box
        self.box_colour = box_colour

    def draw(self):
        current_value = (self.value_float)
        value_text = midFont.render(str(current_value).ljust(1, ' '), True, self.value_forecolour)
        value_rect = value_text.get_rect()
        value_rect.centerx = self.text_center_x
        value_rect.centery = self.text_center_y
        windowSurface.blit(value_text, value_rect)

    def update(self, value_float):
        pygame.draw.rect(windowSurface, self.box_colour, [self.box_x1, self.box_y1, self.box_x2, self.box_y2])
        self.value_float = round(value_float, 3)
        value_text = midFont.render(str((self.value_float)).ljust(1, ' '), True, self.value_forecolour)
        value_rect = value_text.get_rect()
        value_rect.centerx = self.text_center_x
        value_rect.centery = self.text_center_y
        windowSurface.blit(value_text, value_rect)
        pygame.display.update()


class logo_text(object):
    def __init__(self, name, team, team_forecolour):
        self.name = name
        self.team = team
        self.team_forecolour = team_forecolour

    def draw_logo(self):
        team_logo_text = logoFont.render(self.team.center(20, ' '), True, self.team_forecolour)
        logo_rect = team_logo_text.get_rect()
        logo_rect.centerx = (display_width/2)
        logo_rect.centery = (display_height/2)+100
        windowSurface.blit(team_logo_text, logo_rect)

    def update_logo(self, team):
        self.team = team

        team_logo_text = logoFont.render(self.team.center(20, ' '), True, self.team_forecolour, BLACK)
        logo_rect = team_logo_text.get_rect()
        logo_rect.centerx = (display_width/2)
        logo_rect.centery = (display_height/2)+100
        windowSurface.blit(team_logo_text, logo_rect)


class sector_timer(object):
    def __init__(self, default_list, current_sector, which_sector):
        self.sector_times = default_list
        self.current_sector = current_sector
        self.which_sector = which_sector
        self.sector_purple = 0

    def update(self, current_sector):
        if current_sector != 0.0:
            if current_sector != self.current_sector:
                self.current_sector = current_sector
                self.sector_times.append(current_sector)
                length1 = (len(self.sector_times)-1)
                length2 = (len(self.sector_times)-2)
                length3 = (len(self.sector_times)-3)

                if self.which_sector == 1:
                    if length1 > -1:
                        my_s1_val1.update(self.sector_times[length1])
                    if length2 > -1:
                        my_s1_val2.update(self.sector_times[length2])
                    if length3 > -1:
                        my_s1_val3.update(self.sector_times[length3])

                    if self.sector_purple > current_sector:
                        self.sector_purple = current_sector
                        my_s1_purple.update(self.sector_purple)

                    if self.sector_purple == 0.0:
                        self.sector_purple = current_sector
                        my_s1_purple.update(self.sector_purple)

                if self.which_sector == 2:
                    if length1 > -1:
                        my_s2_val1.update(self.sector_times[length1])
                    if length2 > -1:
                        my_s2_val2.update(self.sector_times[length2])
                    if length3 > -1:
                        my_s2_val3.update(self.sector_times[length3])

                    if self.sector_purple > current_sector:
                        self.sector_purple = current_sector
                        my_s2_purple.update(self.sector_purple)

                    if self.sector_purple == 0.0:
                        self.sector_purple = current_sector
                        my_s2_purple.update(self.sector_purple)


class pedal_line(object):
    def __init__(self, name, pedal_value, line_width, line_colour, start_x, start_y):
        self.name = name
        self.pedal_value = pedal_value
        self.line_width = line_width
        self.line_colour = line_colour
        self.start_x = start_x
        self.start_y = start_y
        self.max_length = 100

        #    def draw_line(self):
    def draw(self, pedal_value):
        self.pedal_value = pedal_value

        colour_green = 70 + self.pedal_value * 125          # Calc green based on start 70 + function of pedal value
        colour = (0, colour_green, 0)                       # Use colour

        pygame.draw.line(windowSurface, BLACK, (self.start_x, self.start_y), (self.start_x, self.start_y-200), self.line_width)

        pygame.draw.line(windowSurface, colour, (self.start_x, self.start_y),
                         (self.start_x, (self.start_y - (self.pedal_value*200))), self.line_width)


class purple_lap(object):
    def __init__(self, name, purple_laptime, new_laptime):
        self.name = name
        self.purple_laptime = purple_laptime
        self.new_laptime = new_laptime

    def update(self, new_laptime):
        if self.purple_laptime == 0.0:
            if new_laptime > 0.0:
                self.purple_laptime = new_laptime
                my_purple_lap_text.update(self.purple_laptime)

        elif new_laptime < self.purple_laptime:
            self.purple_laptime = new_laptime
            my_purple_lap_text.update(self.purple_laptime)


# set up pygame
pygame.init()
pygame.font.init()


# find fonts  ----
available_fonts = pygame.font.get_fonts()
for font in range(len(available_fonts)):
    if available_fonts[font] == LCD_font:
        fontpath = pygame.font.match_font(available_fonts[font])

# set up fonts
basicFont = pygame.font.Font(fontpath, gear_fontsize)
instruFont = pygame.font.SysFont(None, instru_fontsize)
logoFont = pygame.font.SysFont(None, logo_fontsize)
# rpmFont = pygame.font.SysFont(None, rpm_fontsize)
bigFont = pygame.font.Font(fontpath, big_fontsize)
midFont = pygame.font.Font(fontpath, mid_fontsize)
mphFont = pygame.font.SysFont(None, rpm_fontsize)
brakeFont = pygame.font.SysFont(None, rpm_fontsize)

# set up the window
windowSurface = pygame.display.set_mode((display_width, display_height), 0, 32)
pygame.display.set_caption(display_title)

# draw the black background onto the surface
windowSurface.fill(BLACK)

def inital_setup():
    # draw screen formatting
    pygame.draw.line(windowSurface, TEXT_BG, (00, 50), (display_width, 50), display_line_width)
    pygame.draw.line(windowSurface, TEXT_BG, (00, 350), (display_width, 350), display_line_width)
    pygame.draw.line(windowSurface, TEXT_BG, (190, 50), (190, 500), display_line_width)
    pygame.draw.line(windowSurface, TEXT_BG, ((display_width - 190), 50), ((display_width - 190), 350),
                     display_line_width)

    # Small lines around brake and psi
    pygame.draw.line(windowSurface, TEXT_BG, (0, 275), (190, 275), display_line_width)
    pygame.draw.line(windowSurface, TEXT_BG, (0, 297), (190, 297), display_line_width)

    # Small lines around DRS
    pygame.draw.line(windowSurface, TEXT_BG, (0, 200), (190, 200), display_line_width)
    pygame.draw.line(windowSurface, TEXT_BG, (0, 222), (190, 222), display_line_width)

    # Small lines around fuel
    pygame.draw.line(windowSurface, TEXT_BG, ((display_width-190), 275), (display_width, 275), display_line_width)
    pygame.draw.line(windowSurface, TEXT_BG, ((display_width-190), 297), (display_width, 297), display_line_width)

    # Small lines around ???
    pygame.draw.line(windowSurface, TEXT_BG, ((display_width-190), 200), (display_width, 200), display_line_width)
    pygame.draw.line(windowSurface, TEXT_BG, ((display_width-190), 222), (display_width, 222), display_line_width)

    # sector 1 line delimiter
    pygame.draw.line(windowSurface, TEXT_BG, (00, 410), (display_width, 410), display_line_width)


    rpm_key = Basic_instrument_text("rpm_key", "RPM", TEXT_INSTRU, BLACK, 112, 98)
    mph_key = Basic_instrument_text("mph_key", "MPH", TEXT_INSTRU, BLACK, (display_width - 120), 98)
    brake_key = Basic_instrument_text("Brake_key", "BRAKE", TEXT_INSTRU, BLACK, 20, 279)
    psi_key = Basic_instrument_text("PSI_key", "PSI", TEXT_INSTRU, BLACK, 135, 279)

    fuel_mass_key = Basic_instrument_text("Fuel_mass_key", "FUEL MASS", TEXT_INSTRU, BLACK, 635, 279)
    fuel_Perc_key = Basic_instrument_text("Fuel_mass_%", "FUEL %", TEXT_INSTRU, BLACK, 740, 279)

    lap_count_key = Basic_instrument_text("Current_Lap", "CURRENT LAP", TEXT_INSTRU, BLACK, 620, 205)
    position_key = Basic_instrument_text("position_key", "POSITION", TEXT_INSTRU, BLACK, display_width-72, 205)

    drs_key = Basic_instrument_text("DRS_key", "DRS", TEXT_INSTRU, BLACK, 70, 204)

    rpm_key.draw_text()
    mph_key.draw_text()
    brake_key.draw_text()
    psi_key.draw_text()
    fuel_mass_key.draw_text()
    fuel_Perc_key.draw_text()
    drs_key.draw_text()
    lap_count_key.draw_text()
    position_key.draw_text()


    return


def initial_rpm():
    # Instantiate RPM ball objects
    global rpm_1k
    global rpm_2k
    global rpm_3k
    global rpm_4k
    global rpm_5k
    global rpm_6k
    global rpm_7k
    global rpm_8k
    global rpm_9k
    global rpm_10k
    global rpm_11k
    global rpm_12k
    global rpm_13k
    global rpm_14k
    global rpm_15k
    global rpm_16k
    rpm_1k = Rpm_ball("rpm_1k", 0, 1)
    rpm_2k = Rpm_ball("rpm_2k", 1, 1)
    rpm_3k = Rpm_ball("rpm_3k", 2, 1)
    rpm_4k = Rpm_ball("rpm_4k", 3, 1)
    rpm_5k = Rpm_ball("rpm_5k", 4, 1)
    rpm_6k = Rpm_ball("rpm_6k", 5, 1)
    rpm_7k = Rpm_ball("rpm_7k", 6, 1)
    rpm_8k = Rpm_ball("rpm_8k", 7, 1)
    rpm_9k = Rpm_ball("rpm_9k", 8, 1)
    rpm_10k = Rpm_ball("rpm_10k", 9, 1)
    rpm_11k = Rpm_ball("rpm_11k", 10, 2)
    rpm_12k = Rpm_ball("rpm_12k", 11, 2)
    rpm_13k = Rpm_ball("rpm_13k", 12, 2)
    rpm_14k = Rpm_ball("rpm_14k", 13, 3)
    rpm_15k = Rpm_ball("rpm_15k", 14, 3)
    rpm_16k = Rpm_ball("rpm_16k", 15, 3)
    return


def rpm_off():
    # Draw balls
    rpm_1k.draw_off()
    rpm_2k.draw_off()
    rpm_3k.draw_off()
    rpm_4k.draw_off()
    rpm_5k.draw_off()
    rpm_6k.draw_off()
    rpm_7k.draw_off()
    rpm_8k.draw_off()
    rpm_9k.draw_off()
    rpm_10k.draw_off()
    rpm_11k.draw_off()
    rpm_12k.draw_off()
    rpm_13k.draw_off()
    rpm_14k.draw_off()
    rpm_15k.draw_off()
    rpm_16k.draw_off()
    return


def rpm_blank():
    # Draw balls
    rpm_1k.draw_blank()
    rpm_2k.draw_blank()
    rpm_3k.draw_blank()
    rpm_4k.draw_blank()
    rpm_5k.draw_blank()
    rpm_6k.draw_blank()
    rpm_7k.draw_blank()
    rpm_8k.draw_blank()
    rpm_9k.draw_blank()
    rpm_10k.draw_blank()
    rpm_11k.draw_blank()
    rpm_12k.draw_blank()
    rpm_13k.draw_blank()
    rpm_14k.draw_blank()
    rpm_15k.draw_blank()
    rpm_16k.draw_blank()
    return


def rpm_on():
    # Draw balls
    rpm_1k.draw_on()
    rpm_2k.draw_on()
    rpm_3k.draw_on()
    rpm_4k.draw_on()
    rpm_5k.draw_on()
    rpm_6k.draw_on()
    rpm_7k.draw_on()
    rpm_8k.draw_on()
    rpm_9k.draw_on()
    rpm_10k.draw_on()
    rpm_11k.draw_on()
    rpm_12k.draw_on()
    rpm_13k.draw_on()
    rpm_14k.draw_on()
    rpm_15k.draw_on()
    rpm_16k.draw_on()
    return


def rpm_max():
    # Draw balls
    rpm_1k.draw_max()
    rpm_2k.draw_max()
    rpm_3k.draw_max()
    rpm_4k.draw_max()
    rpm_5k.draw_max()
    rpm_6k.draw_max()
    rpm_7k.draw_max()
    rpm_8k.draw_max()
    rpm_9k.draw_max()
    rpm_10k.draw_max()
    rpm_11k.draw_max()
    rpm_12k.draw_max()
    rpm_13k.draw_max()
    rpm_14k.draw_max()
    rpm_15k.draw_max()
    rpm_16k.draw_max()
    return


def calc_rpm(rpm_value):
    rpm_off()
    if rpm_value > rpm1:
        rpm_1k.draw_on()
    if rpm_value > rpm2:
        rpm_2k.draw_on()
    if rpm_value > rpm3:
        rpm_3k.draw_on()
    if rpm_value > rpm4:
        rpm_4k.draw_on()
    if rpm_value > rpm5:
        rpm_5k.draw_on()
    if rpm_value > rpm6:
        rpm_6k.draw_on()
    if rpm_value > rpm7:
        rpm_7k.draw_on()
    if rpm_value > rpm8:
        rpm_8k.draw_on()
    if rpm_value > rpm9:
        rpm_9k.draw_on()
    if rpm_value > rpm10:
        rpm_10k.draw_on()
    if rpm_value > rpm11:
        rpm_11k.draw_on()
    if rpm_value > rpm12:
        rpm_12k.draw_on()
    if rpm_value > rpm13:
        rpm_13k.draw_on()
    if rpm_value > rpm14:
        rpm_14k.draw_on()
    if rpm_value > rpm15:
        rpm_15k.draw_on()
    if rpm_value > rpm16:
        rpm_16k.draw_on()
    if rpm_value > rpm_max:
        rpm_max()
    return


def initiate_gear_display():
    global my_gear
    my_gear = Gear_indicator("mygear", 1, GREEN, BLACK)
    return


def initiate_rpm_text_display():
    global my_rpm
    my_rpm = Big_text("myrpm", 0.0, GREEN, 90, 90, 0, 65, 180, 70, BLACK)
    return


def initiate_mph_text_display():
    global my_mph
    my_mph = Big_text("mymph", 0.0, GREEN, (display_width-65), 90, 670, 65, 120, 70, BLACK)
    return


def initiate_braketemp_text_display():
    global my_braketemp
    my_braketemp = Mid_text("mybreaktemp", 0.0, GREEN, 45, 320, 5, 305, 80, 39, BLACK)
    return


def initiate_psi_text_display():
    global my_psi
    my_psi = Mid_text("mypsi", 0.0, GREEN, 145, 320, 120, 305, 55, 39, BLACK)
    return


def initiate_logo_text_display():
    global my_logo_text
    my_logo_text = logo_text("mylogo", f1_teams[13], DARK_GREEN)
    return


def initiate_throttle_line():
    global my_throttle_line
    my_throttle_line = pedal_line("throttle", 0, 20, GREEN, throt_pedal_line_x, throt_pedal_line_y)
    return


def initiate_brake_line():
    global my_brake_line
    my_brake_line = pedal_line("brake", 0, 20, GREEN, brake_pedal_line_x,brake_pedal_line_y)
    return


def initiate_lap_text_display():
    global my_lap
    my_lap = Mid_text("mylap", 0.0, GREEN, 673, 243, 635, 228, 67, 39, BLACK)
    return


def initiate_lastlap_text_display():
    global my_last_lap
    my_last_lap = Mid_text_float("mylastlap", 0.0, GREEN, 80, 435, 10, 418, 170, 50, BLACK)
    return


def initiate_purplelap_text_display():
    global my_purple_lap_text
    my_purple_lap_text = Mid_text_float("mypurple", 0.0, PURPLE, 80, 375, 10, 358, 170, 50, BLACK)
    return


def initiate_position_text_display():
    global my_position
    my_position = Mid_text("myposition", 0.0, GREEN, 760, 243, 730, 228, 67, 39, BLACK)
    return


def initiate_purple_laps():
    global my_purple_laps
    my_purple_laps = purple_lap("purplelaps", 0.0, 0.0)


def initiate_fuel_mass():
    global my_fuel_mass
    my_fuel_mass = Mid_text("myfuelmass", 0.0, GREEN, 670, 320, 640, 305, 80, 39, BLACK)


def initiate_fuel_perc():
    global my_fuel_perc
    my_fuel_perc = Mid_text("myfuelperc", 0.0, GREEN, 760, 320, 730, 305, 70, 39, BLACK)
    return


def initiate_time_sector1():
    global my_time_sector1
    global my_s1_val1
    global my_s1_val2
    global my_s1_val3
    global my_s1_purple
    global my_s2_val1
    global my_s2_val2
    global my_s2_val3
    global my_s2_purple

    s1_loc_y = 375
    s2_loc_y = 435
    spacer = 125
    starter = 330
    #my_time_sector1 = Mid_text_float("my_time_sector1", 0.0, GREEN, 80, 400, 10, 385, 130, 45, BLACK)

    #Sector 1 values
    my_s1_val1 = Mid_text_float("my_s1_val1", 0.0, GREEN, (starter+spacer), s1_loc_y, (starter+spacer-68), 358, 130, 45, BLACK)
    my_s1_val2 = Mid_text_float("my_s1_val2", 0.0, GREEN, (starter+(spacer*2)), s1_loc_y, (starter+(spacer*2)-68), 358, 130, 45, BLACK)
    my_s1_val3 = Mid_text_float("my_s1_val3", 0.0, GREEN, (starter+(spacer*3)), s1_loc_y, (starter+(spacer*3)-68), 358, 130, 45, BLACK)
    my_s1_purple = Mid_text_float("my_s1_purple", 0.0, PURPLE, 245, s1_loc_y, 194, 358, 110, 45, BLACK)

    #Sector 2 values
    my_s2_val1 = Mid_text_float("my_s2_val1", 0.0, GREEN, (starter+spacer), s2_loc_y, (starter+spacer-68), s2_loc_y-15, 130, 43, BLACK)
    my_s2_val2 = Mid_text_float("my_s2_val2", 0.0, GREEN, (starter+(spacer*2)), s2_loc_y, (starter+(spacer*2)-68), s2_loc_y-15, 130, 43, BLACK)
    my_s2_val3 = Mid_text_float("my_s2_val3", 0.0, GREEN, (starter+(spacer*3)), s2_loc_y, (starter+(spacer*3)-68), s2_loc_y-15, 130, 43, BLACK)
    my_s2_purple = Mid_text_float("my_s2_purple", 0.0, PURPLE, 253, s2_loc_y, 194, s2_loc_y-15, 110, 42, BLACK)

    return


def initiate_sectors_times():
    global my_sector1_times
    global my_sector2_times
    my_sector1_times = sector_timer([], 0.0, 1)
    my_sector2_times = sector_timer([], 0.0, 2)
    return


def drs_bar(drs_value):
    if int(drs_value) ==0:
        pygame.draw.line(windowSurface, DRS_OFF, (20, 247), (170, 247), 35)
    else:
        pygame.draw.line(windowSurface, GREEN, (20, 247), (170, 247), 35)
    return


def pix_Array():
    # get a pixel array of the surface
    global pixArray
    pixArray = pygame.PixelArray(windowSurface)
    #pixArray[480][380] = BLACK
    pixArray[480][800] = BLACK
    del pixArray
    return


def rpm_process(current_rpm):
    if current_rpm > 12000.000:
        rpm_max()
    else:
        calc_rpm(int(current_rpm))
    return


def game_loop():
    rpm_value = 0
    # run the game loop
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:                       # INTERACTIVE KEYBOARD DEBUG
                if event.key == K_UP:
                    rpm_on()
                if event.key == K_DOWN:
                    rpm_off()
                if event.key == K_LEFT:
                    rpm_max()
                if event.key == K_1:
                    my_gear.update_gear(1)
                if event.key == K_2:
                    my_gear.update_gear(2)
                if event.key == K_3:
                    my_gear.update_gear(3)
                if event.key == K_4:
                    my_gear.update_gear(4)
                if event.key == K_5:
                    my_gear.update_gear(5)
                if event.key == K_6:
                    my_gear.update_gear(6)
                if event.key == K_7:
                    my_gear.update_gear(7)
                if event.key == K_8:
                    my_gear.update_gear(8)
                if event.key == K_9:
                    my_gear.update_gear(9)
                if event.key == K_0:
                    my_gear.update_gear(0)
                if event.key == K_r:
                    rpm_value = 0
                    rpm_off()
                if event.key == K_q:
                    rpm_value += 1000
                    if rpm_value > 12000:
                        rpm_max()
                    else:
                        calc_rpm(rpm_value)
                if event.key == K_a:
                    rpm_value -= 1000
                    if rpm_value > 12000:
                        rpm_on()
                    else:
                        calc_rpm(rpm_value)
            elif event.type == USEREVENT:
                    rpm_off()

        data_gear, data_mph_fix, data_brake, data_rpm, data_psi, data_sector, data_sector1, data_sector2, data_lastlap,\
        data_fuel_in_tank, data_fuel_capacity, data_team_id, data_laptime, data_throttle_ped, \
        data_brake_ped, data_drs, data_lap, data_position = receiver()

        my_gear.update_gear(int(data_gear))
        my_rpm.update(data_rpm)
        my_mph.update(data_mph_fix)
        my_braketemp.update(data_brake)
        my_psi.update(data_psi)
        my_fuel_mass.update(data_fuel_in_tank)
        my_fuel_perc.update((data_fuel_in_tank/data_fuel_capacity)*100)

        #my_time_sector1.update(data_sector1)
        my_sector1_times.update(data_sector1)
        my_sector2_times.update(data_sector2)

        my_lap.update(data_lap+1)
        my_position.update(data_position)
        my_last_lap.update(data_lastlap)

        my_purple_laps.update(data_lastlap)

        rpm_process(data_rpm)
        my_logo_text.update_logo(f1_teams[int(data_team_id)])
        my_throttle_line.draw(data_throttle_ped)
        my_brake_line.draw(data_brake_ped)
        drs_bar(data_drs)


        pygame.display.update()
    return


def main():

    inital_setup()
    pix_Array()
    initial_rpm()
    rpm_blank()

    initiate_gear_display()                                         # Initiate the gear display object
    initiate_rpm_text_display()                                     # Initiate the rpm display object
    initiate_mph_text_display()                                     # Initiate the mph text display object
    initiate_braketemp_text_display()                               # Initiate the brake temp display object
    initiate_psi_text_display()                                     # Initiate the PSI text display object
    initiate_logo_text_display()                                    # Initiate the team logo display object, default RB
    initiate_throttle_line()
    initiate_brake_line()
    initiate_fuel_mass()
    initiate_fuel_perc()
    initiate_time_sector1()
    initiate_sectors_times()
    initiate_lap_text_display()
    initiate_position_text_display()
    initiate_lastlap_text_display()
    initiate_purplelap_text_display()
    initiate_purple_laps()                                          #  Purple lap calc

    my_gear.draw_gear()
    my_rpm.draw()
    my_mph.draw()
    my_braketemp.draw()
    my_psi.draw()
    my_logo_text.draw_logo()                                        # Draw initial logo object
    my_throttle_line.draw(1)
    my_brake_line.draw(1)
    my_fuel_mass.draw()
    my_fuel_perc.draw()
    my_lap.draw()
    my_position.draw()
    my_last_lap.draw()


    #my_time_sector1.draw()
    my_s1_val1.draw()
    my_s1_val2.draw()
    my_s1_val3.draw()
    my_s2_val1.draw()
    my_s2_val2.draw()
    my_s2_val3.draw()
    pygame.display.update()                                     # REFRESH THE DISPLAY
    game_loop()
    return


if __name__ == '__main__':

    main()

Ctrmint
Posts: 3
Joined: Fri Dec 16, 2016 9:24 pm

Re: for event in pygame.event.get(): results in IndexError

Fri Dec 30, 2016 11:08 pm

Will try both suggestions.
However since posting I tried Ubuntu on my Pi at the code immediately worked.
Could it be something to do with the SDL?

Return to “Python”