Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to prevent my "food" from bouncing around

Im making a simple "game" where the objective is to collect food, however whilst the food is meant to stay still it bounces around all over the place. can someone help

ive tried mainly to slightly rewrite the code but nothing works, the code is built for 2 foods, i think 1 is staying still (functional) whilst the other is not (not functional)

the code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import pygame
import time
import random


pygame.init()

White = (255, 255, 255)

WindowWidth = 1280
WindowHeight = 800
CharacterSize = 30
characterYpos = 400
characterXpos = 640
keysdown = 0
global TotalFood
TotalFood = 0
Food1Pos = [0,0]
Food2Pos = [0,0]

Food1 = False
Food2 = False
GoingUp = False
GoingLeft = False
GoingDown = False
GoingRight = False
running = True

global addfood
addfood = False

keys = pygame.key.get_pressed()

screen = pygame.display.set_mode((WindowWidth, WindowHeight))
pygame.display.set_caption("Name")
screen.fill(White)

Food1 = False
Food2 = False

def AddFood():

    global Food1, Food2, TotalFood


    if not Food1:
        Food1Pos[0] = random.randint(0,1230)
        Food1Pos[1] = random.randint(0,750)
        Food1 = True

    else:
        Food2Pos[0] = random.randint(0,1230)
        Food2Pos[1] = random.randint(0,750)
        Food2 = True

    TotalFood += 1

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_w] and characterYpos > 0:
        keysdown += 1
        GoingUp = True

    if keys[pygame.K_a] and characterXpos > 0:
        keysdown += 1
        GoingLeft = True

    if keys[pygame.K_s] and characterYpos < WindowHeight-CharacterSize:
        keysdown += 1
        GoingDown = True

    if keys[pygame.K_d] and characterXpos < WindowWidth-CharacterSize:
        keysdown += 1
        GoingRight = True

    if GoingUp:
        characterYpos -= 12/keysdown
        GoingUp = False

    if GoingLeft:
        characterXpos -= 12/keysdown
        GoingLeft = False

    if GoingDown:
        characterYpos += 12/keysdown
        GoingDown = False

    if GoingRight:
        characterXpos += 12/keysdown
        GoingRight = False

    if TotalFood < 2:
        addfood = True

    if addfood:
        AddFood()

    screen.fill(White)
    pygame.draw.rect(screen, (41, 216, 93), (characterXpos, characterYpos, CharacterSize, CharacterSize))

    if Food1:
        pygame.draw.rect(screen,(227,243,0),(Food1Pos[0],Food1Pos[1],50,50))

    if Food2:
        pygame.draw.rect(screen,(227,243,0),(Food2Pos[0],Food2Pos[1],50,50))

    pygame.display.flip()

    keysdown = 0
    time.sleep(1/90)

    print(TotalFood)

pygame.quit()

>Solution :

The problem appears to be right here:

if TotalFood < 2:
    addfood = True

You are setting addfood to True, but never setting it back to False. Therefore, AddFood() is run every frame, which sets Food2Pos to a new random location every frame.

To fix this, put in an else clause:

if TotalFood < 2:
    addfood = True
else:
    addfood = False

Or more concisely:

addfood = TotalFood < 2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading