英文:
Sprite in Pygame moving up and down in a endless loop
问题
不了解pygame,所以我只是复制了pygame.org上的教程,精灵只是在一个循环中上下旋转。
import sys, pygame
pygame.init()
size = width, height = 500, 500
speed = [2, 2]
blue = 85, 118, 250
screen = pygame.display.set_mode(size)
bird = pygame.image.load("bird-sprite.png")
birdrect = bird.get_rect()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
birdrect = birdrect.move(speed)
if birdrect.left < 0 or birdrect.right > width:
speed[0] = -speed[0]
if birdrect.top < 0 or birdrect bottom > height:
speed[1] = -speed[1]
screen.fill(blue)
screen.blit(bird, birdrect)
pygame.display.flip()
我尝试为精灵创建移动,但精灵只是在整个窗口中不停地移动,形成一个无限循环。
我使用了pygame.org,因为我找不到精确的移动教程,所以是的。
我不太了解Python,所以不知道我做错了什么。
英文:
I don't know much about pygame so I just copied a tutorial on pygame.org and the sprite just spins up and down in a loop.
import sys, pygame
pygame.init()
size = width, height = 500, 500
speed = [2, 2]
blue = 85, 118, 250
screen = pygame.display.set_mode(size)
bird = pygame.image.load("bird-sprite.png")
birdrect = bird.get_rect()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
birdrect = birdrect.move(speed)
if birdrect.left < 0 or birdrect.right > width:
speed[0] = -speed[0]
if birdrect.top < 0 or birdrect.bottom > height:
speed[1] = -speed[1]
screen.fill(blue)
screen.blit(bird, birdrect)
pygame.display.flip()
I tried to create movement for a sprite but instead the sprite just moves around the entire window without stopping in a endless loop.
I used pygame.org because I couldn't find a exact tutorial on movement so yeah.
I don't know much Python so I don't know what I did wrong.
答案1
得分: 0
以下是代码部分的中文翻译:
# 这行代码按照每帧2个像素的速度移动鸟的精灵
birdrect = birdrect.move(speed)
# 这些代码检查鸟的精灵是否超出屏幕
# 如果是的话,速度会反转,所以鸟会开始朝相反的方向移动
if birdrect.left < 0 or birdrect.right > width:
speed[0] = -speed[0]
if birdrect.top < 0 or birdrect.bottom > height:
speed[1] = -speed[1]
这是使用箭头键实现鸟移动的简单代码实现:
import sys, pygame
pygame.init()
size = width, height = 500, 500
speed = [0.1, 0.1]
blue = 85, 118, 250
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
bird = pygame.image.load("bird-sprite.png")
birdrect = bird.get_rect()
# 鸟的坐标
bird_x, bird_y = birdrect.x, birdrect.y
# 鸟的速度,以像素/秒为单位
speed = 200
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
# 计算自上一帧以来经过的毫秒数
# 这对于平滑的移动是必要的
time_passed = clock.tick(60) / 1000
# 获取按键状态
pressed_keys = pygame.key.get_pressed()
# 如果按下左箭头键 - 从鸟的X坐标中减去速度*time_passed
# 这意味着当你按下左箭头键1秒钟时,鸟会向左移动200像素
# 对于每个键都进行相同的计算
if pressed_keys[pygame.K_LEFT]:
bird_x -= speed * time_passed
if pressed_keys[pygame.K_RIGHT]:
bird_x += speed * time_passed
if pressed_keys[pygame.K_DOWN]:
bird_y += speed * time_passed
if pressed_keys[pygame.K_UP]:
bird_y -= speed * time_passed
# 将更改应用到鸟的rect
birdrect.x = int(bird_x)
birdrect.y = int(bird_y)
# 绘制所有内容
screen.fill(blue)
screen.blit(bird, birdrect)
# 更新显示
pygame.display.flip()
希望这些翻译对你有帮助。
英文:
Well, it does exactly what code tells it to do - move bird every frame by 2 pixels (speed
variable):
# This line of code moves bird sprite every frame by 2 pixels
birdrect = birdrect.move(speed)
# This lines of code checks if bird sprite is outside of the screen
# If it is - then speed inverts, so bird starts moving in the opposite way
if birdrect.left < 0 or birdrect.right > width:
speed[0] = -speed[0]
if birdrect.top < 0 or birdrect.bottom > height:
speed[1] = -speed[1]
If you want to implement movement, you should really read a bunch of tutorials about pygame.
Here is my simple implementation of movement (birds moves using arrow keys):
import sys, pygame
pygame.init()
size = width, height = 500, 500
speed = [0.1, 0.1]
blue = 85, 118, 250
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
bird = pygame.image.load("bird-sprite.png")
birdrect = bird.get_rect()
# Coordinates of bird
bird_x, bird_y = birdrect.x, birdrect.y
# Speed of bird in px/second
speed = 200
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
# Calculate how many milliseconds passed since previous frames
# This is necessary for smooth movement
time_passed = clock.tick(60) / 1000
# Get pressed keys state
pressed_keys = pygame.key.get_pressed()
# If LEFT is pressed - subtract speed*time_passed from bird`s X coordinate
# this means that when you press LEFT for 1 second - bird will move 200 pixels left
# Do same calculations for every key
if pressed_keys[pygame.K_LEFT]:
bird_x -= speed * time_passed
if pressed_keys[pygame.K_RIGHT]:
bird_x += speed * time_passed
if pressed_keys[pygame.K_DOWN]:
bird_y += speed * time_passed
if pressed_keys[pygame.K_UP]:
bird_y -= speed * time_passed
# Apply changes to bird's rect
birdrect.x = int(bird_x)
birdrect.y = int(bird_y)
# Draw everyting
screen.fill(blue)
screen.blit(bird, birdrect)
# Update display
pygame.display.flip()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论