我正在尝试创建一个 purple rain program在 python 中使用 pygame.我创建了一个名为 Rain 的类,我将用它来制作多个雨滴,但我尝试测试是否只有 1 个雨物体会掉落,但是......它没有,而且我似乎无法弄清楚我做错了什么。虽然这可能是一个菜鸟和 super 明显的问题,但请记住,我仍在学习 pygame 和 python,所以......这是我的代码。
import pygame, sys
from pygame.locals import *
import random
pygame.init()
FPS = 60
BLACK = (0,0,0)
WHITE = (255,255,255)
PURPLE = (130, 50, 200)
screen_resolution = (800, 600)
screen = pygame.display.set_mode(screen_resolution)
pygame.display.set_caption('Purple Rain')
clock = pygame.time.Clock()
gameLoop = True
class Rain:
def __init__(self, x, y, width, height, yspeed):
self.rect = pygame.Rect(x,y,width,height)
self.yspeed = yspeed
self.x = x
self.y = y
def fall(self):
self.rect.move(self.x, self.y - self.yspeed)
def draw(self):
pygame.draw.rect(screen, PURPLE, self.rect)
drop = Rain(400,300,2,20,9)
while gameLoop:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.fill(WHITE)
drop.draw()
drop.fall()
pygame.display.flip()
clock.tick(FPS)
最佳答案
self.rect.move(self.x, self.y - self.yspeed)
返回一个新矩形,但不会更改当前矩形。
您想要执行 self.rect.move_ip(0, - self.yspeed)
以便更新 self.rect
对象。
(并且参数是偏移量,而不是绝对坐标)
关于python - 学习Pygame,为什么这个 'raindrop'(矩形对象)不会掉落?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41579499/