当我运行下面的代码时,pygame rect不会移动。它设置为在每个左箭头按下时沿x方向移动-7,但没有任何反应。我试图打印矩形的位置,看起来它的位置从初始位置300更新到293,尽管屏幕上没有移动。非常感谢任何帮助!
import sys
import pygame
PINK = (255, 102, 255)
BLACK = (0, 0, 0)
pygame.init()
clock = pygame.time.Clock()
pygame.display.init()
font = pygame.font.SysFont("impact", 20)
screen = pygame.display.set_mode(750, 550)
class Paddle:
def __init__(self):
self.paddle = pygame.Rect(300, 730,
10,
52)
def draw_paddle(self):
# Draw paddle
pygame.draw.rect(screen, PINK,
self.paddle)
def move_paddle_left(self):
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.paddle.left -= 7
if self.paddle.left < 0:
self.paddle.left = 0
print(self.paddle)
class Game:
def __init__(self):
self.run()
Paddle()
# Levels().draw_level_one()
def run(self):
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# fps lock, screen fill and method call for input
clock.tick(60)
screen.fill(BLACK)
Paddle().draw_paddle()
Paddle().move_paddle_left()
pygame.display.flip()
# Creates instance of the game class, and runs it
if __name__ == "__main__":
Game()
Game().run()
答案 0 :(得分:1)
虽然我无法让你的代码实际绘制一个paddle,但我注意到你在循环的每次迭代中都创建了一个新的Paddle实例,因此每次位置都会重置为300.
请改为尝试:
// HTML
<div class="container">
<div class="img first-background"></div>
<div class="img second-background"></div>
<div class="img third-background"></div>
</div>
// CSS
.img{
width: 100%;
height: 33vh; // vh means ViewHeight, 33% of your window height
background-size: cover;
background-position: center;
}
.first-background{ background-image: url('/a-link-to-your-image.jpg') }
.second-background{ background-image: url('/a-second-link-to-your-image.jpg') }
.third-background{ background-image: url('/a-third-link-to-your-image.jpg') }
这样你就可以创建一个paddle对象,然后在每次迭代中重复使用它的值,并且它会一直向左移动直到它达到0。