0

so when I run my code and start the game all four of the coffee beans fall because of the for i in range(num_of_coffee) on line 53. There is another command on line 328 which is supposed to change the position of all the coffees on the list but only affects one. I've tried using functions but that didn't seem to work. I'm not sure why it only affects one.

import pygame
import math
from pygame.locals import *
from pygame import mixer
from typing import Tuple
import pickle
from os import path
import time
import sys
import os
import random
pygame.init()

#colours

light_blue = 173, 216, 230
white = 255,255,255
black = 0,0,0
green = 0, 255, 0
blue = 0,0,255
brown = 103,71,54
red = 255,0,0
game_over = 0

#fonts

font = pygame.font.SysFont('Comic Sans MS', 50, 70)
beforefont = pygame.font.SysFont('Comic Sans MS', 200, 200)
smallfont = pygame.font.SysFont('Comic Sans MS', 30, 200)
bigfont = pygame.font.SysFont('Comic Sans MS', 100, 70)

screen_width = 650
screen_height = 800
screen = pygame.display.set_mode([screen_width,screen_height])
pygame.display.set_caption("Darsh's Game")
pygame.display.update()
pygame.draw.rect(screen,white,(200,150,100,50))
speed = 1

#PLAYERS CO-ORDS
x = 25
y = 595

#coffee

coffeeimage = []
coffeex = []
coffeey = []
coffeey_change = []

num_of_coffee = 4

for i in range(num_of_coffee):
    coffeeimage.append(pygame.image.load("newgameimg/coffee_bean1.png"))
    coffeex.append(random.randrange(10, 640))
    coffeey.append(random.randrange(-100, -45))
    coffeey_change.append(1)





isJump = False
jumpCount = 10
left = False
right = True
score = 1000
timer_menu = 1
start_timer = 0
end_time = 0
skin = 1
jumpcoffeespeed = 1

secret = False
menu = True
run = False
game_over = 2
speeds = 0
speedcost = 10

mutee = 0
clock = pygame.time.Clock()



keys = pygame.key.get_pressed()

def draw_text(text, font, text_col, x, y):
    img = font.render(text, True, text_col)
    screen.blit(img, (x, y))

def draw_rect():
    rect1 = pygame.Rect(0, 0, 25, 800)
    pygame.draw.rect(screen, black, rect1)
    rect2 = pygame.Rect(625, 0, 25, 800)
    pygame.draw.rect(screen, black, rect2)
    rect3 = pygame.Rect(0, 775, 650, 25)
    pygame.draw.rect(screen, green, rect3)
    pygame.display.flip()

def direction(left, right):
    if left == True:
        if skin == 1:
            screen.blit(mainleft1, (x, y))
        if skin == 2:
            screen.blit(mainleft2, (x, y))
        if skin == 3:
            screen.blit(mainleft3, (x, y))
        if skin == 4:
            screen.blit(mainleft4, (x, y))
        if skin == 5:
            screen.blit(mainleft5, (x, y))
        if skin == 6:
            screen.blit(mainleft6, (x, y))
        if skin == 7:
            screen.blit(mainleft7, (x, y))
        if skin == 8:
            screen.blit(mainleft8, (x, y))
        if skin == 9:
            screen.blit(mainleft9, (x, y))
        if skin == 10:
            screen.blit(mainleft10, (x, y))
        if skin == 11:
            screen.blit(mainleft11, (x, y))
        if skin == 12:
            screen.blit(mainleft12, (x, y))
        if skin == 13:
            screen.blit(mainleft13, (x, y))
    elif right == True:
        if skin == 1:
            screen.blit(mainright1, (x, y))
        if skin == 2:
            screen.blit(mainright2, (x, y))
        if skin == 3:
            screen.blit(mainright3, (x, y))
        if skin == 4:
            screen.blit(mainright4, (x, y))
        if skin == 5:
            screen.blit(mainright5, (x, y))
        if skin == 6:
            screen.blit(mainright6, (x, y))
        if skin == 7:
            screen.blit(mainright7, (x, y))
        if skin == 8:
            screen.blit(mainright8, (x, y))
        if skin == 9:
            screen.blit(mainright9, (x, y))
        if skin == 10:
            screen.blit(mainright10, (x, y))
        if skin == 11:
            screen.blit(mainright11, (x, y))
        if skin == 12:
            screen.blit(mainright12, (x, y))
        if skin == 13:
            screen.blit(mainright13, (x, y))



