如何防止我的”食物”跳来跳去

huangapple go评论61阅读模式
英文:

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(&quot;Name&quot;)
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 &gt; 0:
keysdown += 1
GoingUp = True
if keys[pygame.K_a] and characterXpos &gt; 0:
keysdown += 1
GoingLeft = True
if keys[pygame.K_s] and characterYpos &lt; WindowHeight-CharacterSize:
keysdown += 1
GoingDown = True
if keys[pygame.K_d] and characterXpos &lt; 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 &lt; 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 &lt; 2:
    addfood = True

你将addfood设置为True,但从未将其设置为False。因此,AddFood()每帧都会运行,每帧都会将Food2Pos设置为新的随机位置。

要解决此问题,请添加一个else子句:

if TotalFood &lt; 2:
    addfood = True
else:
    addfood = False

或更简洁地写成:

addfood = TotalFood &lt; 2
英文:

The problem appears to be right here:

if TotalFood &lt; 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 &lt; 2:
addfood = True
else:
addfood = False

Or more concisely:

addfood = TotalFood &lt; 2

huangapple
  • 本文由 发表于 2023年7月18日 05:13:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定