【发布时间】:2021-08-03 20:47:45
【问题描述】:
程序正常启动并显示带有图像的窗口,当我单击它时使用内部创建函数(代码部分旁边有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)
在类函数 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")
然后是具有类函数的相同代码,当我单击它时不会移动图像:
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)
还有具有类函数的类(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)
我的问题是我不知道如何使类函数起作用;我知道为什么当我点击它时类函数不移动图像。
【问题讨论】:
标签: python pygame python-class