Python 矩形闪烁,不随其余部分移动 导入pygame 导入系统 从pygame.sprite导入sprite 课程设置: 定义初始化(自): 自雨滴速度=3 自雨滴方向=-1 自雨滴_dropseed=3 self.backgroundcolor=(30,30,30) 下课(精灵): 定义初始化(自): super()。\uuuu init\uuuuu() self.image=pygame.image.load(“raindrop.png”) self.rect=self.image.get_rect() self.rect.y=self.rect.height self.rect.x=self.rect.width self.x_cord=self.rect.x self.y_cord=self.rect.y 类别雨滴: 定义初始化(自): pygame.init() self.screen=pygame.display.set_模式((1200800)) self.settings=settings() self.backgroundcolor=self.settings.backgroundcolor self.raindrops=pygame.sprite.Group() self.screen\u rect=self.screen.get\u rect() 自我。_添加_雨滴() def_添加_雨滴(自身): 新雨滴=雨滴() 落差高度=新落差高度 drop\u width=新的\u raindrop.rect.width 打印(下拉宽度) 屏幕空间=self.screen\u rect.width 屏幕高度空间=self.screen\u rect.height aviable_row_space=屏幕高度_space//(下降高度*2) 可飞行屏幕空间=屏幕空间-(下降宽度*2) 列的数量=可用屏幕空间//(下拉宽度*2) self.\u添加列(列的数量、可用行空间) 定义添加列(自身、列数、可用行空间): 对于范围内下降的高度(可用行空间): 对于范围内的下降次数(列的数量): 下降=下降() drop.x_线缆=(drop.rect.width*2)*drop数量 drop.y_线=(drop.rect.height*2)*drop的高度 drop.rect.x=drop.x_线 drop.rect.y=drop.y\u线 self.raindrops.add(drop) def_带下雨滴(自身): 对于自动雨滴: drop.y_cord+=自设置.raindrop\u dropseed drop.rect.y=drop.y\u线 def_更新_删除(自): 高度\计数器=1 self.raindrops.update() 用于输入self.raindrops.copy(): drop.x_线+=self.settings.raindrop_方向*self.settings.raindrop_速度 drop.rect.x=drop.x_线 如果drop.rect.right>=self.screen\u rect.right: self.settings.raindrop\u方向=-1 自我。(带来)雨滴() elif drop.rect.left=self.screen\u rect.height或drop.rect.y
你的问题在于当你意识到自己撞到了屏幕边缘时你会做什么。当你发现任何一滴从屏幕上掉下来的时候,你就可以这样做,在这一点上,你可以反转方向。问题是,您已经将顶行上的一些液滴向一个方向移动,但在识别出屏幕边缘后,您将继续在同一过程中向相反方向移动其余的液滴。这就是事情如何走出怪圈的 你要做的是注意你已经到达了屏幕的边缘,但是没有马上做任何不同的事情,所以你仍然以相同的方式处理屏幕上的所有水滴。画完所有的水滴后,再改变方向 这里有一个版本的Python 矩形闪烁,不随其余部分移动 导入pygame 导入系统 从pygame.sprite导入sprite 课程设置: 定义初始化(自): 自雨滴速度=3 自雨滴方向=-1 自雨滴_dropseed=3 self.backgroundcolor=(30,30,30) 下课(精灵): 定义初始化(自): super()。\uuuu init\uuuuu() self.image=pygame.image.load(“raindrop.png”) self.rect=self.image.get_rect() self.rect.y=self.rect.height self.rect.x=self.rect.width self.x_cord=self.rect.x self.y_cord=self.rect.y 类别雨滴: 定义初始化(自): pygame.init() self.screen=pygame.display.set_模式((1200800)) self.settings=settings() self.backgroundcolor=self.settings.backgroundcolor self.raindrops=pygame.sprite.Group() self.screen\u rect=self.screen.get\u rect() 自我。_添加_雨滴() def_添加_雨滴(自身): 新雨滴=雨滴() 落差高度=新落差高度 drop\u width=新的\u raindrop.rect.width 打印(下拉宽度) 屏幕空间=self.screen\u rect.width 屏幕高度空间=self.screen\u rect.height aviable_row_space=屏幕高度_space//(下降高度*2) 可飞行屏幕空间=屏幕空间-(下降宽度*2) 列的数量=可用屏幕空间//(下拉宽度*2) self.\u添加列(列的数量、可用行空间) 定义添加列(自身、列数、可用行空间): 对于范围内下降的高度(可用行空间): 对于范围内的下降次数(列的数量): 下降=下降() drop.x_线缆=(drop.rect.width*2)*drop数量 drop.y_线=(drop.rect.height*2)*drop的高度 drop.rect.x=drop.x_线 drop.rect.y=drop.y\u线 self.raindrops.add(drop) def_带下雨滴(自身): 对于自动雨滴: drop.y_cord+=自设置.raindrop\u dropseed drop.rect.y=drop.y\u线 def_更新_删除(自): 高度\计数器=1 self.raindrops.update() 用于输入self.raindrops.copy(): drop.x_线+=self.settings.raindrop_方向*self.settings.raindrop_速度 drop.rect.x=drop.x_线 如果drop.rect.right>=self.screen\u rect.right: self.settings.raindrop\u方向=-1 自我。(带来)雨滴() elif drop.rect.left=self.screen\u rect.height或drop.rect.y,python,pygame,Python,Pygame,你的问题在于当你意识到自己撞到了屏幕边缘时你会做什么。当你发现任何一滴从屏幕上掉下来的时候,你就可以这样做,在这一点上,你可以反转方向。问题是,您已经将顶行上的一些液滴向一个方向移动,但在识别出屏幕边缘后,您将继续在同一过程中向相反方向移动其余的液滴。这就是事情如何走出怪圈的 你要做的是注意你已经到达了屏幕的边缘,但是没有马上做任何不同的事情,所以你仍然以相同的方式处理屏幕上的所有水滴。画完所有的水滴后,再改变方向 这里有一个版本的\u update\u drops可以做到这一点: import
\u update\u drops
可以做到这一点:
import pygame
import sys
from pygame.sprite import Sprite
class Settings:
def __init__(self):
self.raindrop_speed = 3
self.raindrop_direction = -1
self.raindrop_dropseed = 3
self.backgroundcolor = (30,30,30)
class Drop(Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("raindrop.png")
self.rect = self.image.get_rect()
self.rect.y = self.rect.height
self.rect.x = self.rect.width
self.x_cord = self.rect.x
self.y_cord = self.rect.y
class RainDrop:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((1200,800))
self.settings = Settings()
self.backgroundcolor = self.settings.backgroundcolor
self.raindrops = pygame.sprite.Group()
self.screen_rect = self.screen.get_rect()
self._add_raindrops()
def _add_raindrops(self):
new_raindrop = Drop()
drop_height = new_raindrop.rect.height
drop_width = new_raindrop.rect.width
print(drop_width)
screen_space = self.screen_rect.width
screen_height_space = self.screen_rect.height
aviable_row_space = screen_height_space//(drop_height*2)
avivable_screen_space = screen_space - (drop_width*2)
amount_of_columns = avivable_screen_space//(drop_width*2)
self._add_columns(amount_of_columns,aviable_row_space)
def _add_columns(self,amount_of_columns,aviable_row_space):
for height_of_drops in range(aviable_row_space):
for number_of_drops in range(amount_of_columns):
drop = Drop()
drop.x_cord = (drop.rect.width *2)* number_of_drops
drop.y_cord =(drop.rect.height *2)* height_of_drops
drop.rect.x = drop.x_cord
drop.rect.y = drop.y_cord
self.raindrops.add(drop)
def _bring_down_raindrops(self):
for drop in self.raindrops:
drop.y_cord += self.settings.raindrop_dropseed
drop.rect.y = drop.y_cord
def _update_drops(self):
height_counter = 1
self.raindrops.update()
for drop in self.raindrops.copy():
drop.x_cord += self.settings.raindrop_direction * self.settings.raindrop_speed
drop.rect.x = drop.x_cord
if drop.rect.right >= self.screen_rect.right:
self.settings.raindrop_direction = -1
self._bring_down_raindrops()
elif drop.rect.left <= 0:
self.settings.raindrop_direction = 1
self._bring_down_raindrops()
#if drop.rect.y >= self.screen_rect.height or drop.rect.y <= 0:
# drop.rect.x = drop.rect.width
# drop.y_cord = drop.rect.height
# drop.rect.y = drop.y_cord
print(height_counter)
print(self.raindrops)
def _update_game(self):
self.screen.fill(self.backgroundcolor)
self.raindrops.draw(self.screen)
pygame.display.flip()
def _check_events(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
sys.exit()
def run_game(self):
while True:
self._check_events()
self._update_drops()
self._update_game()
if __name__ == "__main__":
rd = RainDrop()
rd.run_game()
def\u更新\u删除(自):
高度\计数器=1
self.raindrops.update()
#假设我们没有击中屏幕的任何一个边缘
反向=0
用于输入self.raindrops.copy():
drop.x_线+=self.settings.raindrop_方向*self.settings.raindrop_速度
drop.rect.x=drop.x_线
如果drop.rect.right>=self.screen\u rect.right:
#记住,我们击中了屏幕的右边缘
反向=-1
elif drop.rect.left
def _update_drops(self):
height_counter = 1
self.raindrops.update()
# assume we haven't hit either edge of the screen
reverse = 0
for drop in self.raindrops.copy():
drop.x_cord += self.settings.raindrop_direction * self.settings.raindrop_speed
drop.rect.x = drop.x_cord
if drop.rect.right >= self.screen_rect.right:
# remember that we hit the right edge of the screen
reverse = -1
elif drop.rect.left <= 0:
# remember that we hit the left edge of the screen
reverse = 1
# if we hit one of the edges, change directions and drop down
if reverse != 0:
self.settings.raindrop_direction = reverse
self._bring_down_raindrops()