我是使用 pygame 的新手。當我運行代碼時,我有一個帶有固定在螢屏左側的球的 GUI。我還有 10 個代表不同速度的不同按鈕。當我單擊一個速度按鈕時,我想在回圈中從左到右更改球位置,反之亦然(不移動球,只是消失并出現在另一個位置),直到我按下按鈕“停止”或更改速度. 我的兩個問題是:當代碼開始時,我的球以高速交替而不是速度為 0(我不明白為什么),第二個問題是當球移動時腳本不接受其他輸入按鈕. 如果長時間按下按鈕或快速按下按鈕超過 30 次,則它會收到命令(這很有用)
這里的代碼:
WHITE = (255, 255, 255)
BLUE = (83, 175, 190)
#initial set up
pygame.init()
size = width, height = 1500, 650
background = WHITE
screen = pygame.display.set_mode(size)
pygame.display.set_caption("nuovo alternato")
#circle = pygame.Surface((200, 300))
screen.fill(WHITE)
#drawing a circle
pygame.draw.circle(screen, (83, 175, 190), (105, 105), 100, 0)
pygame.draw.circle(screen, (255, 255, 255), (1395, 105), 100, 0)
counter = 0
speed=0
mainClock = pygame.time.Clock()
logo = pygame.image.load("C:\\...logodef.png").convert_alpha()
start = pygame.image.load("C:\\...startdef.png").convert_alpha()
stop = pygame.image.load("C:\\...stopdef.png").convert_alpha()
uno = pygame.image.load("C:\\...\\1.png").convert_alpha()
due = pygame.image.load("C:\\...\\2.png").convert_alpha()
tre = pygame.image.load("C:\\...\\3.png").convert_alpha()
quattro = pygame.image.load("C:\\...\\4.png").convert_alpha()
cinque = pygame.image.load("C:\\...\\5.png").convert_alpha()
sei = pygame.image.load("C:\\...\\6.png").convert_alpha()
sette = pygame.image.load("C:\\...\\7.png").convert_alpha()
otto = pygame.image.load("C:\\...\\8.png").convert_alpha()
nove = pygame.image.load("C:\\...\\9.png").convert_alpha()
zero = pygame.image.load("C:\\...\\0.png").convert_alpha()
class Button():
def __init__(self, x, y, image, scale):
width = image.get_width()
height = image.get_height()
self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.clicked = False
def draw(self):
action = False
pos = pygame.mouse.get_pos()
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
self.clicked = True
action = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
screen.blit(self.image, (self.rect.x, self.rect.y))
return action
immagine = Button(500, 430, logo, 1)
buttonvel1 = Button(200, 250, uno, 1)
buttonvel2 = Button(350, 250, due, 1)
buttonvel3 = Button(500, 250, tre, 1)
buttonvel4 = Button(650, 250, quattro, 1)
buttonvel5 = Button(800, 250, cinque, 1)
buttonvel6 = Button(950, 250, sei, 1)
buttonvel7 = Button(1100, 250, sette, 1)
buttonvel8 = Button(1250, 250, otto, 1)
buttonvel9 = Button(1400, 250, nove, 1)
buttonvel0 = Button(50, 250, zero, 1)
buttonstart = Button(400, 350, start, 1)
buttonstop = Button(900, 350, stop, 1)
clock=pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if counter % 2 == 0:
fl_color = BLUE
fl_color2 = WHITE
else:
fl_color = WHITE
fl_color2 = BLUE
pygame.draw.circle(screen, fl_color, (105, 105), 100, 0)
pygame.draw.circle(screen, fl_color2, (1395, 105), 100, 0)
pygame.display.flip()
counter = 1
clock.tick(speed)
pygame.display.flip()
pygame.display.update()
pygame_widgets.update(event)
screen.fill(background)
immagine.draw()
if buttonstart.draw() == True:
speed = 1
print("Click su START")
if buttonstop.draw() == True:
speed = 100
print("Click su STOP")
if buttonvel1.draw() == True:
speed = 0.2
print("Click su 1")
if buttonvel2.draw() == True:
speed = 0.4
print("Click su 2")
if buttonvel3.draw() == True:
speed = 0.6
print("Click su 3")
if buttonvel4.draw() == True:
speed = 0.8
print("Click su 4")
if buttonvel5.draw() == True:
speed = 1
print("Click su 5")
if buttonvel6.draw() == True:
speed = 1.2
print("Click su 6")
if buttonvel7.draw() == True:
speed = 1.4
print("Click su 7")
if buttonvel8.draw() == True:
speed = 1.6
print("Click su 8")
if buttonvel9.draw() == True:
speed = 1.8
print("Click su 9")
if buttonvel0.draw() == True:
speed = 0.1
print("Click su 0")
uj5u.com熱心網友回復:
您可以通過兩種方式解決您的問題。
與其像你現在做的那樣逐步增加你的價值,不如創建一個計時器并將你的球的位置硬設定到右側或左側的位置。
你可以有一個 bool 全域變數
show_ball,你只畫你的球if show_ball:。您現在可以在隱藏時移動您的球。
如果你選擇選項一。定義您的左右炮臺:
RIGHT_EMPLACEMENT = (25, 25)
LEFT_EMPLACEMENT = (475, 25)
創建一個 blinking_speed 變數:
blinking_speed = 50
然后創建一個自定義事件和計時器以配合它(如果您不希望球首先移動,請將其設定為 0):
BLINK = pg.USEREVENT
pg.time.set_timer(BLINK, blinking_speed) # Or 0 if you wish
然后簡單地捕捉你的 BLINK 事件:
for evt in pg.event.get():
if evt.type == BLINK:
blink()
最后,您可以在 blink 內管理您的邏輯:
def blink():
if ball_pos == right_emplacement:
ball_pos = left_emplacement
else:
ball_pos = right_emplacement
您的按鈕可以重新定義您的計時器(它將覆寫以前的計時器):
blinking_speed = some_value
pg.time.set_timer(BLINK, blinking_speed)
如果您選擇第二個選項,則只需創建一個 ball_speed var :
ball_speed = 50
然后你的球可以從direction*ball_speed
方向移動 1 或 -1 取決于你是向右還是向左
其他要考慮的要點:您的繪圖功能應該只負責繪制按鈕,而不是其他任何事情。
見:SRP
將所有按鈕存盤在串列中以簡單/縮短您的代碼
buttons = []
快樂編程
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/514369.html
標籤:表现循环按钮游戏备用
上一篇:根據串列值從dict中檢索
