我会移动我的图像,图像是matiskinfinal.png。
matiskinfinal.png
我尝试将像素添加到 x 或某些我不知道它是什么的东西,因为我真的是 python 的初学者,pygame但是是“x += x 变化”但图像没有移动。
pygame
import os import pygame,sys max_tps = 325.0 BLACK = (0, 0, 0) WHITE = (200, 200, 200) pygame.init() screen_width = 1530 screen_height = 780 pygame.display.set_caption("Kroniki Matiego") dead = False screen = pygame.display.set_mode((1530,780)) #skin skin = pygame.image.load('matiskinfinal.png') #location skin def add_skin_at_location(x,y): screen.blit(skin,(x,y)) x = 726 y = 390 add_skin_at_location(x, y) x_change = 0 y_change = 0 clock = pygame.time.Clock() delta = 0.0 #grid def drawGrid(): blockSize = 111 #Set the size of the grid block for x in range(0, screen_width, blockSize): for y in range(0, screen_height, blockSize): rect = pygame.Rect(x, y, blockSize, blockSize) pygame.draw.rect(screen, WHITE, rect, 1) while True: drawGrid() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() pygame.display.update() #image skin = pygame.image.load('matiskin.png') while True: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) #use grid drawGrid() #Ticking delta += clock.tick()/1000.0 while delta > 1 / max_tps: delta -= 1/max_tps #Checking imputs keys = pygame.key.get_pressed() if keys[pygame.K_d]: x_change += 1 if keys[pygame.K_s]: y_change += 1 if keys[pygame.K_w]: y_change -= 1 if keys[pygame.K_a]: x_change -= 1 ## dont work ## x += x_change y += y_change
我添加了一个网格,因为我想为我制作一个边框,但如果您可以在这个程序中制作边框,我将非常感激
下面是你的程序的一些问题和改进,以及一个完整的解决方案,用于实现以下功能:
x_change
y_change
x
y
解决方法:每次循环开始时将 x_change 和 y_change 重置为 0。
网格遮挡图像:
drawGrid()
解决方法:清屏 (screen.fill(BLACK)) 后再绘制网格和图像。
screen.fill(BLACK)
边框限制:
import pygame, sys # Constants BLACK = (0, 0, 0) WHITE = (200, 200, 200) SCREEN_WIDTH = 1530 SCREEN_HEIGHT = 780 BLOCK_SIZE = 111 # Grid block size # Initialization pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Kroniki Matiego") clock = pygame.time.Clock() max_tps = 325.0 # Load the character image skin = pygame.image.load("matiskinfinal.png") skin_width, skin_height = skin.get_size() # Starting position x = (SCREEN_WIDTH - skin_width) // 2 y = (SCREEN_HEIGHT - skin_height) // 2 def draw_grid(): """Draw the grid.""" for grid_x in range(0, SCREEN_WIDTH, BLOCK_SIZE): for grid_y in range(0, SCREEN_HEIGHT, BLOCK_SIZE): rect = pygame.Rect(grid_x, grid_y, BLOCK_SIZE, BLOCK_SIZE) pygame.draw.rect(screen, WHITE, rect, 1) while True: # Reset change variables x_change = 0 y_change = 0 # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Check keyboard inputs keys = pygame.key.get_pressed() if keys[pygame.K_d]: # Move right x_change += 5 if keys[pygame.K_a]: # Move left x_change -= 5 if keys[pygame.K_s]: # Move down y_change += 5 if keys[pygame.K_w]: # Move up y_change -= 5 # Update character position x += x_change y += y_change # Prevent the character from leaving the screen x = max(0, min(x, SCREEN_WIDTH - skin_width)) y = max(0, min(y, SCREEN_HEIGHT - skin_height)) # Clear screen screen.fill(BLACK) # Draw grid and character draw_grid() screen.blit(skin, (x, y)) # Update the display pygame.display.flip() # Control frame rate clock.tick(max_tps)
每次按键输入只改变一次位置。
边框约束:
使用 max() 和 min() 保证 x 和 y 不超出屏幕范围。
max()
min()
代码优化:
draw_grid()
清屏后按照顺序绘制网格和角色图像。
帧率控制:
clock.tick()
WASD
如果需要进一步修改,请随时告诉我! 😊