英文:
How to prevent my "food" from bouncing around
问题
我正在制作一个简单的“游戏”,目标是收集食物,然而食物应该保持静止,但实际上它在四处弹跳。有人可以帮助吗?
我尝试过主要是稍微重写代码,但没有任何效果,代码是为两种食物而构建的,我认为其中一种保持静止(功能正常),而另一种则不是(功能不正常)。
以下是代码:
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()
希望这可以帮助你解决问题。
英文:
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
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()
答案1
得分: 1
问题似乎出在这里:
if TotalFood < 2:
addfood = True
你将addfood设置为True,但从未将其设置为False。因此,AddFood()每帧都会运行,每帧都会将Food2Pos设置为新的随机位置。
要解决此问题,请添加一个else
子句:
if TotalFood < 2:
addfood = True
else:
addfood = False
或更简洁地写成:
addfood = TotalFood < 2
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论