Python 为什么推杆文本会这样做?
我想做一个简单的平板游戏。一切都很顺利,但当我试图在这里输入文本时,发生了一些奇怪的事情。我真的不知道为什么会这样,因为我和以前的项目一样 这是printscreen:Python 为什么推杆文本会这样做?,python,text,pygame,Python,Text,Pygame,我想做一个简单的平板游戏。一切都很顺利,但当我试图在这里输入文本时,发生了一些奇怪的事情。我真的不知道为什么会这样,因为我和以前的项目一样 这是printscreen: 如果你知道为什么会这样,为什么还会这样,请告诉我 这是我的代码: from pygame import * WIN_WIDTH = 1923 WIN_HEIGHT = 1000 DISPLAY = (WIN_WIDTH, WIN_HEIGHT) DEPTH = 32 FLAGS = 0 CAMERA_SLACK = 30 py
如果你知道为什么会这样,为什么还会这样,请告诉我 这是我的代码:
from pygame import *
WIN_WIDTH = 1923
WIN_HEIGHT = 1000
DISPLAY = (WIN_WIDTH, WIN_HEIGHT)
DEPTH = 32
FLAGS = 0
CAMERA_SLACK = 30
pygame.init()
green = (0, 255, 0)
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
level = [
'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP',
'P P',
'P P',
'P PP P',
'P P',
'P PP P',
'P P',
'P P',
'P PP P',
'P P',
'P P',
'P P',
'P P',
'P PP P',
'P P',
'P P',
'P P',
'P P P',
'P PP P P',
'P P P',
'P P P',
'P P P',
'P PP P P',
'P P P',
'P P P',
'P PP P P',
'P P P',
'P PPP P PP',
'P P P',
'P P LP',
'PSSSSSSSSSSSSSSSSSSSSSSPPPSSSSSSSSSSSSSSSSSSSSSSSSSSPSSPPPPPPP',
]
def main():
pygame.display.set_caption('BlaBlabla!')
timer = pygame.time.Clock()
up = down = left = right = running = left_dash = right_dash = dashing = False
bg = Surface((WIN_WIDTH, WIN_HEIGHT // 30))
bg.convert()
bg.fill(Color('#000000'))
text = 'Score:'
font = pygame.font.SysFont('Consolas', 22)
bg.blit(font.render(text, True, (green)), (1, 1))
entities = pygame.sprite.Group()
platforms = []
killing_entities = []
another_level = []
blockade = []
player = Player(767, 900)
x = y = 0
global level
for row in level:
for col in row:
if col == 'P':
p = Platform(x, y)
platforms.append(p)
entities.add(p)
if col == 'E':
e = Block(x, y)
platforms.append(e)
entities.add(e)
if col == 'S':
s = Spike(x, y)
killing_entities.append(s)
entities.add(s)
if col == 'L':
l = Another_Level(x, y)
another_level.append(l)
entities.add(l)
x += 32
y += 32
x = 0
entities.add(player)
run = True
while run:
timer.tick(65)
for e in pygame.event.get():
if e.type == QUIT:
run = False
if e.type == KEYDOWN and e.key == K_SPACE:
up = True
if e.type == KEYDOWN and e.key == K_s:
down = True
if e.type == KEYDOWN and e.key == K_a:
left = True
if e.type == KEYDOWN and e.key == K_d:
right = True
if e.type == KEYDOWN and e.key == K_q:
running = True
if e.type == KEYUP and e.key == K_SPACE:
up = False
if e.type == KEYUP and e.key == K_s:
down = False
if e.type == KEYUP and e.key == K_d:
right = False
if e.type == KEYUP and e.key == K_a:
left = False
if e.type == KEYUP and e.key == K_d:
right = False
if pygame.sprite.spritecollideany(player, killing_entities):
main()
if pygame.sprite.spritecollideany(player, another_level):
level = [
'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP',
'P S S S S P',
'P S S S P',
'P PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP P',
'PP P P P SS SS S S P',
'PS P P S P S SS SS SS SS S S S S P',
'P P S P S P SS SS SS SS S S S S P',
'P P P P S SS SS S S P',
'P P PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPSSP',
'PP EEP',
'PS P',
'P P',
'P P',
'P P',
'PP P',
'PS P',
'P P',
'P P',
'P P',
'PPS P',
'PS P',
'P P',
'P P',
'P P',
'PP P',
'P P',
'P P',
'P P',
'P P',
'P P P',
'PSSSSSSSSSSSSSSSSSSSSSSPPPSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSPPPP',]
main()
for y in range(32):
for x in range(32):
screen.blit(bg, (x * 32, y * 32))
player.update(up, down, left, right, left_dash, right_dash, running, dashing, platforms)
entities.draw(screen)
pygame.display.flip()
pygame.display.update()
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Player(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.xvel = 0
self.yvel = 0
self.onGround = False
self.image = Surface((32,32))
self.image.fill(Color('#0000FF'))
self.image.convert()
self.rect = Rect(x, y, 32, 32)
def update(self, up, down, left, right, running, platforms):
if up:
if self.onGround: self.yvel -= 9
if down:
pass
if running:
self.xvel = 12
if left:
self.xvel = -8
if right:
self.xvel = 8
if not self.onGround:
self.yvel += 0.25
if self.yvel > 100: self.yvel = 100
if not(left or right):
self.xvel = 0
self.rect.left += self.xvel
self.collide(self.xvel, 0, platforms)
self.rect.top += self.yvel
self.onGround = False
self.collide(0, self.yvel, platforms)
def collide(self, xvel, yvel, platforms):
for p in platforms:
if pygame.sprite.collide_rect(self, p):
if isinstance(p, Block):
pygame.event.post(pygame.event.Event(QUIT))
if xvel > 0:
self.rect.right = p.rect.left
if xvel < 0:
self.rect.left = p.rect.right
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
if yvel < 0:
self.rect.top = p.rect.bottom
self.onGround = False
class Platform(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(Color('#DDDDDF'))
self.rect = Rect(x, y, 32, 32)
def update(self):
pass
class Spike(pygame.sprite.Sprite):
def __init__(self, x, y):
Platform.__init__(self, x, y)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(Color('#E70018'))
self.rect = Rect(x, y, 32, 32)
class Another_Level(pygame.sprite.Sprite):
def __init__(self, x, y):
Platform.__init__(self, x, y)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(Color('#8C563E'))
self.rect = Rect(x, y, 32, 32)
if __name__ == '__main__':
main()
pygame.quit()```
从pygame导入*
WIN_宽度=1923
WIN_高度=1000
显示=(窗口宽度、窗口高度)
深度=32
标志=0
摄像机的松弛度=30
pygame.init()
绿色=(0,255,0)
screen=pygame.display.set_模式(显示、标志、深度)
级别=[
“pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp,
“P”,
“P”,
"P PP P",,
“P”,
"P PP P",,
“P”,
“P”,
"P PP P",,
“P”,
“P”,
“P”,
“P”,
"P PP P",,
“P”,
“P”,
“P”,
"P",,
"P P P",,
"P",,
"P",,
"P",,
"P P P",,
"P",,
"P",,
"P P P",,
"P",,
"P PPP P",,
"P",,
“P LP”,
“PSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS,
]
def main():
pygame.display.set_标题('BlaBlabla!')
timer=pygame.time.Clock()
向上=向下=左=右=运行=左\u破折号=右\u破折号=破折号=错误
bg=表面((宽度,高度//30))
bg.convert()
背景填充(颜色('000000'))
text='分数:'
font=pygame.font.SysFont('Consolas',22)
blit(字体渲染(文本,真,(绿色)),(1,1))
entities=pygame.sprite.Group()
平台=[]
杀死实体=[]
另一个_级别=[]
封锁=[]
玩家=玩家(767900)
x=y=0
全球一级
对于级别中的行:
对于行中的列:
如果col==“P”:
p=平台(x,y)
平台。附加(p)
实体.添加(p)
如果col==“E”:
e=块(x,y)
平台。附加(e)
实体.添加(e)
如果col==“S”:
s=尖峰(x,y)
正在删除\u实体。追加
实体。添加
如果col=='L':
l=另一个_级别(x,y)
另一个_级别。追加(l)
实体.添加(l)
x+=32
y+=32
x=0
实体。添加(玩家)
运行=真
运行时:
计时器。滴答(65)
对于pygame.event.get()中的e:
如果e.type==退出:
运行=错误
如果e.type==KEYDOWN和e.key==K_空间:
向上=正确
如果e.type==KEYDOWN和e.key==K_s:
向下=真
如果e.type==KEYDOWN和e.key==K_a:
左=真
如果e.type==KEYDOWN和e.key==K\u d:
右=真
如果e.type==KEYDOWN和e.key==K_q:
运行=真
如果e.type==KEYUP和e.key==K_空间:
向上=错误
如果e.type==KEYUP和e.key==K_s:
向下=错误
如果e.type==KEYUP和e.key==K\u d:
右=假
如果e.type==KEYUP和e.key==K_a:
左=假
如果e.type==KEYUP和e.key==K\u d:
右=假
如果pygame.sprite.spriteany(玩家,杀死实体):
main()
如果pygame.sprite.SpriteAny(玩家,另一个等级):
级别=[
“pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp,
“P S P”,
“P S P”,
“pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp,
“PP P SS S S P”,
“PS PS S SS SS SS S P”,
“P S P S S S S P”,
“P P S SS S S P”,
“pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp,
“PP EEP”,
“PS P”,
“P”,
“P”,
“P”,
"PP",,
“PS P”,
“P
for y in range(32):
for x in range(32):
screen.blit(bg, (x * 32, y * 32))
bg = Surface(DISPLAY)
bg.convert()
bg.fill(Color('#000000'))
text = 'Score:'
font = pygame.font.SysFont('Consolas', 22)
bg.blit(font.render(text, True, (green)), (100, 100))
...
# in the main loop
screen.blit(bg, (0, 0))
bg = Surface((WIN_WIDTH, WIN_HEIGHT // 30))
bg.convert()
bg.fill(Color('#000000'))
text = 'Score:'
font = pygame.font.SysFont('Consolas', 22)
text_surf, text_rect = font.render(text, True, (green)), (1, 1)
entities = pygame.sprite.Group()
...
# in the main loop:
screen.fill('black')
screen.blit(text_surf, (100, 100))