python-3.x 我正在尝试用pygame创建我的pacman版本,我怎样才能用我的主角图片替换白色的移动方块呢?

dwbf0jvd  于 2022-12-24  发布在  Python
关注(0)|答案(1)|浏览(122)

这是我的代码。有一些错误我无法修复,这是我第一次使用pygame。有人能帮我吗?

import pygame

pygame.init()

pp = pygame.image.load("warrior4.gif")
pp = pygame.transform.scale(pp, (100, 105))

size=40

tiles=[[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,1],
    [1,2,1,1,2,1,1,1,2,1,2,1,1,1,2,1,1,2,1],
    [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1],
    [1,2,1,1,2,1,2,1,1,1,1,1,2,1,2,1,1,2,1],
    [1,2,2,2,2,1,2,2,2,1,2,2,2,1,2,2,2,2,1],
    [1,1,1,1,2,1,1,1,2,1,2,1,1,1,2,1,1,1,1],
    [0,0,0,1,2,1,2,2,2,2,2,2,2,1,2,1,0,0,0],
    [1,1,1,1,2,1,2,1,1,1,1,1,2,1,2,1,1,1,1],
    [2,2,2,2,2,2,2,1,0,0,0,1,2,2,2,2,2,2,2],#
    [1,1,1,1,2,1,2,1,1,1,1,1,2,1,2,1,1,1,1],
    [0,0,0,1,2,1,2,2,2,2,2,2,2,1,2,1,0,0,0],
    [1,1,1,1,2,1,1,1,2,1,2,1,1,1,2,1,1,1,1],
    [1,2,2,2,2,1,2,2,2,1,2,2,2,1,2,2,2,2,1],
    [1,2,1,1,2,1,2,1,1,1,1,1,2,1,2,1,1,2,1],
    [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1],
    [1,2,1,1,2,1,1,1,2,1,2,1,1,1,2,1,1,2,1],
    [1,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]

(width, height) = (len(tiles)*size, len(tiles[0]*size))
screen=pygame.display.set_mode((width, height))

Teseo=[9,7]
TeseoX=Teseo[0]*size
TeseoY=Teseo[1]*size

Warrior1=[1,2]
Warrior2=[5,10]
Warrior3=[9,2]
Warrior4=[8,3]

points = 0
font = pygame.font.Font('freesansbold.ttf', 26)

textX = 10
textY = 10

win_image = pygame.image.load("win.jpg").convert()

# Functions

def write_score(x,y):
    score = font.render("Score: " + str(points), True, (255,255,255))
    screen.blit(score, (x,y))

def game_over():
    screen.fill((0,0,0))
    font = pygame.font.Font('freesansbold.ttf', 40)
    score = font.render("GAME OVER", True, (0,0,255))
    screen.blit(score, (300,300))

    def win():
    screen.blit(win_image, (0, 0))
                  
    def valid_move(col, row):
    if tiles[int(col+2/3)][int(row)]==1:
        return False
    if tiles[int(col+1/3)][int(row)]==1:
        return False
    if tiles[int(col)][int(row+2/3)]==1:
        return False
    if tiles[int(col)][int(row+1/3)]==1:
        return False
    if tiles[int(col+2/3)][int(row+2/3)]==1:
        return False
    if tiles[int(col+1/3)][int(row+1/3)]==1:
        return False
    if tiles[int(col)][int(row)]!=1:
        return True
    return False

def board():
    screen.fill((0,0,0))

    for a in range(len(tiles)):
        for b in range(len(tiles[0])):
            if tiles[a][b]==1:
                pygame.draw.rect(screen, (0,153,0), (b*size, a*size, size, size))

            elif tiles[a][b]==2:
                pygame.draw.circle(screen, (255,255,255), (b*size+size//2, a*size+size//2), size//6)

    pygame.draw.rect(screen, (255,255,255), (Teseo[0]*size, Teseo[1]*size, size, size))

    # screen.blit(pp, (TeseoX,TeseoY))

playing=True
while playing:

    for event in pygame.event.get():
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_q:
                pygame.quit()
            elif event.key==pygame.K_w:
                dir="Up"
            elif event.key==pygame.K_s:
                dir="Down"
            elif event.key==pygame.K_a:
                dir="Left"  
            elif event.key==pygame.K_d:
                dir="Right"
                
        if dir=="Up":
            if valid_move(Teseo[1]-1/3, Teseo[0]):
                Teseo[1]-=1/3
        if dir=="Down":
            if valid_move(Teseo[1]+1/3, Teseo[0]):
                Teseo[1]+=1/3
        if dir=="Right":
            if valid_move(Teseo[1], Teseo[0]+1/3):
                Teseo[0]+=1/3
        if dir=="Left":
            if valid_move(Teseo[1], Teseo[0]-1/3):
                Teseo[0]-=1/3

    if tiles[int(Teseo[1])][int(Teseo[0])]==2:
        points+=1
        tiles[int(Teseo[1])][int(Teseo[0])]=3

    if int(Teseo[0])==17 and Teseo[1]==9:
        Teseo[0]=0
    if int(Teseo[0])==0 and Teseo[1]==9:
        Teseo[0]=18

    board()
    write_score(textX, textY)
    pygame.display.update()

game_over()
mnowg1ta

mnowg1ta1#

blit在与绘制矩形的位置相同的位置创建图像。为位置创建pygame.Rect。获取图像的边框,并通过位置矩形的中心设置边框的中心。使用blit中的边框:

def board():
    screen.fill((0,0,0))
    for a in range(len(tiles)):
        for b in range(len(tiles[0])):
            if tiles[a][b]==1:
                pygame.draw.rect(screen, (0,153,0), (b*size, a*size, size, size))
            elif tiles[a][b]==2:
                pygame.draw.circle(screen, (255,255,255), (b*size+size//2, a*size+size//2), size//6)

    # pygame.draw.rect(screen, (255,255,255), (Teseo[0]*size, Teseo[1]*size, size, size))
    rect = pygame.Rect(Teseo[0]*size, Teseo[1]*size, size, size)
    screen.blit(pp, pp.get_rect(center = rect.center))

相关问题