[英]Pygame Python : fonction display movement but don't display when used with class function
程序正常啟動並顯示 window 以及當我單擊它時移動的圖像,並在內部創建 function(代碼部分旁邊有 2 條注釋):
import Units_sol_1
import pygame
import sys
pygame.init()
clock = pygame.time.Clock()
resolution = (1080, 720)
pygame.display.set_caption("AWO")
surface = pygame.display.set_mode((1080, 720))
recon1_x = 0
recon1_y = 0
running = True
while running:
for event in pygame.event.get():
surface.fill((0, 0, 0))
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
recon1 = Units_sol_1.Recon(recon1_x, recon1_y)
pygame.display.update()
surface.blit(recon1.icon, recon1.pos)
#this is the code below
if recon1.pos <= pygame.mouse.get_pos() <= (recon1_x + recon1.w_icon, recon1_y + recon1.h_icon):
print("work1")
if event.type == pygame.MOUSEBUTTONDOWN:
recon1_x += 1
recon1_y += 1
recon1.w_icon += 1
recon1.h_icon += 1
print(recon1.pos)
#this is the code upside
pygame.display.flip()
clock.tick(60)
在 class 功能 Recon (Units_sol_1) 下方:
import pygame
class Recon(Vehicule1, pygame.sprite.Sprite):
def __init__(self, posx, posy):
super().__init__(posx, posy)
self.pos = (posx, posy)
self.w_icon = 33
self.h_icon = 33
self.icon = pygame.image.load("Unité/recon_l.png")
然后與 class 功能相同的代碼,當我單擊它時不會移動圖像:
import Units_sol_1
import pygame
import sys
pygame.init()
clock = pygame.time.Clock()
resolution = (1080, 720)
pygame.display.set_caption("AWO")
surface = pygame.display.set_mode((1080, 720))
recon1_x = 0
recon1_y = 0
running = True
while running:
for event in pygame.event.get():
surface.fill((0, 0, 0))
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
recon1 = Units_sol_1.Recon(recon1_x, recon1_y)
pygame.display.update()
surface.blit(recon1.icon, recon1.pos)
recon1.icon_moving(recon1_x, recon_y)
pygame.display.flip()
horloge.tick(60)
以及具有 class 功能的 class (Units_sol_1):
import pygame
class Recon(Vehicule1, pygame.sprite.Sprite):
def __init__(self, posx, posy):
self.pos = (posx, posy)
self.w_icon = 33
self.h_icon = 33
self.icon = pygame.image.load("Unité/recon_l.png")
def icon_moving(self, posx, posy):
for event in pygame.event.get():
if self.pos <= pygame.mouse.get_pos() <= (posx + self.w_icon, posy + self.h_icon):
print("ok 1")
if event.type == pygame.MOUSEBUTTONDOWN:
print("ok 2")
posx += 1
posy += 1
self.w_icon += 1
self.h_icon += 1
print(self.pos)
我的問題是我不知道如何使 class function 起作用; 我知道為什么 class function 在我單擊它時不移動圖像。
您必須在應用程序循環之前創建Recon
的實例。 但是,您必須在應用程序循環中連續繪制 object。 此外,當您移動 object 時,您必須更改 position 屬性self.pos[0]
和self.pos[1]
。
pygame.event.get()
獲取所有消息並將它們從隊列中刪除。 請參閱文檔:
這將獲取所有消息並將它們從隊列中刪除。 [...]
如果在多個事件循環中調用pygame.event.get()
,則只有一個循環接收事件,但並非所有循環都接收所有事件。 結果,似乎錯過了一些事件。
每幀獲取一次事件並在多個循環中使用它們或將事件列表傳遞給處理它們的函數和方法:
class Recon(Vehicule1, pygame.sprite.Sprite):
def __init__(self, posx, posy):
self.pos = [posx, posy]
self.w_icon = 33
self.h_icon = 33
self.icon = pygame.image.load("Unité/recon_l.png")
def icon_moving(self, event_list):
for event in event_list:
if event.type == pygame.MOUSEBUTTONDOWN:
rect = pygame.Rect(*self.pos, self.w_icon, self.h_icon)
if rect.collidepoint(event.pos):
self.pos[0] += 1
self.pos[1] += 1
recon1 = Units_sol_1.Recon(recon1_x, recon1_y)
running = True
while running:
event_list = pygame.event.get()
for event in event_list:
surface.fill((0, 0, 0))
if event.type == pygame.QUIT:
running = False
recon1.icon_moving(event_list)
surface.fill(0)
surface.blit(recon1.icon, recon1.pos)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.