如何移动我的图像? Python 3.10.4 Pygame
我会将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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在主循环中添加以下代码。
add_skin_at_location(x,y)
You could add the following code in the main loop.
add_skin_at_location(x,y)