[英]how to make character an image pygame
I got this code from a tutorial, but I added some stuff to make it into my own thing.我从教程中获得了这段代码,但我添加了一些东西使其成为我自己的东西。 A while back I was messing around and managed to make the controllable object an image.
前段时间我在胡思乱想,设法使可控对象成为图像。 So I could move an image around the screen with W A S D , but I changed it and forgot how to fix it now, and can't manage to
blit
it onto the screen.所以我可以用W A S D在屏幕上移动一个图像,但我改变了它并且忘记了现在如何修复它,并且无法设法将它
blit
到屏幕上。 It's just a rectangle which I can move around the screen.它只是一个我可以在屏幕上移动的矩形。 How do I make it an image again?
我如何再次使它成为图像?
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("Altoria")
mc_front = pygame.image.load('bernie front.jpg')
mc_front = pygame.transform.scale(mc_front, (100, 160))
block = pygame.image.load('block.png')
block = pygame.transform.scale(block,(100,60))
block_x = 100
block_y = 50
x = 50
y = 400
width = 40
height = 60
vel = 20
run = True
while run:
win.blit(block, [block_x, block_y])
pygame.display.update()
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and x > vel: # Making sure the top left position of our character is greater than our vel so we never move off the screen.
x -= vel
if keys[pygame.K_d] and x < 500 - vel - width: # Making sure the top right corner of our character is less than the screen width - its width
x += vel
if keys[pygame.K_w] and y > vel: # Same principles apply for the y coordinate
y -= vel
if keys[pygame.K_s] and y < 500 - height - vel:
y += vel
win.fill((0,0,0))
pygame.draw.rect(win, (250,0,0), (x, y, width, height))
pygame.display.update()
pygame.quit()
A pygame.Surface
( A
) can drawn on another Surface ( B
) by B.blit(A, (x, y))
. pygame.Surface
( A
) 可以通过B.blit(A, (x, y))
在另一个 Surface ( B
) 上绘制。 eg:例如:
win.fill((0,0,0))
#pygame.draw.rect(win, (250,0,0), (x, y, width, height))
win.blit(mc_front, (x, y))
pygame.display.update()
In the end of your mainloop, you have it draw a rectangle, not an image.在你的主循环结束时,你让它绘制一个矩形,而不是图像。 To blit the image onto the screen, the end of your mainloop would have to look like this:
要将图像 blit 到屏幕上,主循环的结尾必须如下所示:
win.fill((0,0,0)) #clear screen
win.blit(mc_front, (x, y)) #add player image
pygame.display.flip() #update the display
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.