如何解决使用pygame在屏幕上创建雨滴
这是“崩溃”课程的练习-在这个阶段,我试图创建一排雨滴,但是我相信我的for循环中有些东西坏了。.我在每次迭代时更新图像的rect位置,然后添加进入精灵组,为什么不将draw()放到屏幕上?
import sys
import pygame
from raindrops import Raindrop
from pygame.sprite import Group
def let_it_rain():
'''initialize pygame,settings,and screen object'''
pygame.init()
screen_width = 1200
screen_height = 800
bg_color = (144,177,226)
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Let It Rain")
raindrop = Raindrop(screen)
raindrops = Group()
#number of drops in a row
spacex = screen_width - (2 * raindrop.rect.width)
raindrop_number_x = int(spacex / (2 * raindrop.rect.width))
#start window for raindrops
while True:
screen.fill(bg_color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
for raindrop_number in range(raindrop_number_x):
raindrop = Raindrop(screen)
raindrop.rect.x = raindrop.rect.x + 2 * raindrop.rect.x * raindrop_number_x
raindrops.add(raindrop)
raindrops.draw(screen)
pygame.display.flip()
let_it_rain()
这是我在另一个模块中的雨滴类
import pygame
from pygame.sprite import Sprite
class Raindrop(Sprite):
def __init__(self,screen):
#load image
super(Raindrop,self).__init__()
self.screen = screen
self.pic = pygame.image.load('rain.png')
self.image = pygame.transform.smoothscale(self.pic,(50,60))
self.rect = self.image.get_rect()
#starting position
self.rect.x = self.rect.width
self.rect.y = self.rect.height
def blit(self):
raindrops.draw(screen)
# self.screen.blit(self.image,self.rect)
感觉像是blit vs. draw或我的位置没有以某种方式更新
解决方法
在主循环中,设置放置矩形时,请使用循环变量而不是放置计数:
for raindrop_number in range(raindrop_number_x):
raindrop = Raindrop(screen)
raindrop.rect.x = raindrop.rect.x + 2 * raindrop.rect.x * raindrop_number # use loop variable
raindrops.add(raindrop)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。