Baz
Baz

Reputation: 159

Rect value not updating x postition?

I'm creating a game with cars coming at you. I want the cars to change x_pos every time they go off the screen, but for some reason the value changes but the cars don't actually move. I don't know how to fix this and was wondering if there's something I have to do to make it change. If you want to test my code just remove the background and everything else will work. Thanks.


import time, pygame, random
import math
pygame.init()
#First Variables
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
GREEN = (100, 255, 100)
ORANGE = (255, 140, 0)
YELLOW = (155, 135, 12)
white = (255, 255, 255)
GOLD = (255, 215, 0)
screenwidth = 500
screenheight = 500
Lines = True
Space = True
color = random.sample(range(250), 3)
i = []
x = 247
y = - 20
y1 = 40
y2 = 100
y3 = 160
y4 = 220
y5 = 280
y6 = 340
y7 = 400
y8 = 460
#Display and caption
win = pygame.display.set_mode((screenwidth, screenheight))
pygame.display.set_caption('Lil cary')
clock = pygame.time.Clock()
#Load in images
bkg = pygame.image.load('BG.jpg')

#Actual player class
class player():
    def __init__(self):
        self.color = (12, 124, 134)
        self.vel = 5
        self.grassdamage = 0
        self.image = pygame.Surface([50, 100], pygame.SRCALPHA, 32)
        pygame.draw.rect(self.image, (self.color), (0, 0, 50, 100))
        self.rect = self.image.get_rect(center = (200, 390))
        self.damage1 = False
        self.damage2 = False
        self.damage3 = False
        self.damage4 = False
        self.damage5 = False
        self.damage6 = False
        self.damage7 = False
        self.damage8 = False
        self.dead = False
        self.score = 0
    def draw (self, win):
        #pygame.draw.rect(win, (self.color), (self.x, self.y, self.width, self.height))
        win.blit(self.image, self.rect.topleft)
    def moveback(self):
        if self.rect.y < 390 and self.rect.y >= 0:
            self.rect.y += 10
    def grass(self):
        if self.rect.x > -10 and self.rect.x < 150:
            self.grassdamage += 1
            self.vel = 3
        elif self.rect.x > 300 and self.rect.x < 500:
            self.vel =3
            self.grassdamage += 1
        else:
            self.vel = 5
    def grasshealth(self):
        if self.grassdamage == 100:
            self.damage1 = True
        elif self.grassdamage == 200:
            self.damage2 = True
        elif self.grassdamage == 300:
            self.damage3 = True
        elif self.grassdamage == 400:
            self.damage4 = True
        elif self.grassdamage == 500:
            self.damage5 = True
        elif self.grassdamage == 600:
            self.damage6 = True
        elif self.grassdamage == 600:
            self.damage6 = True
        elif self.grassdamage == 700:
            self.damage7 = True
        elif self.grassdamage == 800:
            self.damage8 = True
            self.dead = True
    def death(self):
        if self.dead == True:
            raise SystemExit ('YOU MOFO DIED')
    def scorecount (self):
        if self.score >= 15:
            raise SystemExit ('YOU WIN OMG KIDS')
    def scoredraw(self):
        Text = pygame.font.Font('freesansbold.ttf', 25)
        TextSurf, TextRect1 = text_objects("Score: " + str(self.score), Text)
        TextRect1.center = ((50), (450))
        win.blit(TextSurf, TextRect1)

#Text Setup
def text_objects(text, font):
    textSurface = font.render(text, True, BLACK)
    return textSurface, textSurface.get_rect()
#Drawing damage bar
def drawbar ():
    if player.damage1 == True and car.damage1 == False or car.damage1 == True:
        pygame.draw.rect(win, (GOLD), (50, 50, 25, 25))
    if player.damage2 == True and car.damage1 == False or car.damage1 == True:
        pygame.draw.rect(win, (GOLD), (50, 75, 25, 25))
    if car.damage1 == True and player.grassdamage <= 200:
        player.grassdamage = 201
    if player.damage3 == True and car.damage2 == False or car.damage2 == True:
        pygame.draw.rect(win, (GOLD), (50, 100, 25, 25))
    if player.damage4 == True and car.damage2 == False or car.damage2 == True:
        pygame.draw.rect(win, (GOLD), (50, 125, 25, 25))
    if car.damage2 == True and player.grassdamage <= 400:
        player.grassdamage = 401
    if player.damage5 == True and car.damage3 == False or car.damage3 == True:
        pygame.draw.rect(win, (GOLD), (50, 150, 25, 25))
    if player.damage6 == True and car.damage3 == False or car.damage3 == True:
        pygame.draw.rect(win, (GOLD), (50, 175, 25, 25))
    if car.damage3 == True and player.grassdamage <= 600:
        player.grassdamage = 601
    if player.damage7 == True and car.damage4 == False or car.damage4 == True:
        pygame.draw.rect(win, (GOLD), (50, 200, 25, 25))
    if player.damage8 == True and car.damage4 == False or car.damage4 == True:
        pygame.draw.rect(win, (GOLD), (50, 225, 25, 25))
        raise SystemExit ('YOU LOST')
    if car.damage4 == True and player.grassdamage <= 800:
        player.grassdamage = 800
    pygame.draw.rect(win, (0,0,0), (50, 50, 25, 200), 3)
    Text = pygame.font.Font('freesansbold.ttf', 20)
    TextSurf, TextRect = text_objects("Damage", Text)
    TextRect.center = ((65), (30))
    win.blit(TextSurf, TextRect)


