如何移动我的图像? Python 3.10.4 Pygame

发布于 2025-01-19 09:07:50 字数 1952 浏览 0 评论 0原文

我会将WSAD移动我的图像,图像为matiskinfinal.png

我尝试将像素添加到X或我不知道的东西,因为我确实是Python和pygame的初学者,但是是“ x += x更改”,但图像不移动。

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

我添加了一个网格,因为我想为我建立边界,但是如果您可以在此程序中进行边界,我将非常感谢

I would move wsad my image, image is matiskinfinal.png.

I tried add to pixels to x or something I don't know what is it because I am really a beginner in python and pygame but is a "x += x change" but the image does not move.

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

I added a grid because I wanted to make a border for me, but if you can do a border in this program, I would be very grateful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一向肩并 2025-01-26 09:07:50

您可以在主循环中添加以下代码。

add_skin_at_location(x,y)

You could add the following code in the main loop.

add_skin_at_location(x,y)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文