鱼C论坛

 找回密码
 立即注册
查看: 1645|回复: 4

小甲鱼pygame81讲中退出全屏BUG,求解

[复制链接]
发表于 2022-5-21 22:40:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import pygame
import sys
from pygame.locals import *

pygame.init()

listmode = pygame.display.list_modes()

size = width,height = 600,400

speed = [-2,1]

bg = (255,255,255) 

fullscreen = False


screen = pygame.display.set_mode(size,RESIZABLE)

pygame.display.set_caption('word game')

turtle = pygame.image.load('btn_tili.png')

position = turtle.get_rect()

l_head = turtle
r_head = pygame.transform.flip(turtle,True,False)

while True:
    
    for event in pygame.event.get():
        
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                turtle = l_head
                speed = [-1,0]

            if event.key == K_RIGHT:
                turtle = r_head
                speed = [1,0]
            if event.key == K_UP:
                speed = [0,-1]
            if event.key == K_DOWN:
                speed = [0,1]
            
            #全屏(F11)
                if event.key == K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    screen = pygame.display.set_mode((listmode[0]),FULLSCREEN|HWSURFACE)
                    width,height = listmode[0]
                    
                else:
                    screen = pygame.display.set_mode(size)
                    

        if event.type == VIDEORESIZE:
            size == event.size
            width,height = size
            screen = pygame.display.set_mode(size,RESIZABLE)
            
    position = position.move(speed)

    if position.left < 0 or position.right > width:

        turtle = pygame.transform.flip(turtle,True,False)

        speed[0] = -speed[0]

    if position.top < 0 or position.bottom > height:

        speed[1] = -speed[1]

    screen.fill(bg)

    screen.blit(turtle,position)

    pygame.display.flip()

    pygame.time.delay(10)


标红的为全屏的代码

当乌龟跑超过了 width,height = 600,400;再退出全屏游戏就会报错,请问如何优化这个BUG?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-5-22 10:51:39 | 显示全部楼层
有点忘了, 应该可以检测点击全屏和退出全屏的按钮, 提供一个思路, 我记得是可行的
if 全屏按钮:
    if 全屏状态:
        重置小乌龟位置
        重置游戏框大小
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-23 09:54:33 | 显示全部楼层
白two 发表于 2022-5-22 10:51
有点忘了, 应该可以检测点击全屏和退出全屏的按钮, 提供一个思路, 我记得是可行的

这个逻辑我知道,但是写了不行,不知道为什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-23 12:13:50 | 显示全部楼层
这是我以前写的, 你参考一下
import pygame
import sys
from pygame.constants import *


def change_pos(position, width, height):
    if position.right > width:
        position.right = width
                    
    if position.bottom > height:
        position.bottom = height

pygame.init()
# size = width, height = (600, 400)
size = width, height = (1536, 864)
speed = [5, 0]
bg = (255, 255, 255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size, RESIZABLE)
pygame.display.set_caption("初次见面,请多多关照!")
# post the turtle
turtle = pygame.image.load(r"D:\user\Desktop\pyPractice\game\turtle\turtle.png").convert_alpha()
position = turtle.get_rect()

for i in range(position.width):
    for j in range(position.height):
        at = turtle.get_at((i, j))
        if at[0] > 100 and at[2] > 200 and at[1] > 100:
            at[3] = 0
        turtle.set_at((i, j),at)

turtle_right = pygame.transform.rotate(turtle, 90)
turtle_top = pygame.transform.rotate(turtle, 90*2)
turtle_left = pygame.transform.rotate(turtle, 90*3)
turtle_bottom = turtle
turtle = turtle_top
fullscreen = False
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        
        if event.type == VIDEORESIZE:
            if (event.size[0] < 200):
                event.size = (200, event.size[1])
                size = event.size
                width, height = size
                screen = pygame.display.set_mode(size, RESIZABLE)
                change_pos(position, width, height)
            
            if (event.size[1] < 200):
                event.size = (event.size[0], 200)
                size = event.size
                width, height = size
                screen = pygame.display.set_mode(size, RESIZABLE)
                change_pos(position, width, height)
            else:    
                size = event.size
                width, height = size
                screen = pygame.display.set_mode(size, RESIZABLE)
                change_pos(position, width, height)

        if event.type == KEYDOWN:    
            if event.key == pygame.K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    # size = width, height = pygame.display.list_modes()[0]
                    size = width, height = (1536, 864)
                    screen = pygame.display.set_mode(size, pygame.HWSURFACE)

                else:
                    size = width, height = (600, 400)
                    screen = pygame.display.set_mode(size)
                    change_pos(position, width, height)

    position = position.move(speed)
    if position.right > width:
        turtle = turtle_right
        position = turtle_rect = turtle.get_rect()
        position.right = width
        speed = [0, 5]

    if position.bottom > height:
        turtle = turtle_bottom
        position = turtle_rect = turtle.get_rect()
        position.right = width
        position.bottom = height
        speed = [-5, 0]
    
    if position.left < 0:
        turtle = turtle_left
        position = turtle_rect = turtle.get_rect()
        position.bottom = height
        speed = [0, -5]
    
    if position.top < 0:
        turtle = turtle_top
        position = turtle_rect = turtle.get_rect()
        speed = [5, 0]

    screen.fill(bg)
    screen.blit(turtle, position)
    pygame.display.flip()
    pygame.time.delay(10)
    clock.tick(200)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-23 12:50:01 | 显示全部楼层
白two 发表于 2022-5-23 12:13
这是我以前写的, 你参考一下
import pygame
import sys
from pygame.locals import *
from pygame.constants import *

pygame.init()



size = width,height  = 600,400

speed = [1,1]

bg = (255,255,255) #RGB颜色

fullscreen = False


screen = pygame.display.set_mode(size,RESIZABLE)

pygame.display.set_caption('word game')

turtle = pygame.image.load('btn_tili.png').convert_alpha()

position = turtle.get_rect()

l_head = turtle
r_head = pygame.transform.flip(turtle,True,False)

def change_pos(position, width, height):
    if position.right > width:
        position.right = width
                    
    if position.bottom > height:
        position.bottom = height

while True:
    
    
    for event in pygame.event.get():
        
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                turtle = l_head
                speed = [-1,0]

            if event.key == K_RIGHT:
                turtle = r_head
                speed = [1,0]
            if event.key == K_UP:
                speed = [0,-1]
            if event.key == K_DOWN:
                speed = [0,1]
            
            #全屏(F11)
            if event.key == K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    listmode = pygame.display.list_modes()
                    width, height =  listmode[0]
                    screen = pygame.display.set_mode((listmode[0]),pygame.FULLSCREEN)
                    
                else:
                    size = width, height = (600, 400)
                    screen = pygame.display.set_mode(size)
                    change_pos(position, width, height)
                    

        if event.type == VIDEORESIZE:
            size == event.size
            width,height = size
            screen = pygame.display.set_mode(size,RESIZABLE)
            
    position = position.move(speed)

    if position.left < 0 or position.right > width:

        turtle = pygame.transform.flip(turtle,True,False)

        speed[0] = -speed[0]

    if position.top < 0 or position.bottom > height:

        speed[1] = -speed[1]

    screen.fill(bg)

    screen.blit(turtle,position)

    pygame.display.flip()

    pygame.time.delay(10)

        

按照你这个调了缩小可以了,但是还有一个BUG F11,只可以按一次,退出后第二次按就不可以全屏,这个是什么原因?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-8 00:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表