class car1():
    ROAD_LEFT = 175
    ROAD_RIGHT = 300
    def __init__(self):
        self.x_pos = random.randrange(car1.ROAD_LEFT, car1.ROAD_RIGHT)
        self.color = random.sample(range(250), 3)
        self.image = pygame.Surface([50, 100], pygame.SRCALPHA, 32)
        pygame.draw.rect(self.image, (self.color), (0, 0, 50, 100))
        self.rect = self.image.get_rect(topleft = (self.x_pos, -100))
        self.carvel = random.randrange(8, 10)
        self.damage = 0
        self.damage1 = False
        self.damage2 = False
        self.damage3 = False
        self.damage4 = False
    def draw(self, win):
        #pygame.draw.rect(win,(self.color),self.rect)
        win.blit(self.image, self.rect.topleft)
    def move(self):
        if self.rect.y < 530:
            self.rect.y += self.carvel
        else:
            self.rect.y = -100
            player.score += 1
            self.color = random.sample(range(250), 3)
            self.carvel = random.randrange(6, 8)
            self.x_pos = random.randrange(car1.ROAD_LEFT, car1.ROAD_RIGHT)
    def collision_check(self, another_object):
        if self.rect.colliderect(another_object):
            print('collison')
            self.rect.y = -100
            self.damage += 1
    def cardamage(self):
        if self.damage == 1:
            self.damage1 = True
            player.damage1 = True
            player.damage2 = True
        if self.damage == 2:
            self.damage2 = True
            player.damage3 = True
            player.damage4 = True
        if self.damage == 3:
            self.damage3 = True
            player.damage5 = True
            player.damage6 = True
        if self.damage == 4:
            self.damage4 = True
            player.damage7 = True
            player.damage8 = True


        #Putting variables to the classes
player = player()
car = car1()
car_list = [ car ]  # start with one car
car_add_time = 0
#Main drawing function
def redrawgamewindow():
    win.blit(bkg, (0, 0))
    pygame.draw.rect(win, (0, 0, 0), (150, 0, 200, 500))
    pygame.draw.rect(win, (255, 255, 255), (x, (y), 10, 30))
    pygame.draw.rect(win, (255, 255, 255), (x, (y1), 10, 30))
    pygame.draw.rect(win, (255, 255, 255), (x, (y2), 10, 30))
    pygame.draw.rect(win, (255, 255, 255), (x, (y3), 10, 30))
    pygame.draw.rect(win, (255, 255, 255), (x, (y4), 10, 30))
    pygame.draw.rect(win, (255, 255, 255), (x, (y5), 10, 30))
    pygame.draw.rect(win, (255, 255, 255), (x, (y6), 10, 30))
    pygame.draw.rect(win, (255, 255, 255), (x, (y7), 10, 30))
    pygame.draw.rect(win, (255, 255, 255), (x, (y8), 10, 30))
    player.draw(win)
    for car in car_list:
        car.draw(win)
    player.grasshealth()
    for car in car_list:
        car.cardamage()
    player.grass()
    player.death()
    for car in car_list:
        car.collision_check(player.rect)
        car.move()
    drawbar()
    player.scorecount()
    player.scoredraw()
    pygame.display.update()
#MAINLOOP

run = True
while run:
    #Making background and FPS
    clock.tick(80)
    #Quiting Funciton
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False
    time_now = pygame.time.get_ticks()

    if (time_now - car_add_time > 5000):
        new_car = car1()
        car_list.append(new_car)
        car_add_time = time_now
    #Scrolling the lines
    if Lines:
        y += player.vel
        y1 += player.vel
        y2 += player.vel
        y3 += player.vel
        y4 += player.vel
        y5 += player.vel
        y6 += player.vel
        y7 += player.vel
        y8 += player.vel
    if Lines:
        if y >= 500:
            y = -40
        if y1 >= 500:
            y1 = -40
        if y2 >= 500:
            y2 = -40
        if y3 >= 500:
            y3 = -40
        if y4 >= 500:
            y4 = -40
        if y5 >= 500:
            y5 = -40
        if y6 >= 500:
            y6 = -40
        if y7 >= 500:
            y7 = -40
        if y8 >= 500:
            y8 = -40
    #User input
    keys = pygame.key.get_pressed()
    #Boost controller
    if keys[pygame.K_SPACE]:
        start_time = time.time()
        if player.rect.y > 200:
            player.rect.y -= 9
            Space = True
    else:
        player.moveback()
        end_time = time.time()
    #Left movement
    if  keys[pygame.K_LEFT] and player.rect.x > 150:
        player.rect.x -= 5
    if keys [pygame.K_LEFT] and player.rect.x <= 150:
        if player.rect.x > 0:
            player.rect.x -=5
    #Right movement
    if keys[pygame.K_RIGHT] and player.rect.x < 300:
        player.rect.x += 5
    if keys[pygame.K_RIGHT]and player.rect.x >= 300:
        if player.rect.x < 500 - player.rect.width:
            player.rect.x += 5

    #Grass and grass damage

    #MAIN DRAW RECALL
    redrawgamewindow()


Upvotes: 2

Views: 51

Answers (1)

Rabbid76
Rabbid76

Reputation: 210890

It is not sufficient to set a random value to self.x_pos. For drawing the "car" the position of the pygame.Rect object self.rect is used:

def draw(self, win):
   win.blit(self.image, self.rect.topleft)

So you've to change self.rect.x, too:

class car1():
    # [...]

    def move(self):
        if self.rect.y < 530:
            self.rect.y += self.carvel
        else:
            self.rect.y = -100
            player.score += 1
            self.color = random.sample(range(250), 3)
            self.carvel = random.randrange(6, 8)
            self.x_pos = random.randrange(car1.ROAD_LEFT, car1.ROAD_RIGHT)
            self.rect.x = self.x_pos # <---------------------------------------

Upvotes: 1

Related Questions