0

Am new in Python(programming) and pygame and I am trying to make a game and whenever i run the code , the act_lt is throwing away the first saved elements and saving the current speed.

Here is the complete code:

import **pygame**
import panda as pd
import random

RED = (255, 0, 0)
YELLOW = (255, 255, 0)

size = 600, 200
width, height = size
screen = pygame.display.set_mode(size)

ball = pygame.Surface([50, 50])
ball.fill(RED)
rect = ball.get_rect()
speed = [2, 2]
rect2 = Rect(575,0,25,200)

n_df = pd.DataFrame()


def m_left(rg):
    speed[0] = -rg
        
def m_right(rg):
    speed[0] = rg
        
def m_top(rg):
    speed[1] = -rg
        
def m_bottom(rg):
    speed[1] = rg


def dr_pc():
    global rect
    global n_df
    all_lt = []
    act_lt = []
    col_lt = []
    tl_lt = [(rect.w, rect.h)]
    lm_lt = []
    con = 0
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
        while True:
            rg = random.choice([2, 3, 4])
            moves = random.choice([f'm_left({rg})', f'm_right({rg})',f'm_top({rg})',f'm_bottom({rg})'])
            eval(moves)
            rect = rect.move(speed)
            
            if rect.left < 5:
                speed[0] = rg
                rect = rect.move(speed)
            elif rect.top < 5:
                speed[1] = rg
                rect = rect.move(speed)
            elif rect.bottom > (height-5):
                speed[1] = -rg
                rect = rect.move(speed)
            elif rect.colliderect(rect2):
                speed[0] = -rg
                if len(col_lt) < 1:
                    col_lt.append([(rect.x, rect.y)])
                rect = rect.move(speed)
            else:
                if len(act_lt) < 11:
                    print('adding speed', speed)
                    act_lt.append(speed)
                if len(lm_lt) < 11:
                    lm_lt.append(rg)
                if (len(act_lt) == 10) and (len(lm_lt) == 10):
                    break
                
            screen.fill(GRAY)
            pygame.draw.rect(screen, RED, rect, 2)
            pygame.draw.rect(screen, YELLOW, rect2)
            screen.blit(ball, rect)
            pygame.display.flip()
        if col_lt == []:
            col_lt.append('no_col')
        con += 1
        for t_r in act_lt, lm_lt, tl_lt, col_lt:
            for e_m in t_r:
                all_lt.append(e_m)
        print('act_lt(final)=', act_lt)
        n_df2 = pd.DataFrame([all_lt], columns=['action1', 'action2', 'action3',
                                              'action4', 'action5', 'action6',
                                              'action7', 'action8', 'action9',
                                              'action10', 'pow1', 'pow2', 'pow3',
                                              'pow4', 'pow5', 'pow6', 'pow7', 'pow8',
                                              'pow9', 'pow10', 'tool', 'coll_pt1'])
        n_df = pd.concat([n_df, n_df2], ignore_index=True)
        all_lt = []
        act_lt = []
        col_lt = []
        lm_lt = []
        if con == 1000:
            print(f'reached {con}')
            break
    pygame.quit()

**RESULT**

adding speed [3, 3]
adding speed [-2, 3]
adding speed [2, 3]
adding speed [2, -4]
adding speed [2, -3]
adding speed [3, -3]
adding speed [2, 4]
adding speed [2, -4]
adding speed [2, 3]
adding speed [-4, 3]
act_lt(final)= [[-4, 3], [-4, 3], [-4, 3], [-4, 3], [-4, 3], [-4, 3], [-4, 3], [-4, 3], [-4, 3], [-4, 3]]

i posted the whole code and a partial result(as the rest is just repetition) because i don't know how to summarize it.

  • I'm sorry, it is completely unclear what you are asking. What does "the list(act_lt) is throwing away the first saved elements and saving the current speed." mean exactly? `list(act_lt)` doesn't show up in your code anywhere. – juanpa.arrivillaga Apr 14 '22 at 17:52
  • There is only 1 speed list. – quamrana Apr 14 '22 at 18:34

0 Answers0