gpt4 book ai didi

python - 打字机效果 Pygame

转载 作者:行者123 更新时间:2023-12-05 01:45:38 32 4
gpt4 key购买 nike

这个问题真的很难问,但我知道 Stack Overflow 的你们是最聪明的人。

我完全不明白为什么会出现这个问题(我相当熟悉 Python 和 Pygame,所以任何关于如何改进代码的建议都会随着我对提高技能的热爱而收到)。

我正在创建的内容:这真的是一个噱头项目,我有一个 2.5"的小屏幕 (PiTFT) 连接到 Raspberry Pi,代码正在创建一个打字机效果,在编写文本时光标会在文本前面移动。

挑战 1 是每次在 pygame 中移动 Sprite 时,都必须重新绘制所有内容,否则你会看到一条轨迹,由于光标在文本前面移动,结果将如下所示:

enter image description here

我设法通过黑屏/清屏解决了这个问题。但后来我丢失了所有以前写的信件。所以我创建了一个列表(整个单词),其中填充了所有以前编写的字符。每次循环时我都使用这个列表来重绘所有以前写的文本。所以现在:

Sample of issue

如您所见,文字看起来很有趣。它应该是这样的:

[i] 正在初始化 ...

[i] 进入幽灵模式... []

为了达到这一点,我已经花费了数小时的时间 - 代码几乎可以完美运行!魔法发生在函数 print_screen() 中,但是我的代码中的什么导致文本最后包含另一行的字母? :>

非常感谢您的帮助<3

完整代码如下:

import pygame
import time
import os
import sys
from time import sleep
from pygame.locals import *

positionx = 10
positiony = 10
entireword = []
entireword_pos = 10
counter = 0
entire_newline = False


#Sets the width and height of the screen
WIDTH = 320
HEIGHT = 240
speed = 0.05

#Importing the external screen
os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')

#Initializes the screen - Careful: all pygame commands must come after the init
pygame.init()

#Sets mouse cursor visibility
pygame.mouse.set_visible(False)
#Sets the screen note: must be after pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# initialize font; must be called after 'pygame.init()' to avoid 'Font not Initialized' error
myfont = pygame.font.SysFont("monospace", 18)

#Class

class cursors(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 20))
self.image.fill((0,255,0))
self.rect = self.image.get_rect()
self.rect.center = (positionx + 10, positiony + 10)

def update(self):
self.rect.x = positionx + 10
self.rect.y = positiony

#Functions

#Prints to the screen
def print_screen(words, speed):
rel_speed = speed
for char in words:
#speed of writing
if char == ".":
sleep(0.3)
else:
sleep(rel_speed)

#re-renders previous written letters
global entireword

# Old Typewriter functionality - Changes position of cursor and text a newline

#Makes sure the previous letters are rendered and not lost
#xx is a delimter so the program can see when to make a newline and ofcourse ignore writing the delimiter
entireword.append(char)
if counter > 0:
loopcount = 1
linecount = 0 # This is to which line we are on
for prev in entireword:
if prev == 'xx':
global linecount
global positiony
global loopcount
linecount = linecount + 1
positiony = 17 * linecount
loopcount = 1
if prev != 'xx': #ignore writing the delimiter
pchar = myfont.render(prev, 1, (255,255,0))
screen.blit(pchar, (loopcount * 10, positiony))
loopcount = loopcount + 1

if char != 'xx':
# render text
letter = myfont.render(char, 1, (255,255,0))
#blits the latest letter to the screen
screen.blit(letter, (positionx, positiony))

# Appends xx as a delimiter to indicate a new line
if entire_newline == True:
entireword.append('xx')
global entire_newline
entire_newline = False

global positionx
positionx = positionx + 10
all_sprites.update()
all_sprites.draw(screen)
pygame.display.flip()
screen.fill((0,0,0)) # blackens / clears the screen

global counter
counter = counter + 1

#Positions cursor at new line
def newline():
global positionx
global positiony
positionx = 10
positiony = positiony + 17

all_sprites = pygame.sprite.Group()
cursor = cursors()
all_sprites.add(cursor)

#Main loop
running = True
while running:
global speed
global entire_newline

words = "[i] Initializing ..."
entire_newline = True
newline()
print_screen(words,speed)

words = "[i] Entering ghost mode ..."
entire_newline = True
newline()
print_screen(words,speed)

#Stops the endless loop if False
running = False
sleep(10)

最佳答案

很抱歉,如果我没有直接回答你的问题,因为你的代码现在对我来说太困惑了,所以我冒昧地重写了你的代码来完成你想要的。

想法是有两个 Sprite :

  • 光标,a) 显示在屏幕上,b) 跟踪要写的文本和位置

  • 板,它基本上只是一个呈现文本的表面

注意所有的编写逻辑是如何在 Cursor 类上进行的,我们有一个漂亮、简单且愚蠢的主循环。

import pygame
import os

#Sets the width and height of the screen
WIDTH = 320
HEIGHT = 240

#Importing the external screen
os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')

#Initializes the screen - Careful: all pygame commands must come after the init
pygame.init()
clock = pygame.time.Clock()

#Sets mouse cursor visibility
pygame.mouse.set_visible(False)
#Sets the screen note: must be after pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))


class Board(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((WIDTH, HEIGHT))
self.image.fill((13,13,13))
self.image.set_colorkey((13,13,13))
self.rect = self.image.get_rect()
self.font = pygame.font.SysFont("monospace", 18)

def add(self, letter, pos):
s = self.font.render(letter, 1, (255, 255, 0))
self.image.blit(s, pos)

class Cursor(pygame.sprite.Sprite):
def __init__(self, board):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 20))
self.image.fill((0,255,0))
self.text_height = 17
self.text_width = 10
self.rect = self.image.get_rect(topleft=(self.text_width, self.text_height))
self.board = board
self.text = ''
self.cooldown = 0
self.cooldowns = {'.': 12,
'[': 18,
']': 18,
' ': 5,
'\n': 30}

def write(self, text):
self.text = list(text)

def update(self):
if not self.cooldown and self.text:
letter = self.text.pop(0)
if letter == '\n':
self.rect.move_ip((0, self.text_height))
self.rect.x = self.text_width
else:
self.board.add(letter, self.rect.topleft)
self.rect.move_ip((self.text_width, 0))
self.cooldown = self.cooldowns.get(letter, 8)

if self.cooldown:
self.cooldown -= 1

all_sprites = pygame.sprite.Group()
board = Board()
cursor = Cursor(board)
all_sprites.add(cursor, board)

text = """[i] Initializing ...
[i] Entering ghost mode ...

done ...

"""

cursor.write(text)

#Main loop
running = True
while running:

for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False

all_sprites.update()
screen.fill((0, 0, 0))
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(60)

enter image description here

关于python - 打字机效果 Pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41101662/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com