def start_buttons():
    pygame.draw.rect(screen, light_blue, start_button)
    pygame.draw.rect(screen, brown, quit_button)
    pygame.draw.rect(screen, brown, mute_button)
    draw_text("Mute", font, white, 25, 715)
    draw_text("Start", font, white, 245, 260)
    draw_text("Quit", font, white, 490, 713)
    draw_text("Coffee Catcher", font, brown, 100, 100)

def power_ups():
    screen.blit(speed_power_up, (150,725))
    screen.blit(up_power_up, (315, 725))
    screen.blit(temp, (515, 750))

def coffee(x, y, i):
    screen.blit(coffeeimage[i], (x, y))

    # all the color characters
mainright1 = pygame.image.load('newgameimg/main1.png')
mainleft1 = pygame.transform.flip(mainright1, True, False)
mainright2 = pygame.image.load('newgameimg/main2.png')
mainleft2 = pygame.transform.flip(mainright2, True, False)
mainright3 = pygame.image.load('newgameimg/main3.png')
mainleft3 = pygame.transform.flip(mainright3, True, False)
mainright4 = pygame.image.load('newgameimg/main4.png')
mainleft4 = pygame.transform.flip(mainright4, True, False)
mainright5 = pygame.image.load('newgameimg/main5.png')
mainleft5 = pygame.transform.flip(mainright5, True, False)
mainright6 = pygame.image.load('newgameimg/main6.png')
mainleft6 = pygame.transform.flip(mainright6, True, False)
mainright7 = pygame.image.load('newgameimg/main7.png')
mainleft7 = pygame.transform.flip(mainright7, True, False)
mainright8 = pygame.image.load('newgameimg/main8.png')
mainleft8 = pygame.transform.flip(mainright8, True, False)
mainright9 = pygame.image.load('newgameimg/main9.png')
mainleft9 = pygame.transform.flip(mainright9, True, False)
mainright10 = pygame.image.load('newgameimg/main10.png')
mainleft10 = pygame.transform.flip(mainright10, True, False)
mainright11 = pygame.image.load('newgameimg/main11.png')
mainleft11 = pygame.transform.flip(mainright11, True, False)
mainright12 = pygame.image.load('newgameimg/main12.png')
mainleft12 = pygame.transform.flip(mainright12, True, False)
mainright13 = pygame.image.load('newgameimg/main13.png')
mainleft13 = pygame.transform.flip(mainright13, True, False)
arrow_right = pygame.image.load('newgameimg/arrow.png')
arrow_left = pygame.transform.flip(arrow_right, True, False)
pygame.display.set_icon(mainright1)


bg = pygame.image.load("newgameimg/bg.png")
bgmenu = pygame.image.load("newgameimg/bgmenu.jpeg")
coffee_bean1 = pygame.image.load("newgameimg/coffee_bean1.png")
coffee_bean2 = pygame.image.load("newgameimg/coffee_bean2.png")
coffee_bean3 = pygame.image.load("newgameimg/coffee_bean3.png")
coffee_bean4 = pygame.image.load("newgameimg/coffee_bean4.png")
speed_power_up = pygame.image.load("newgameimg/speed.png")
up_power_up = pygame.image.load("newgameimg/up.png")
temp = pygame.image.load("newgameimg/coffeetemp.png")
sus = pygame.image.load("newgameimg/sus.png")

start_button = pygame.draw.rect(screen,(black),(225,250,200,100))
quit_button = pygame.draw.rect(screen,(black),(480,720,153,65))
mute_button = pygame.draw.rect(screen,(black),(20,720,153,65))
restart_button = pygame.draw.rect(screen,(black),(200,350,250,100))
end_quit_button = pygame.draw.rect(screen,(black),(200,500,250,100))

music = pygame.mixer.music.load('newgameimg/music.wav')

pygame.mixer.music.play(-1)



