我正在尝试使用 Pygame 在 python 中下雨,但是在运行代码时所有的水滴都冻结了

这是我用过的代码。我是初学者,所以我不能在这里使用“对象”的概念。当我运行代码时,绘制了多条线,但所有线都被冻结了。


    import pygame

    import random


    y=0

    screen=pygame.display.set_mode((600,600))

    pygame.display.set_caption('Trial')

    running=1

    while running:

        for event in pygame.event.get():

            if event.type==pygame.QUIT:

                running=0

        x=random.randint(0,600)

        pygame.draw.line(screen,(255,255,255),(x,y),(x,y+10))

        y+=1

        pygame.display.flip()


慕仙森
浏览 109回答 1
1回答

一只萌萌小番薯

我不知道这是否是您所期望的,或者这是否是正确的方法,但我正在更新屏幕并等待再次用黑色填充屏幕。这似乎有效,并且线路没有冻结。import pygameimport randomy = 0screen = pygame.display.set_mode((600, 600))pygame.display.set_caption('Trial')running = 1while running:&nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; running = 0&nbsp; &nbsp; x = random.randint(0, 600)&nbsp; &nbsp; pygame.draw.line(screen, (255, 255, 255), (x, y), (x, y + 10))&nbsp; &nbsp; y += 1&nbsp; &nbsp; pygame.display.update()&nbsp; &nbsp; pygame.display.flip()&nbsp; &nbsp; pygame.time.wait(1)&nbsp; &nbsp; screen.fill(pygame.Color("black"))这个档案中的雨水发生器代码在产生雨水效果方面做得很好。我已经格式化代码并将其粘贴在下面(因为该站点的格式有点混乱)。import pygameimport randomimport timeSCREENSIZE = 640, 480class Rain(object):&nbsp; &nbsp; def __init__(self, screen, height = 160, speed = 3, color = (180, 215, 228, 255), numdrops = 10):&nbsp; &nbsp; &nbsp; &nbsp; 'Create and reuse raindrop particles'&nbsp; &nbsp; &nbsp; &nbsp; self.screen&nbsp; &nbsp; &nbsp;= screen&nbsp; &nbsp; &nbsp; &nbsp; self.drops&nbsp; &nbsp; &nbsp; = []&nbsp; &nbsp; &nbsp; &nbsp; self.height&nbsp; &nbsp; &nbsp;= height&nbsp; &nbsp; &nbsp; &nbsp; self.speed&nbsp; &nbsp; &nbsp; = speed&nbsp; &nbsp; &nbsp; &nbsp; self.color&nbsp; &nbsp; &nbsp; = color&nbsp; &nbsp; &nbsp; &nbsp; self.numdrops&nbsp; &nbsp;= numdrops&nbsp; &nbsp; &nbsp; &nbsp; for i in range(self.numdrops):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Randomize the size of the raindrop.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raindropscale = random.randint(40, 100) / 100.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; w, h = 3, int(raindropscale * self.height)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # The bigger the raindrop, the faster it moves.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; velocity = raindropscale * self.speed/10.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pic = pygame.Surface((w, h), pygame.SRCALPHA, 32).convert_alpha()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colorinterval = float(self.color[3] * raindropscale)/h&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r, g, b = self.color[:3]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for j in range(h):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # The smaller the raindrop, the dimmer it is.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = int(colorinterval * j)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pic.fill( (r, g, b, a), (1, j, w-2, 1) )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.draw.circle(pic, (r, g, b, a), (1, h-2), 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drop = Rain.Drop(self.speed, velocity, pic)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.drops.append(drop)&nbsp; &nbsp; def Timer(self, now):&nbsp; &nbsp; &nbsp; &nbsp; ' Render the rain'&nbsp; &nbsp; &nbsp; &nbsp; dirtyrects = []&nbsp; &nbsp; &nbsp; &nbsp; for drop in self.drops:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r = drop.Render(self.screen, now)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if r:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = r.collidelist(dirtyrects)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i > -1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dirtyrects[i].union_ip(r)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dirtyrects.append(r)&nbsp; &nbsp; &nbsp; &nbsp; return dirtyrects&nbsp; &nbsp; def AdjustSpeed(self, adj):&nbsp; &nbsp; &nbsp; &nbsp; newspeed = self.speed + adj&nbsp; &nbsp; &nbsp; &nbsp; newspeed = max(1, newspeed)&nbsp; &nbsp; &nbsp; &nbsp; newspeed = min(100, newspeed)&nbsp; &nbsp; &nbsp; &nbsp; self.speed = newspeed&nbsp; &nbsp; &nbsp; &nbsp; for drop in self.drops:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drop.SetSpeed(newspeed)&nbsp; &nbsp; &nbsp; &nbsp; print ('Rain speed: %d' % newspeed)&nbsp; &nbsp; class Drop(object):&nbsp; &nbsp; &nbsp; &nbsp; ' Rain drop used by rain generator'&nbsp; &nbsp; &nbsp; &nbsp; nexttime = 0&nbsp; &nbsp;# The next time the raindrop will draw&nbsp; &nbsp; &nbsp; &nbsp; interval = .01 # How frequently the raindrop should draw&nbsp; &nbsp; &nbsp; &nbsp; def __init__(self, speed, scale, pic):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' Initialize the rain drop'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speed = speed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.scale = scale&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.pic = pic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.size = pic.get_size()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.SetSpeed(speed)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.pos = [random.random() * SCREENSIZE[0], -random.randint(-SCREENSIZE[1], SCREENSIZE[1])]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.currentspeed = speed&nbsp; &nbsp; &nbsp; &nbsp; def SetSpeed(self, speed):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' Speed up or slow down the drop'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speed = speed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.velocity = self.scale * self.speed/10.0&nbsp; &nbsp; &nbsp; &nbsp; def Reset(self):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' Restart the drop at the top of the screen.'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.pos = [random.random() * SCREENSIZE[0], -random.random() * self.size[1] - self.size[1]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.currentspeed = self.speed&nbsp; &nbsp; &nbsp; &nbsp; def Render(self, screen, now):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' Draw the rain drop'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if now < self.nexttime:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return None&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.nexttime = now + self.interval&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oldrect = pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1]+self.currentspeed)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.pos[1] += self.currentspeed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrect = pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r = oldrect.union(newrect)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; screen.blit(self.pic, self.pos)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.currentspeed += self.velocity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.pos[1] > SCREENSIZE[1]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.Reset()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return rdef main():&nbsp; &nbsp; # Initialize pygame&nbsp; &nbsp; pygame.init()&nbsp; &nbsp; pygame.key.set_repeat(500, 30)&nbsp; &nbsp; screen = pygame.display.set_mode(SCREENSIZE, 0, 32)&nbsp; &nbsp; # Create rain generator&nbsp; &nbsp; rain = Rain(screen)&nbsp; &nbsp; print ('right arrow to increase speed, left arrow to decrease speed.')&nbsp; &nbsp; # Main loop&nbsp; &nbsp; quitgame = 0&nbsp; &nbsp; while not quitgame:&nbsp; &nbsp; &nbsp; &nbsp; # Emulate CPU usage.&nbsp; &nbsp; &nbsp; &nbsp; # Commenting this out will no longer matter,&nbsp; &nbsp; &nbsp; &nbsp; # as the raindrops update on a timer.&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(.01)&nbsp; &nbsp; &nbsp; &nbsp; # Draw rain&nbsp; &nbsp; &nbsp; &nbsp; dirtyrects = rain.Timer(time.time())&nbsp; &nbsp; &nbsp; &nbsp; # Update the screen for the dirty rectangles only&nbsp; &nbsp; &nbsp; &nbsp; pygame.display.update(dirtyrects)&nbsp; &nbsp; &nbsp; &nbsp; # Fill the background with the dirty rectangles only&nbsp; &nbsp; &nbsp; &nbsp; for r in dirtyrects:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; screen.fill((0, 0, 0), r)&nbsp; &nbsp; &nbsp; &nbsp; # Look for user events&nbsp; &nbsp; &nbsp; &nbsp; pygame.event.pump()&nbsp; &nbsp; &nbsp; &nbsp; for e in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if e.type in [pygame.QUIT, pygame.MOUSEBUTTONDOWN]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quitgame = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif e.type == pygame.KEYDOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if e.key == 27:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quitgame = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif e.key in [pygame.K_LEFT, pygame.K_UP]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rain.AdjustSpeed(-1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif e.key in [pygame.K_RIGHT, pygame.K_DOWN]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rain.AdjustSpeed(1)&nbsp; &nbsp; # Terminate pygame&nbsp; &nbsp; pygame.quit()if __name__ == "__main__":&nbsp; &nbsp; main()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go