Python-如何使用不同的方法填充pygame屏幕
我试图在tkinter表单中为使用pygame的程序设置一个重置按钮 重置按钮必须重置pygame屏幕,这意味着填充一种颜色,在我的情况下是黑色 我将不显示完整的代码,因为它很长,而是片段Python-如何使用不同的方法填充pygame屏幕,python,tkinter,pygame,Python,Tkinter,Pygame,我试图在tkinter表单中为使用pygame的程序设置一个重置按钮 重置按钮必须重置pygame屏幕,这意味着填充一种颜色,在我的情况下是黑色 我将不显示完整的代码,因为它很长,而是片段 btn_reset = Button(simFrame) btn_reset.config(text = "Reset", borderwidth = 2, padx = 0, pady=0, width = 8) btn_reset.bind("<Button-1>", self
btn_reset = Button(simFrame)
btn_reset.config(text = "Reset", borderwidth = 2, padx = 0, pady=0, width = 8)
btn_reset.bind("<Button-1>", self.reset) #left click to acvtivate
btn_reset.grid(row = 8, column = 0, sticky = W)
这就是我想做的,显然这不起作用
屏幕已经用另一种方法制作,如何从这个方法填充
或者更好,我可以在激活按钮时从pygame方法中填充屏幕吗
编辑:
def显示(自):
#将变量从一个方法移动到另一个方法
megatonVar=self.megatonVar
locationVar=self.locationVar
populationVar=self.populationVar
#重置圆的宽度
locCircleWidth=2
fireCircleWidth=2
air20CircleWidth=2
radCircleWidth=2
air5CircleWidth=2
thermCircleWidth=2
constantK=(float(megatonVar))**(1/3)#创建一个可供以后使用的常量变量
#爆炸半径方程式是否使用兆吨输入计算爆炸半径
#R=K x立方英尺(Y)
#R=半径K=常数Y=屈服
locCircleRadius=(locationVar)/1
fireCircleRadius=0.0912*(康斯坦特克)
radCircleRadius=0.3683*(康斯坦特克)
thermCircleRadius=0.9910*(康斯坦特克)
air20CircleRadius=0.2182*(康斯坦特克)
air5CircleRadius=0.4545*(康斯坦特克)
#计算受影响人口
#受爆炸半径影响的人口
如果(air5CircleRadius/locCircleRadius)>=1:
self.popBlastValue.set(“%.2f”%(populationVar))
其他:
self.popBlastValue.set(“%.2f”%((air5CircleRadius/locCircleRadius)*(populationVar)))
#受辐射半径影响的人口
如果(耐热/耐寒)>=1:
self.popRadValue.set(“%.2f”%(populationVar))
其他:
self.popRadValue.set(“%.2f”%((thermCircleRadius/locCircleRadius)*(populationVar)))
pygame.init()初始化pygame
screen=pygame.display.set_模式((800900))#创建pygame显示(x,y)
屏幕。填充(黑色)#清除屏幕
#根据屏幕比例更新显示屏幕比例的文本
如果(Locircleradius>=20)或(thermCircleRadius>=20):
屏幕比例=“60公里”
elif(Locircleradius>10和Locircleradius<20)或(thermCircleRadius>10和thermCircleRadius<20):
屏幕比例=“40公里”
其他:
屏幕比例=“20公里”
#绘制比例线
pygame.draw.rect(屏幕,白色,(0,35800,2))#x,y,w,h
#根据大小向比例添加增量
如果(Locircleradius>=20)或(thermCircleRadius>=20):
pygame.draw.rect(屏幕,白色,(66,35,2,10))
pygame.draw.rect(屏幕,白色,(133,35,2,10))
pygame.draw.rect(屏幕,白色,(201,35,2,10))
pygame.draw.rect(屏幕,白色,(268,35,2,10))
pygame.draw.rect(屏幕,白色,(334,35,2,10))
pygame.draw.rect(屏幕,白色,(400,35,2,10))
pygame.draw.rect(屏幕,白色,(467,35,2,10))
pygame.draw.rect(屏幕,白色,(534,35,2,10))
pygame.draw.rect(屏幕,白色,(601,35,2,10))
pygame.draw.rect(屏幕,白色,(667,35,2,10))
pygame.draw.rect(屏幕,白色,(734,35,2,10))
elif(Locircleradius>10和Locircleradius<20)或(thermCircleRadius>10和thermCircleRadius<20):
pygame.draw.rect(屏幕,白色,(100,35,2,10))
pygame.draw.rect(屏幕,白色,(200,35,2,10))
pygame.draw.rect(屏幕,白色,(300,35,2,10))
pygame.draw.rect(屏幕,白色,(400,35,2,10))
pygame.draw.rect(屏幕,白色,(500,35,2,10))
pygame.draw.rect(屏幕,白色,(600,35,2,10))
pygame.draw.rect(屏幕,白色,(700,35,2,10))
其他:
pygame.draw.rect(屏幕,白色,(200,35,2,10))
pygame.draw.rect(屏幕,白色,(400,35,2,10))
pygame.draw.rect(屏幕,白色,(600,35,2,10))
#用半径缩放屏幕
如果(Locircleradius>=20)或(thermCircleRadius>=20):
Locircleradius*=10
fireCircleRadius*=10
radCircleRadius*=10
耐热*=10
air20CircleRadius*=10
air5CircleRadius*=10
elif(Locircleradius>10和Locircleradius<20)或(thermCircleRadius>10和thermCircleRadius<20):
Locircleradius*=20
fireCircleRadius*=20
radCircleRadius*=20
耐热*=20
air20CircleRadius*=20
air5CircleRadius*=20
其他:
Locircleradius*=40
fireCircleRadius*=40
radCircleRadius*=40
耐高温*=40
air20CircleRadius*=40
air5CircleRadius*=40
#如果圆半径小于宽度,则pygame会给出一个错误,
#因此,如果半径小于宽度,则宽度必须小于半径
如果Loccirleradiusdef reset(self, event):
screen.fill(black)
pygame.display.update()
def display(self):
#moving variables from method to method
megatonVar = self.megatonVar
locationVar = self.locationVar
populationVar = self.populationVar
#resets circle widths
locCircleWidth = 2
fireCircleWidth = 2
air20CircleWidth = 2
radCircleWidth = 2
air5CircleWidth = 2
thermCircleWidth = 2
constantK = (float(megatonVar)) ** (1/3) #creates a constant variable that can be used later
#does blast radius equations using the megaton input to calculate the blast radius'
#R = K x cuberoot(Y)
#R = radius K = constant Y = yield
locCircleRadius = (locationVar) / 1
fireCircleRadius = 0.0912 * (constantK)
radCircleRadius = 0.3683 * (constantK)
thermCircleRadius = 0.9910 * (constantK)
air20CircleRadius = 0.2182 * (constantK)
air5CircleRadius = 0.4545 * (constantK)
#calculate population affected
#population affected by blast radius
if (air5CircleRadius / locCircleRadius) >= 1:
self.popBlastValue.set("%.2f" %(populationVar))
else:
self.popBlastValue.set("%.2f" %((air5CircleRadius / locCircleRadius)*(populationVar)))
#population affected by radiation radius
if (thermCircleRadius / locCircleRadius) >= 1:
self.popRadValue.set("%.2f" %(populationVar))
else:
self.popRadValue.set("%.2f" %((thermCircleRadius / locCircleRadius)*(populationVar)))
pygame.init() #initialises pygame
screen = pygame.display.set_mode((800,900)) #creates pygame display (x, y)
screen.fill(black) #clears screen
#updates the text displaying the screen scale depending on scale
if (locCircleRadius >= 20) or (thermCircleRadius >= 20):
screenScale = "60km"
elif (locCircleRadius > 10 and locCircleRadius < 20) or (thermCircleRadius > 10 and thermCircleRadius < 20):
screenScale = "40km"
else:
screenScale = "20km"
#draws scale line
pygame.draw.rect(screen, white, (0, 35, 800, 2)) #x, y, w, h
#adds increments to scale depending on size
if (locCircleRadius >= 20) or (thermCircleRadius >= 20):
pygame.draw.rect(screen, white, (66, 35, 2, 10))
pygame.draw.rect(screen, white, (133, 35, 2, 10))
pygame.draw.rect(screen, white, (201, 35, 2, 10))
pygame.draw.rect(screen, white, (268, 35, 2, 10))
pygame.draw.rect(screen, white, (334, 35, 2, 10))
pygame.draw.rect(screen, white, (400, 35, 2, 10))
pygame.draw.rect(screen, white, (467, 35, 2, 10))
pygame.draw.rect(screen, white, (534, 35, 2, 10))
pygame.draw.rect(screen, white, (601, 35, 2, 10))
pygame.draw.rect(screen, white, (667, 35, 2, 10))
pygame.draw.rect(screen, white, (734, 35, 2, 10))
elif (locCircleRadius > 10 and locCircleRadius < 20) or (thermCircleRadius > 10 and thermCircleRadius < 20):
pygame.draw.rect(screen, white, (100, 35, 2, 10))
pygame.draw.rect(screen, white, (200, 35, 2, 10))
pygame.draw.rect(screen, white, (300, 35, 2, 10))
pygame.draw.rect(screen, white, (400, 35, 2, 10))
pygame.draw.rect(screen, white, (500, 35, 2, 10))
pygame.draw.rect(screen, white, (600, 35, 2, 10))
pygame.draw.rect(screen, white, (700, 35, 2, 10))
else:
pygame.draw.rect(screen, white, (200, 35, 2, 10))
pygame.draw.rect(screen, white, (400, 35, 2, 10))
pygame.draw.rect(screen, white, (600, 35, 2, 10))
#scales the screen with radius
if (locCircleRadius >= 20) or (thermCircleRadius >= 20):
locCircleRadius *= 10
fireCircleRadius *= 10
radCircleRadius *= 10
thermCircleRadius *= 10
air20CircleRadius *= 10
air5CircleRadius *= 10
elif (locCircleRadius > 10 and locCircleRadius < 20) or (thermCircleRadius > 10 and thermCircleRadius < 20):
locCircleRadius *= 20
fireCircleRadius *= 20
radCircleRadius *= 20
thermCircleRadius *= 20
air20CircleRadius *= 20
air5CircleRadius *= 20
else:
locCircleRadius *= 40
fireCircleRadius *= 40
radCircleRadius *= 40
thermCircleRadius *= 40
air20CircleRadius *= 40
air5CircleRadius *= 40
#if the circle radius is smaller than the width pygame gives an error,
#so the width must become lower than the radius if radius is lower than width
if locCircleRadius < locCircleWidth:
locCircleWidth = (locCircleRadius - 0.1)
if fireCircleRadius < fireCircleWidth:
fireCircleWidth = (fireCircleRadius - 0.1)
if air20CircleRadius < air20CircleWidth:
air20CircleWidth = (air20CircleRadius - 0.1)
if radCircleRadius < radCircleWidth:
radCircleWidth = (radCircleRadius - 0.1)
if air5CircleRadius < air5CircleWidth:
air5CircleWidth = (air5CircleRadius - 0.1)
if thermCircleRadius < thermCircleWidth:
thermCircleWidth = (thermCircleRadius - 0.1)
#draws the circles on the pygame display. (colour, (x,y midpoint), radius, width)
pygame.draw.circle(screen, white, (400,500), int(locCircleRadius), int(locCircleWidth)) #location - white
pygame.draw.circle(screen, orange, (400,500), int(fireCircleRadius), int(fireCircleWidth)) #fireball - orange
pygame.draw.circle(screen, lightGreen, (400,500), int(radCircleRadius), int(radCircleWidth)) #radiation - light green
pygame.draw.circle(screen, lightOrange, (400,500), int(thermCircleRadius), int(thermCircleWidth)) #thermalradiation - light orange
pygame.draw.circle(screen, red, (400,500), int(air20CircleRadius), int(air20CircleWidth)) #airblast20psi - red
pygame.draw.circle(screen, grey, (400,500), int(air5CircleRadius), int(air5CircleWidth)) #airblast5psi - grey
#initialize font; must be called after 'pygame.init()' to avoid 'Font not Initialized' error
myfont = pygame.font.SysFont("courier", 20)
#render text
widthLabel = myfont.render(("Screen width = {}".format(screenScale)), 1, white)
screen.blit(widthLabel, (10, 10))
scaleLabel = myfont.render(("Scale increment = 5km"), 1, white)
screen.blit(scaleLabel, (540, 10))
#updates the display so the circles are shown
pygame.display.update()
#button - reset
btn_reset = Button(simFrame)
btn_reset.config(text = "Reset", borderwidth = 2, padx = 0, pady=0, width = 8)
btn_reset.bind("<Button-1>", self.reset) #left click to acvtivate
btn_reset.grid(row = 8, column = 0, sticky = W)
def display(self):
pygame.init() #initialises pygame
screen = pygame.display.set_mode((800,900)) #creates pygame display (x, y)
self.screen = screen
screen.fill(black) #clears screen
def reset(self, event):
(self.screen).fill(black)
pygame.display.update()