while menu:
    if game_over == 2:
        start_time = pygame.time.get_ticks()
        screen.blit(bgmenu, (0, 0))
        keys = pygame.key.get_pressed()
        start_buttons()
        mx, my = pygame.mouse.get_pos()
        screen.blit(arrow_left, (200, 440))
        screen.blit(arrow_right, (330, 440))
        if skin > 13:
            skin = 1
        if skin == 1 or skin == 0:
            screen.blit(mainright1, (270, 400))
        if skin == 2:
            screen.blit(mainright2, (270, 400))
        if skin == 3:
            screen.blit(mainright3, (270, 400))
        if skin == 4:
            screen.blit(mainright4, (270, 400))
        if skin == 5:
            screen.blit(mainright5, (270, 400))
        if skin == 6:
            screen.blit(mainright6, (270, 400))
        if skin == 7:
            screen.blit(mainright7, (270, 400))
        if skin == 8:
            screen.blit(mainright8, (270, 400))
        if skin == 9:
            screen.blit(mainright9, (270, 400))
        if skin == 10:
            screen.blit(mainright10, (270, 400))
        if skin == 11:
            screen.blit(mainright11, (270, 400))
        if skin == 12:
            screen.blit(mainright12, (270, 400))
        if skin == 13 or skin == 14:
            screen.blit(mainright13, (270, 400))
        start_time = pygame.time.get_ticks()
        pygame.display.update()
        for event in pygame.event.get():
            if mx >= 600 and mx <= 650 and my >= 0 and my <= 20 and event.type == pygame.MOUSEBUTTONUP: #gets x and y of mouse to check if it is on the button
                menu = False
                secret = True
                start_time = pygame.time.get_ticks()
            if keys[pygame.K_ESCAPE]:
                pygame.quit()
            if mx >= 225 and mx <= 445 and my >= 250 and my <= 350 and event.type == pygame.MOUSEBUTTONUP: #gets x and y of mouse to check if it is on the button
                score = 1000
                speedcost = 10
                speeds = 0
                speed = 1
                x = 25
                y = 595
                game_over = 0
            if mx >= 480 and mx <= 633 and my >= 720 and my <= 785 and event.type == pygame.MOUSEBUTTONUP:
                pygame.quit()
            if mx >= 20 and mx <= 173 and my >= 720 and my <= 785 and event.type == pygame.MOUSEBUTTONUP or keys[pygame.K_m]:
                if mutee == 1:
                    mutee = 0
                    pygame.mixer.music.pause()
                elif mutee == 0:
                    mutee = 1
                    pygame.mixer.music.unpause()
            if mx >= 245 and mx <= 263 and my >= 442 and my <= 477 and event.type == pygame.MOUSEBUTTONUP:
                skin = skin - 1
            if skin < 1:
                skin = 13
            if mx >= 367 and mx <= 394 and my >= 442 and my <= 477 and event.type == pygame.MOUSEBUTTONUP:
                skin = skin + 1





    if game_over == 0:
        mx, my = pygame.mouse.get_pos()
        keys = pygame.key.get_pressed()
        pygame.display.update()
        if keys[pygame.K_a]:
            x -= speed
            left = True
            right = False
        if keys[pygame.K_d]: # and x < screen_width - speed - 89:
            x += speed
            left = False
            right = True
        if keys[pygame.K_ESCAPE]:
            menu = True
            run = False
        if keys[pygame.K_p]:
            game_over = 1
        if x >= 700:
            x = -100
        if x <= -101:
            x = 669
        if y > 595:
            y = 595
        if coffeey[i] >= 650:
            coffeey[i] = random.randrange(-100, -45)
            coffeex[i] = random.randrange(40, 610)
            coffeeskin = random.randrange(1, 4)
            coffeeimage[i] = pygame.image.load(f"newgameimg/coffee_bean{coffeeskin}.png")
        screen.blit(bg, (0, 0))
        draw_text("Time:", smallfont, white, 10, 720)
        if not (isJump):
            if keys[pygame.K_SPACE]:  # jumping code
                isJump = True
                normal_speed = speed
        else:
            if jumpCount >= -10:
                jumpcoffeespeed = 9
                time.sleep(0.015)
                speed = normal_speed * 7
                y -= (jumpCount * abs(jumpCount)) * 0.25
                jumpCount -= 1
            else:
                jumpCount = 10
                isJump = False  # jumping code
                speed = normal_speed
                jumpcoffeespeed = 1

        timer1 = pygame.time.get_ticks() - start_time - end_time
        timer2 = timer1 / 1000
        timer = round(timer2, 1)

        game_timer = timer2

        draw_text(f'{timer}', smallfont, white, 10, 752)


        direction(left, right)
        draw_text("George is dumb", font, green, 100, 400)
        draw_text(f"{score}", smallfont, white, 560, 740)
        power_ups()
        for i in range(num_of_coffee):
            coffeey[i] += coffeey_change[i] * jumpcoffeespeed
            coffee(coffeex[i], coffeey[i], i)



        for event in pygame.event.get():
            if keys[pygame.K_e] or mx >= 250 and mx <= 313 and my >= 725 and my <= 789 and event.type == pygame.MOUSEBUTTONDOWN:
                if score >= speedcost:
                    speed = speed + 0.5
                    score = score - speedcost
                    speeds = speeds + 1
                    speedcost = speedcost + 10
                else:
                    print("not work")
            if keys[pygame.K_m]:
                if mutee == 1:
                    mutee = 0
                    pygame.mixer.music.pause()
                elif mutee == 0:
                    mutee = 1
                    pygame.mixer.music.unpause()




        draw_text(f"{speeds}", smallfont, white, 230, 720)
        draw_text(f"${speedcost}", smallfont, white, 230, 755)
        pygame.display.update()

    if game_over == 1:
        end_time = pygame.time.get_ticks()
        keys = pygame.key.get_pressed()
        mx, my = pygame.mouse.get_pos()
        screen.blit(bgmenu, (0, 0))
        draw_text("Game Over!", bigfont, light_blue, 10, 10)
        draw_text(f"Your Score: {score}", font, white, 100, 150)
        draw_text(f"Final Time: {timer}", font, white, 100, 230)
        pygame.draw.rect(screen, light_blue, restart_button)
        draw_text("Restart", font, white, 215, 360)
        pygame.draw.rect(screen, light_blue, end_quit_button)
        draw_text("Menu", font, white, 250, 510)
        for event in pygame.event.get():
            if mx >= 200 and mx <= 450 and my >= 350 and my <= 450 and event.type == pygame.MOUSEBUTTONUP:  # gets x and y of mouse to check if it is on the button
                print("hi")
                end_time = pygame.time.get_ticks() - start_time - game_timer
                score = 1000
                speedcost = 10
                speeds = 0
                speed = 1
                x = 25
                y = 595
                game_over = 0
            if mx >= 200 and mx <= 450 and my >= 500 and my <= 600 and event.type == pygame.MOUSEBUTTONUP or keys[pygame.K_ESCAPE]:  # gets x and y of mouse to check if it is on the button
                end_time = 0
                game_over = 2

    pygame.display.update()

while secret:
    screen.blit(sus, (0, 0))
    pygame.display.update()

Matiiss
  • 5,970
  • 2
  • 12
  • 29
dashgandhi
  • 11
  • 2
  • 2
    You have to provide MRE (Minimal Reproducible Example), basically a shortened form of Your issue that can be still used for testing. A suggestion: most of those if statements can be changed to for loops that just loop over and check which number it is and then adjust it accordingly, there is no point of that many if statements. – Matiiss Apr 10 '21 at 00:19
  • or at least use elif statements instead of so many ifs that will also improve speed – Matiiss Apr 10 '21 at 00:24
  • You have to loop over each element in the list and change their position use a `for loop` (which You should use for those if statements too) – Matiiss Apr 10 '21 at 00:25
  • There is definitely no need to post *hundreds of lines of code* in your question. Isolate the part of the code causing the bug, by removing the unnecessary parts and simplifying until you're left with the minimum amount of code that demonstrates the bug. You may well figure it out yourself in the process of doing this. – kaya3 Apr 11 '21 at 01:06

0 Answers0