【发布时间】:2018-09-22 21:45:25
【问题描述】:
我实际上正在制作一个程序,它可以识别两个 PS3 控制器并将它们的图像连同它们的控制器名称和其他“背景精灵”一起传送到屏幕上。我希望用户单击一个控制器,然后他们的控制器上会出现一个圆圈或类似的东西 - 这样他们就知道他们已经拿起了哪个,然后当他们的按钮被释放时,圆圈会消失(或者被我所有的背景艺术有效地从屏幕上删除圆圈)。我想这个背景列表以及它似乎没有像我想要的那样更新屏幕的方式是这里的基础......
为了简化这个问题,我给出了一个相关的简短示例: 我想将控制器显示为由控制器图像表示的附加设备,或者在这段缩短的代码中显示为白色垂直线。 我已经使用列表 bgd[] 构建了背景图像。 当我按下任一操纵杆上的按钮时,我希望看到相关控制器(白线)出现一条临时灰线,当按键被抬起时它应该消失。我的问题是我无法让灰线消失。 我以为我只需要用内容覆盖所有内容 不变的背景图片 bgd[].
import pygame
from pygame.locals import *
import os
# Place the display 'screen' at top.
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (0,10)
# Iterate over all graphics which make up the 'unalterable background'
# This should put graphics into memory ready for blitting later...
#
def reDraw(background):
for r in (range (len(background))):
background[r]
pygame.init()
size = display_width, display_height = 100,100
screen = pygame.display.set_mode(size)
# List of 'unalterable background graphics/sprites
bgd = []
# Bottom of the virtual z-stack of screen graphics - a black background
bgd.append(screen.fill((0,0,0)))
# Count the number of Joysticks
joystick_count = pygame.joystick.get_count()
# One joystick is represented by a grey line on the left of the screen
if joystick_count == 1:
bgd.append(pygame.draw.rect(screen, (255,255,255), (10,20,10,60)))
reDraw(bgd) #Ready to display.update later
# A second joystick is represented by a grey line on the right of the screen
if joystick_count == 2:
bgd.append(pygame.draw.rect(screen, (255,255,255), (10,20,10,60)))
bgd.append(pygame.draw.rect(screen, (255,255,255), (80,20,10,60)))
reDraw(bgd) #Ready to display.update later
# Now to create TEMPORARY SCREEN FEATURES, which should only be present
# Whilst the buttons are pressed on each PS3 controller joystick :
run = True
while run == True:
for j in range(joystick_count):
joystick = pygame.joystick.Joystick(j)
joystick.init()
buttons = joystick.get_numbuttons()
reDraw(bgd) #Ready to display.update later
#Now to put all the queued draw statements onto the screen
pygame.display.update()
for i in range( buttons ):
button = joystick.get_button( i )
if button != 0 and j == 0 :
pygame.draw.rect(screen, (150,150,150), (20,20,30,10))
if button != 0 and j == 1 :
pygame.draw.rect(screen, (150,150,150), (50,70,30,10))
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
quit()
我附上一张解释性图片:
【问题讨论】:
-
能否更正代码缩进?
-
完成 - 对不起!
标签: python python-3.x pygame