英文:
how to bring back the claw to initial positon?
问题
我理解了,你想让机械爪在下降到Y光标位置后返回初始位置(-200)。你尝试过多种方法但未成功,包括使用ChatGPT、YouTube、黑盒方法和谷歌搜索。
让我来看一下你的代码。
def update(self):
if self.claw_placed:
if self.rect.y < self.target_y:
# ...
else:
# ...
else:
if self.rect.y > self.target_y:
# ...
问题可能在于你在 update
方法中的条件判断有误。请将所有 <
和 >
替换为 <
和 >
。
def update(self):
if self.claw_placed:
if self.rect.y < self.target_y:
# ...
else:
# ...
else:
if self.rect.y > self.target_y:
# ...
请尝试这样修改后再测试一下。如果仍然有问题,请告诉我。
英文:
I have to make a game, everything looks good except that I can't make my claw go up after going down to the Y position defined by the cursor. I used ChatGPT to help me but he saying rubbish. I think my code's very bad, I'm a beginner in PyGame and I need help.
Here's my code:
import pygame
import time
times = 20000
# initialize Pygame
pygame.init()
clock = pygame.time.Clock()
# create pygam window
screen_width = 467
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
# Set cursor position and cursor direction variables
cursor_x = 71
cursor_y = 25
cursor_direction = 1
cursor_speed = 3
# Set cursor position and direction variables y
cursor_y_x = 13
cursor_y_y = 196
cursor_y_direction = 1
cursor_y_speed = 1.5
claw_placed_x = False
claw_placed_y = False
# Create the Claw class as a subclass of pygame.sprite.Sprite
class Claw(pygame.sprite.Sprite):
def __init__(self, open_image, closed_image, x, y):
super().__init__()
self.open_image = open_image
self.closed_image = closed_image
self.image = self.open_image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.is_open = True
self.target_y = -200
self.claw_placed = False
self.time_to_raise = 3000 # time in milliseconds before raising the claw
self.time_since_placement = self.time_to_raise
self.claw_placed = False # claw placement indicator
def update(self):
if self.claw_placed:
if self.rect.y < self.target_y:
# Calculate the distance to travel
distance = self.target_y - self.rect.y
# Calculate moving speed based on distance
speed = distance / 10
# updating
self.rect.y += speed
pygame.display.update()
clock.tick(60)
else:
# Claw is on targeted position
self.time_since_placement += 1 # increment time since last placement
if self.time_since_placement >= self.time_to_raise:
# if the time since placement is greater than or equal to the time to wait, raise the gripper
self.target_y = -200
self.claw_placed = False
self.time_since_placement = 0 # reset counter
else:
if self.rect.y > self.target_y:
# Calculate the distance to travel
distance = self.rect.y - self.target_y
# # Calculate moving speed based on distance
speed = distance / 10
# updating
self.rect.y -= speed
pygame.display.update()
clock.tick(60)
def toggle(self):
self.is_open = not self.is_open
if self.is_open:
self.image = self.open_image
else:
self.image = self.closed_image
self.target_y = -200 # Make the Claw going to his initial position
self.claw_placed = False
# load images
background_image = pygame.image.load("background.png").convert()
claw_open_image = pygame.image.load("claw_open.png").convert_alpha()
claw_closed_image = pygame.image.load("claw_close.png").convert_alpha()
bkg_front = pygame.image.load("backg.png").convert_alpha()
rod_x = pygame.image.load("rod_x.png").convert_alpha()
rod_y = pygame.image.load("rod_y.png").convert_alpha()
# creete claw object with two state (closed or open)
claw = Claw(claw_open_image, claw_closed_image, 250, -200)
# create pygame window
screen = pygame.display.set_mode((screen_width, screen_height))
# main func
def main():
global cursor_x, cursor_y, cursor_direction, cursor_speed, claw_placed_y, claw_placed_x, times
global cursor_y_x, cursor_y_y, cursor_y_direction
# game loop
def game():
global cursor_x, cursor_y, cursor_direction, cursor_speed, claw_placed_y, claw_placed_x, running, times
global cursor_y_x, cursor_y_y, cursor_y_direction, cursor_y_speed
times = times-1
# manage events
for event in pygame.event.get():
if event.type == pygame.QUIT:
# Quit game if wndow closed
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not claw_placed_x:
# Place the claw on the x axis
claw.rect.x = cursor_x - 71
claw_placed_x = True
cursor_speed = 0
elif event.key == pygame.K_SPACE and claw_placed_x and not claw_placed_y:
# Place the claw on the y axis
claw.rect.y = cursor_y_y - 443
claw_placed_y = True
claw.toggle()
pygame.display.update()
claw.target_y = -200
# update horizontale position of the cursor
cursor_x += cursor_direction
# reverse cursor direction
if cursor_x < 71 or cursor_x > 384:
cursor_direction *= -1
if claw_placed_x == True:
# update vertical position of the cursor y if the claw isn' placed on y axis
if not claw_placed_y:
cursor_y_y += cursor_y_direction*cursor_y_speed
if cursor_y_y < 196 or cursor_y_y > 474:
cursor_y_direction *= -1
cursor_y_speed += 0.05
if cursor_y_speed > 5:
cursor_y_speed = 0.5
try:
# display background
screen.blit(background_image, (0, 0))
# display actual position of the claw
screen.blit(claw.image, (claw.rect.x, claw.rect.y))
# display claw machine
screen.blit(bkg_front, (0, 0))
except pygame.error as e:
print("Erreur:", e)
# display x cursor
if not claw_placed_x:
screen.blit(rod_x, (8, 30))
pygame.draw.rect(screen, (64, 64, 64),
(cursor_x, cursor_y, 10, 20))
# display y cursor
if claw_placed_x == True:
if not claw_placed_y:
screen.blit(rod_y, (18, 189))
pygame.draw.rect(screen, (64, 64, 64),
(cursor_y_x, cursor_y_y, 20, 10))
# update window
pygame.display.update()
if times <= 0:
pygame.quit()
if claw_placed_y:
claw.update()
running = True
while running:
game()
# call main function
if __name__ == "__main__":
main()
I tried to use different methods and different way to solve the problems like chatGPT, youtube, blackbox and google but nothing worked.
I expect to make the claw going back to his initial position (in -200) after going down to the position of the Y cursor.
Nothing worked and either the claw stays up or stays down.(time.sleep or pygame.time didn't work). Can anyone help?
答案1
得分: 1
以下是您提供的代码的中文翻译:
第一点,似乎if claw_placed_y:
语句应该放在"game"方法中。
第二点,你真的不需要在这里嵌套一个方法在另一个方法中,看看这样写是等效的:
在游戏方法/循环内部,有这个:
还有另一个条件没有处理,在claw_placed_x和claw_placed_y都为True时。这两个变量只会在此循环中从False变为True,所以一旦你按了两次空格键,它们就不会再改变。你必须处理所有情况,以满足你所期望的流程。
最后,你可以在你的Claw类中添加一个方法,比如reset。保留你想要的任何起始值...在你想要的时候调用claw.reset()函数。
希望这有所帮助!如果您需要进一步的解释或翻译,请随时告诉我。
英文:
A couple of thoughts that may help. First:
def main():
...
def game():
...
if claw_placed_y:
claw.update()
running = True
while running:
game()
It seems like that if claw_placed_y:
statement is supposed to be in the "game" method.
Second, you really don't need to have a method within a method for this, see how this is equivalent:
def main():
...
if claw_placed_y:
claw.update()
running = True
while running:
...
# This is the contents of that game() #
Inside the game method / while loop there is this:
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not claw_placed_x:
# Place the claw on the x axis
claw.rect.x = cursor_x - 71
claw_placed_x = True
cursor_speed = 0
elif event.key == pygame.K_SPACE and claw_placed_x and not claw_placed_y:
# Place the claw on the y axis
claw.rect.y = cursor_y_y - 443
claw_placed_y = True
claw.toggle()
pygame.display.update()
claw.target_y = -200
There is another condition that isn't handled here, when both claw_placed_x and claw_placed_y are True. Both of these variables are only ever set from False to True in this loop, so once you've hit the space twice they will never change. You have to handle all cases for whatever flow you are trying to make.
Finally, you could add a method in your Claw class, like reset. Keep whatever starting values you want ... call the claw.reset() function whenever you want.
class Claw(pygame.sprite.Sprite):
def __init__(self, open_image, closed_image, x, y):
super().__init__()
# your other initialization stuff ...
# could save the starting x and y if we want
self.startx = x
self.starty = y
def reset(self):
# the new reset method could set values to some static or stored value
self.rect.x = self.startx
self.rect.y = self.starty
self.target_y = -200
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论