Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从列表中的项目创建可滚动的矩形_Python_Pygame - Fatal编程技术网

Python 从列表中的项目创建可滚动的矩形

Python 从列表中的项目创建可滚动的矩形,python,pygame,Python,Pygame,我有一个程序,里面有一个名字列表。我希望能够将这些名字列在一个矩形上。我希望每个名称创建自己的矩形。本例为列表中的每个标题创建一个矩形,我将'放在列表的开头,因为我收到一个错误,说我不能在python中通过添加'来除以零 import pygame width,height = 800,600 screen = pygame.display.set_mode((width,height)) mover = 0 games = ['','Space Invaders','Snake'] whi

我有一个程序,里面有一个名字列表。我希望能够将这些名字列在一个矩形上。我希望每个名称创建自己的矩形。本例为列表中的每个标题创建一个矩形,我将
'
放在列表的开头,因为我收到一个错误,说我不能在python中通过添加
'
来除以零

import pygame

width,height = 800,600
screen = pygame.display.set_mode((width,height))

mover = 0
games = ['','Space Invaders','Snake']
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 4:
                if mover > 0:
                    mover -= 15
            if event.button == 5:
                if mover < (height-120)-60:
                    mover += 15
    screen.fill((47,79,79))
    for game in games:
        for num in range(len(games)):
            if num != 0:
                pygame.draw.rect(screen, (0,51,51), (100,((height-120)/num)-mover,width-200,20))
    pygame.draw.rect(screen, (0,51,51), (width-30,100,25,height-120)) #SCROLL BAR
    pygame.draw.rect(screen, (0,0,0), (width-29,mover+100,23,60)) #SCROLLING BAR
    pygame.draw.line(screen, (0,0,0), (0,100),(width,100), 4)
    pygame.display.flip()
导入pygame
宽度、高度=800600
screen=pygame.display.set_模式((宽度、高度))
移动器=0
游戏=['','太空入侵者','蛇']
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
如果event.type==pygame.MOUSEBUTTONDOWN:
如果event.button==4:
如果移动器>0:
移动器-=15
如果event.button==5:
如果移动器<(高度-120)-60:
移动器+=15
筛选填充((47,79,79))
游戏中的游戏:
对于范围内的num(len(游戏)):
如果num!=0:
pygame.draw.rect(屏幕,(0,51,51),(100,((高度-120)/num)-移动,宽度-200,20))
pygame.draw.rect(屏幕,(0,51,51),(宽-30100,25,高-120))#滚动条
pygame.draw.rect(屏幕,(0,0,0),(宽度-29,移动器+100,23,60))#滚动条
pygame.draw.line(屏幕,(0,0,0),(0100),(宽度,100),4)
pygame.display.flip()

我不确定如何使矩形更接近,使每个矩形之间只有5到10个像素。我也觉得有更好的方法可以做到这一点。

定义到屏幕顶部的距离。e、 g.100。定义线之间的距离。一条线的高度是20。如果两条线之间的间隙应为10,则距离为30。因此,行的开头是
110+num*30-mover

对于num,枚举中的游戏(游戏):
pygame.draw.rect(屏幕,(0,51,51),(100,110+num*30+mover,宽度-200,20))

注意,您可以使用,来获取列表索引和列表元素的元组。

现在我想起来了,我不知道为什么我尝试除法,而不是最好的数学