私信  •  关注

Rabbid76

Rabbid76 最近创建的主题
Rabbid76 最近回复了
2 年前
回复了 Rabbid76 创建的主题 » 我如何解决屏幕上的这个问题。python中pygame中的blit?

enemyX enemyY 以下是清单:

enemy(enemyX, enemyY, i)

enemy(enemyX[i], enemyY[i], i)

有2个 Indentation 问题。第一个在 player.draw

class player(object):
    # [...]

    def draw(self,win):

        if self.moveCount + 1 >= 9: 
            self.moveCount = 0

        #<--| INDENTATION !!!
        if self.left:
                win.blit(moveLeft[self.moveCount//3], (self.x,self.y)) 
                self.moveCount += 1
        elif self.right:
            # [...]

第二个在主回路中:

run = True
while run:
    # [...]

    else:       #this is incase the player is not moving
        man.left = False
        man.right = False
        man.up = False
        man.down = False
        man.moveCount = 0

    #<--| INDENTATION !!!
    redrawGameWindow()

pygame.transform.scale 代码开头的指令是无用的,因为 . 不缩放曲面本身,它将返回一个新的缩放曲面。

更改代码如下( str

moveRight = [pygame.transform.scale(pygame.image.load('R' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
moveLeft  = [pygame.transform.scale(pygame.image.load('L' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
moveUp    = [pygame.transform.scale(pygame.image.load('U' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
moveDown  = [pygame.transform.scale(pygame.image.load('D' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
character = pygame.transform.scale(pygame.image.load('D2.png'), (40, 60))
4 年前
回复了 Rabbid76 创建的主题 » Python只在while循环中运行一次

典型的应用程序有一个单独的应用程序循环。应用程序循环执行以下操作:

  • 处理事件并根据事件更改状态
  • 清除显示
  • 绘制场景
  • 更新显示

这个 KEYDOWY 事件在按下键时发生一次,但在按住键时不会连续发生。
对于连续移动,可以通过 pygame.key.get_pressed() :

keys = pygame.key.get_pressed()

e、 g.如果 s公司 按下可由 keys[pygame.K_s] .

添加坐标( x , y )矩形的位置。当按键时,连续操作主应用程序循环中的位置。

例如
增量 如果 被压制和减量 如果 被按下。
增量 是的 如果 s公司 被压制和减量 是的 如果 西 按下:

import pygame

r_colour = (200, 100,100)
bg_colour = (0,175,200)
(width, height) = (600, 600)
x, y = 20, 30

screen = pygame.display.set_mode((width, height))

running = True
while running:

    # handle the events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # change coordinates
    keys = pygame.key.get_pressed()
    if keys[pygame.K_d]:
        x += 1
    if keys[pygame.K_a]:
        x -= 1
    if keys[pygame.K_s]:
        y += 1
    if keys[pygame.K_w]:
        y -= 1

    # clear the display
    screen.fill(bg_colour)
    # draw the scene
    pygame.draw.rect(screen, r_colour, (x, y, 100, 100), 0)
    # update the display
    pygame.display.update()

pygame.quit()
5 年前
回复了 Rabbid76 创建的主题 » python 3.2w/pygame崩溃

它必须是 pygame.KEYDOWN pygame.K_ESC 而不是 KEYDOWN K_ESC .

但首先你要尊重 Indentation . 在以下代码中,循环没有嵌套:

done = False
while not done:
 pygame.time.delay(100)

for event in pygame.event.get():
 if event.type == pygame.QUIT:
  done = True

这个 for 循环不在 while ,它是 虽然 循环。

你必须这样格式化你的代码:

import pygame
pygame.init()

print("First Fase")
win = pygame.display.set_mode((500,500)) 

pygame.display.set_caption("Test")

print("Second Fase")
x = 50
y =50
width = 40
height = 60
vel = 7

print("Third Fase")

done = False
while not done:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESC:
                done = True

    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()
    print("NoErrors")