This is my code. There are some errors that I can't fix, it's the first time I use pygame. Can somebody help me?
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()
CodePudding user response:
blit
the image at the same position as where you draw the rectangle. Create a pygame.Rect
for the location. Get the bounding rectangle of the image and set the center of the bounding rectangle through the center of the location rectangle. Use the bounding rectangle in 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))