栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

根据pygame中的点击更改图片

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

根据pygame中的点击更改图片

在这里,我进行了简单介绍,以展示鼠标的工作原理。该行

if event.button ==1:
检查是否已按下鼠标左键,如果要使用鼠标右键,请将1更改为2。

import pygame, sysfrom pygame.locals import *TIMER = 30SCREEN_X = 200SCREEN_Y = 200screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y))clock = pygame.time.Clock() #tick-tockending = button1 = button2 = Falsecorner1 = (28,18)  #Top Left corner of button 1corner2 = (56,18)  #Top Left corner of button 2image_length = 100 #length of the buttonsimage_height = 100 #height of the buttonscounter = 0#Main Loop:while ending==False:    counter+=1    clock.tick(TIMER)    for event in pygame.event.get():        if event.type == KEYDOWN: if event.key == K_ESCAPE:     ending=True # Time to leave     print("Game Stopped Early by user")        elif event.type == MOUSEBUTTONDOWN: if event.button == 1:     mouse_x, mouse_y = event.pos     if (mouse_x >= corner1[0]) and (mouse_x <= corner1[0]+image_length) and (mouse_y >= corner1[1]) and (mouse_y <= corner1[1]+image_height):         print ("Button one is selected")         button1=True         button2=False     elif (mouse_x >= corner2[0]) and (mouse_x <= corner2[0]+image_length) and (mouse_y >= corner2[1]) and (mouse_y <= corner2[1]+image_height):         print ("Button two is selected")         button1=False         button2=True     else:         print ("That's not a button")         button1=False         button2=False    if counter == TIMER:  #prints the statements once a second        counter=0        if button1==True: print ("Button one is currently selected")        elif button2==True: print ("Button two is currently selected")        else: print ("No buttons currently selected")

在底部的打印语句中。如果button1或button2变量分别为,则只需将选择的图像用于按钮1或2

True
。否则将是如果未选择任何图像,则将两个图像都作为未选择的按钮。如果您不知道如何使用图像等,请在此处查看:http
:
//www.pygame.org/docs/确实对我有所帮助。自己尝试一下,如果仍然遇到问题,Stack
Exchange仍然会在这里提出您的问题:)

希望能帮助到你



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/626618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号