self.p中的x和y不更新 Self.p是一个绘制变量
当您使用箭头键时,它们会更改x,而按钮x不会更改变量P ................................................... ................................
self.P = pygame.Rect(self.x, self.y, self.width, self.height)
self.OB = pygame.Rect(self.oX, self.oY, self.width, self.height)
很抱歉格式化
这是我的代码:
import pygame
import time
pygame.init()
pygame.display.set_caption('Platformer')
class Game:
def __init__(self):
self.DEBUG=True
self.x=7
self.y=400
self.tX=1200
self.tY=600
self.width=40
self.height=60
self.vel=9
self.run=True
self.isJump=False
self.jumpCount=10
self.clock = pygame.time.Clock()
self.clock.tick(60)
self.flag = False
self.win=pygame.display.set_mode((1200, 600))
self.font = pygame.font.Font('freesansbold.ttf', 64)
self.dieText = self.font.render('Well crap', True, (255,0,0), (245,245,245))
self.dieRect = self.dieText.get_rect()
self.dieRect.center = (self.tX // 2, self.tY // 2)
self.die = False
self.dieFlag = False
self.pause = False
self.pauseFlag=False
self.pauseText = self.font.render('Pause', True, (0,150,150), (245,245,245))
self.pauseRect = self.pauseText.get_rect()
self.pauseRect.center = (self.tX // 2, 150)
self.oX = 400
self.oY = 400
self.P = pygame.Rect(self.x, self.y, self.width, self.height)
self.OB = pygame.Rect(self.oX, self.oY, self.width, self.height)
self.pColor = (0,0,0)
self.oColor = (255, 0, 0)
def GameLoop(self):
while self.run:
if self.DEBUG==True:
print(self.x)
print(self.P)
print(self.OB)
if self.flag == False:
print('yes')
if self.pauseFlag==True:
print('ok')
pygame.time.delay(15)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
self.run == False
keys = pygame.key.get_pressed()
if self.die == True:
self.dieFlag=True
if self.pause == True:
self.pauseFlag=True
if self.pause==False:
self.pauseFlag=False
if keys[pygame.K_p]:
self.pause=True
if keys[pygame.K_o]:
self.pause=False
if keys[pygame.K_ESCAPE]:
pygame.quit()
quit()
if self.die==False and self.pause==False:
if self.flag == False and keys[pygame.K_SPACE] or keys[pygame.K_UP]:
self.starttime = pygame.time.get_ticks()
self.flag = True
self.isJump=True
if self.flag == True and pygame.time.get_ticks() - self.starttime >= 475:
self.flag = False
if keys[pygame.K_p] and self.pause==False and self.die == False:
self.pause=True
if keys[pygame.K_o] and self.pause== True:
self.pause=False
if keys[pygame.K_LEFT] and self.x > self.vel:
self.x-=self.vel
if keys[pygame.K_RIGHT] and self.x < 1200 - self.width - self.vel :
self.x+=self.vel
if keys[pygame.K_k]:
self.die=True
if not(self.isJump):
if keys[pygame.K_DOWN] and self.y < 600 - self.height - self.vel:
self.y += 0
else:
if self.jumpCount >= -10:
self.y -= (self.jumpCount * abs(self.jumpCount)) * .55
self.jumpCount -= 1
else:
self.jumpCount = 10
self.isJump = False
if self.dieFlag == True:
self.win.fill((245,245,245))
if self.pauseFlag == True:
self.win.blit(self.pauseText, self.pauseRect)
pygame.draw.rect(self.win, self.oColor, (self.oX,self.oY, 100, 200))
pygame.draw.rect(self.win, self.pColor, (self.x,self.y, self.width, self.height))
self.win.blit(self.dieText, self.dieRect)
pygame.display.update()
elif self.pauseFlag==False:
pygame.draw.rect(self.win, self.oColor, (self.oX,self.oY, 100, 200))
pygame.draw.rect(self.win, self.pColor, (self.x,self.y, self.width, self.height))
self.win.blit(self.dieText, self.dieRect)
pygame.display.update()
elif self.dieFlag == False:
if self.pauseFlag == True:
self.win.fill((245,245,245))
self.win.blit(self.pauseText, self.pauseRect)
pygame.draw.rect(self.win, self.oColor, (self.oX,self.oY, 100, 200))
pygame.draw.rect(self.win, self.pColor, (self.x,self.y, self.width, self.height))
pygame.display.update()
elif self.pauseFlag==False:
self.win.fill((245,245,245))
pygame.draw.rect(self.win, self.oColor, (self.oX,self.oY, 40, 60))
pygame.draw.rect(self.win, self.pColor, (self.x,self.y, self.width, self.height))
pygame.display.update()
g=Game()
g.GameLoop()
任何帮助都非常感谢 也很抱歉格式化
答案 0 :(得分:0)
如果我没有丢失任何内容,在您发布的代码中,没有地方可以正确更改P和OB的值。?
请注意,这两个变量特定于您的类的实例。它们是“实例变量”
我建议 rewrite annotations 了解类变量和实例变量之间的区别。