Pygame Python : 功能显示移动但与 class 功能一起使用时不显示
Pygame Python : fonction display movement but don't display when used with class function
程序正常启动并显示 window 和图像,当我用里面的函数创建时点击它移动(代码部分在它旁边有 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 函数 (Units_sol_1) 的 class :
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 函数有效;不知道为什么 class 函数在我点击它时不移动图像。
您必须在应用程序循环之前创建 Recon
的实例。但是,您必须在应用程序循环中连续绘制对象。
此外,您必须在移动对象时更改位置属性 self.pos[0]
和 self.pos[1]
。
pygame.event.get()
获取所有消息并将它们从队列中删除。请参阅文档:
This will get all the messages and remove them from the queue. [...]
如果在多个事件循环中调用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()
程序正常启动并显示 window 和图像,当我用里面的函数创建时点击它移动(代码部分在它旁边有 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 函数 (Units_sol_1) 的 class :
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 函数有效;不知道为什么 class 函数在我点击它时不移动图像。
您必须在应用程序循环之前创建 Recon
的实例。但是,您必须在应用程序循环中连续绘制对象。
此外,您必须在移动对象时更改位置属性 self.pos[0]
和 self.pos[1]
。
pygame.event.get()
获取所有消息并将它们从队列中删除。请参阅文档:
This will get all the messages and remove them from the queue. [...]
如果在多个事件循环中调用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()