我想让我的角色在单击按键时左右移动,但是我不知道该怎么做,我意识到我需要将它们分配给character变量来执行此操作。但是我不知道该怎么办。
我之前已经问过这个问题,但是与我的代码相比,该解决方案确实难以遵循,因此找不到解决方案。
import pygame
pygame.init()
window = pygame.display.set_mode((750, 750))
pygame.display.set_caption("PeaShooters")
avatar = pygame.image.load('Sprite 1 Red.png')
background = pygame.image.load('Bg.jpg')
x = 64
y =64
width = 40
height = 60
vel = 5
white = (255, 255, 255)
def drawGrid():
window.blit(background, (0,0))
window.blit(avatar, (300,500))
pygame.draw.line(window, white, [50,50], [50, 600], 5)
pygame.draw.line(window, white, [50,50], [600, 50], 5)
pygame.draw.line(window, white, [600,600], [600, 50], 5)
pygame.draw.line(window, white, [50,600], [600, 600], 5)
pygame.draw.line(window, white, [50,450], [600, 450], 5)
pygame.display.update()
running = True
while running:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and y > 455:
y += vel
if keys[pygame.K_a] and x > 55:
x -= vel
if keys[pygame.K_s] and y < 565:
y -= vel
if keys[pygame.K_d] and x < 575 :
x += vel
x += x
drawGrid()
pygame.quit()
我希望单击某个按钮时精灵会沿指定方向移动。
答案 0 :(得分:1)
删除此无用的行
$ lein repl
nREPL server started on port 44037 on host 127.0.0.1 - nrepl://127.0.0.1:44037
REPL-y 0.4.3, nREPL 0.6.0
Clojure 1.10.0
OpenJDK 64-Bit Server VM 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
dynaframe.core=> (compile 'joy.gui.DynaFrame)
joy.gui.DynaFrame
dynaframe.core=> (def hello-frame (joy.gui.DynaFrame. "Hello"))
#'dynaframe.core/hello-frame
dynaframe.core=> (.show hello-frame)
nil
然后使用x += x
将头像设置在正确的位置
(x, y)
这对我有用。
编辑:您在window.blit(avatar, (x, y))
和y -= ...
中遇到了一些错误-您必须更改位置。
用y += ...
代替y > 55
y > 455