1

Hey i'm a beginner to pygame and I have just started building games but I got stuck at this game and I'm not able to figure out what I am doing wrong.I have made a game in which car gets charged either through light or electricity.When it's in sunlight,the battery level starts increasing while in colder region,it can only be charged if there is an electric charge station.The problem is that the battery level is incrementing in the rest of the code but when i click at the charging icon at the end of the code,it does not.By the way,charging icon is a sort of booster which is provided only if available at the end of the game.I have tried to change that particular code several times and tested that in other modules but it's not working at all.Please tell me why battery level is not incrementing in that case.I have been stuck there for at least five days.Any help would be appreciable.

    import pygame, time, random

    pygame.init()
    screen = pygame.display.set_mode((800, 600))

    dual_car = pygame.image.load("dual-car.png")
    carX = 300
    carY = 528
    carX_change = 0
    carY_change = 0

    charging_icon = pygame.image.load("chargingicon.png").convert()
    iconX = 500
    iconY = 200


    charging_station = pygame.image.load("charging-station.png")
    chargingX = 0
    chargingY = 475

    first_location = pygame.image.load("bright-road.png")
    firstX = 0
    firstY = 0

    second_location = pygame.image.load("snow-road.png")
    secondX = 2000
    secondY = 0

    third_location = pygame.image.load("sunny.png")
    thirdX = 4000
    thirdY = 0

    fourth_location = pygame.image.load("snow-road2.png")
    fourthX = 6000
    fourthY = 0

    battery_level = 1

    game_level = 1

    font = pygame.font.Font("freesansbold.ttf", 32)

    second_font = pygame.font.Font("freesansbold.ttf", 20)

    warning_time = 30

    draw_rectangles =  "a"

    transparent = (0, 0, 0, 0)


    def text():
       text = font.render("Battery Level:" + str(battery_level) +        
       "%",True, (48, 88, 123))
       screen.blit(text, (0, 0))


    def text2():
        text2 = font.render("BATTERY CHARGING", True, (69,117,203))
        screen.blit(text2, (300, 250))


    def text3():
      text3 = font.render("BATTERY DISCHARGING", True, (28, 229,7))
      screen.blit(text3, (300, 250))


    def text4():
        text4 = font.render("BATTERY FULLY CHARGED", 
        True(11,103,77))
        screen.blit(text4, (300, 250))


    def text5():
       text5 = font.render("GAME LEVEL: " + str(game_level), True, 
       (255, 0, 255))
       screen.blit(text5, (0, 130))


    def first():
       screen.blit(first_location, (firstX, firstY))


    def second():
       screen.blit(second_location, (secondX, secondY))


    def third():
       screen.blit(third_location, (thirdX, thirdY))


    def fourth():
        screen.blit(fourth_location, (fourthX, fourthY))


    def car():
        screen.blit(dual_car, (carX, carY))


    def show():
       first()
       second()
       third()
       fourth()
       car()
       text()
       text5()


       run = True
       while run:
       screen.fill((255, 255, 255))
       show()

       firstX -= 2
secondX -= 4
thirdX -= 4
fourthX -= 4


if draw_rectangles == False:
 rect1 = pygame.draw.rect(screen, (49, 97, 132), (100, 200, 500, 150))
 rect2 = pygame.draw.rect(screen, (27, 166, 166), (100, 300, 500, 150))
 text7 = second_font.render("Press below to quit!!", True, (100, 200, 99))
 rect3 = pygame.draw.rect(screen, (255, 255, 255), (100, 450, 100, 100))
 text8 = second_font.render("QUIT", True, (87, 163, 249))
 text9 = second_font.render("SORRY YOU COUDN'T CHARGE THE BATTERY", True, (249, 133, 87))
 rect4 = pygame.draw.rect(screen, (255, 0, 255), (200, 450, 400, 100))
 text10 = second_font.render("USE POWER SUPPLY", True, (0, 0, 0))
 screen.blit(text7, (100, 300))
 screen.blit(text8, (100, 450))
 screen.blit(text9, (100, 200))
 screen.blit(text10, (200, 450))
 pygame.display.flip()
elif draw_rectangles == True:
    screen.blit(charging_icon,(iconX,iconY))
    pygame.display.update()




carX += carX_change
carY += carY_change

if carX >= 0:
    pygame.time.delay(100)
    carX_change = 1

if carX >= 574:
    carX = 300

if secondX - carX > 10 and battery_level < 100 and carX % 2 == 0:
    battery_level += 1
    text2()
    pygame.display.flip()
if secondX - carX <= 10 and battery_level > 0 and carX % 1 == 0:
    battery_level -= 1
    text3()
    pygame.display.flip()

if secondX - carX <= 10 and battery_level == 0:
    minutes = warning_time // 60
    seconds = warning_time - (60 * minutes)
    warning_time -= 1
    if warning_time > -1 and battery_level == 0:
        screen.blit(charging_station, (chargingX, chargingY))
        text6 = font.render("WARNING TIME " + str(minutes) + ":" + str(seconds), True, (56, 70, 72))
        screen.blit(text6, (400, 0))
        pygame.display.flip()
        time.sleep(1)
    elif warning_time > -1 and battery_level > 0:
        game_level += 1
    if warning_time == -1 and battery_level == 0:
        draw_rectangles = False

if carX - chargingX < 200 and battery_level == 0:
    battery_level += 100
    game_level += 1
    carX = 300
if thirdX - carX <= 10 and battery_level < 100 and carX % 1 == 0:
    battery_level += 1
if fourthX - carX <= 10 and battery_level > 0 and carX % 2 == 0:
    battery_level -= 1

if battery_level == 100:
    text4()

    pygame.display.update()

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            carX -= 10
        if event.key == pygame.K_RIGHT:
            carX += 10
    elif event.type == pygame.MOUSEBUTTONDOWN:
        mouse_position = pygame.mouse.get_pos()
        if rect3.collidepoint(mouse_position):
            pygame.quit()
        if rect4.collidepoint(mouse_position):
           draw_rectangles = True
        if charging_icon.get_rect().collidepoint(mouse_position) and warning_time < -1:
            battery_level += 1

        pygame.quit()
Sva Sva
  • 41
  • 4

1 Answers1

0

charging_icon is a pygame.Surface object. A Surface object has no position. It is put to the position which is specified, when it is blit on a target Surface. Hence, the position of the pygame.Rect object which is returned by .get_rect() is always (0, 0).

You have to set the location of the rectangle, e.g. by passing a keyword argument to .get_rect():

charging_rect = charging_icon.get_rect(topleft = (iconX,iconY))

if charging_rect.collidepoint(mouse_position) and warning_time < -1:
    battery_level += 1
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Hey thank you so much.It really helped but i want to ask another question related to the code.How make images disappear in pygame.I know there are lot of answers on the site but everywhere they mention to use screen.fill(color) to make image disappear but if there is already a large image in background,then the black or any colored area will look odd.I want to make it clear completely.If there is a way to do so. – Sva Sva Apr 28 '20 at 12:03