我正在尝试让坦克移动(他们已经做过)但是我想让它能够按住WASD或4个箭头键让它快速移动。现在,你必须反复点击箭头键才能移动。
import pygame, assetloader
from pygame.locals import *
import random, time, math
import pygame
GRAD = math.pi/180
blue = (0, 0, 255)
wallRects = []
maze = [[] for i in range(25)]
assetloader.set_asset_path("assets/")
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, pos):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = assetloader.load_image("Tank.png", -1)
self.rect.x = x
self.rect.y = y
self.rect.clamp_ip(screen.get_rect())
self.dir = 0
self.vel_y = 0
self.vel_x = 0
self.rows = pos[0]
self.cols = pos[1]
self.x = self.cols * gsize
self.y = self.rows * gsize
self.orig_image, self.orig_rect = assetloader.load_image("Tank.png", -1)
self.orig_rect.x = self.x
self.orig_rect.y = self.y
self.orig_gun_pos = self.orig_rect.midtop
self.ammo = 5
self.vel = [0, 0]
self.dead = False
def draw(self, screen):
image = pygame.transform.rotate(self.image, self.dir)
screen.blit(image, self.rect)
def update(self):
oldCenter = self.rect.center
self.rect = self.image.get_rect()
self.rect.center = oldCenter
screen_rect = screen.get_rect()
keys = pygame.key.get_pressed()
if keys[K_UP]:
if (0 < self.dir and self.dir < 90) or (-360 < self.dir and self.dir < -270):
self.vel_x = -1
self.vel_y = -1
elif (270 < self.dir and self.dir < 360) or (-90 < self.dir and self.dir < 0):
self.vel_x = 1
self.vel_y = -1
if (90 < self.dir and self.dir < 180) or (-270 < self.dir and self.dir < -180):
self.vel_x = -1
self.vel_y = 1
elif (180 < self.dir and self.dir < 270) or (-180 < self.dir and self.dir < -90):
self.vel_x = 1
self.vel_y = 1
if self.dir == 0 :
self.vel_x = 0
self.vel_y = -1
if self.dir == 90 :
self.vel_x = -1
self.vel_y = 0
if self.dir == 180:
self.vel_x = 0
self.vel_y = 1
if self.dir == 270:
self.vel_x = 1
self.vel_y = 0
self.rect.move_ip(self.vel_x, self.vel_y)
elif keys[K_DOWN]:
if (0 < self.dir and self.dir < 90) or (-360 < self.dir and self.dir < -270):
self.vel_x = 1
self.vel_y = 1
elif (270 < self.dir and self.dir < 360) or (-90 < self.dir and self.dir < 0):
self.vel_x = -1
self.vel_y = 1
if (90 < self.dir and self.dir < 180) or (-270 < self.dir and self.dir < -180):
self.vel_x = 1
self.vel_y = -1
elif (180 < self.dir and self.dir < 270) or (-180 < self.dir and self.dir < -90):
self.vel_x = -1
self.vel_y = -1
if self.dir == 0 :
self.vel_x = 0
self.vel_y = 1
if self.dir == 90 :
self.vel_x = 1
self.vel_y = 0
if self.dir == 180:
self.vel_x = 0
self.vel_y = -1
if self.dir == 270:
self.vel_x = -1
self.vel_y = 0
self.rect.move_ip(self.vel_x, self.vel_y)
if keys[K_LEFT]:
self.dir += 5
if self.dir > 360:
self.dir = 0
elif keys[K_RIGHT]:
self.dir -= 5
if self.dir < -360:
self.dir = 0
if not screen_rect.contains(self.rect):
self.rect.clamp_ip(screen_rect)
class Enemy(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = assetloader.load_image("New Piskel.png", -1)
self.rect.x = x
self.rect.y = y
self.dir = 0
self.rect.clamp_ip(screen.get_rect())
def draw(self, screen):
image = pygame.transform.rotate(self.image, self.dir)
screen.blit(image, self.rect)
def update(self):
screen_rect = screen.get_rect()
keys = pygame.key.get_pressed()
if keys[K_w]:
if (0 < self.dir and self.dir < 90) or (-360 < self.dir and self.dir < -270):
self.vel_x = -1
self.vel_y = -1
elif (270 < self.dir and self.dir < 360) or (-90 < self.dir and self.dir < 0):
self.vel_x = 1
self.vel_y = -1
if (90 < self.dir and self.dir < 180) or (-270 < self.dir and self.dir < -180):
self.vel_x = -1
self.vel_y = 1
elif (180 < self.dir and self.dir < 270) or (-180 < self.dir and self.dir < -90):
self.vel_x = 1
self.vel_y = 1
if self.dir == 0 :
self.vel_x = 0
self.vel_y = -1
if self.dir == 90 :
self.vel_x = -1
self.vel_y = 0
if self.dir == 180:
self.vel_x = 0
self.vel_y = 1
if self.dir == 270:
self.vel_x = 1
self.vel_y = 0
self.rect.move_ip(self.vel_x, self.vel_y)
elif keys[K_s]:
if (0 < self.dir and self.dir < 90) or (-360 < self.dir and self.dir < -270):
self.vel_x = 1
self.vel_y = 1
elif (270 < self.dir and self.dir < 360) or (-90 < self.dir and self.dir < 0):
self.vel_x = -1
self.vel_y = 1
if (90 < self.dir and self.dir < 180) or (-270 < self.dir and self.dir < -180):
self.vel_x = 1
self.vel_y = -1
elif (180 < self.dir and self.dir < 270) or (-180 < self.dir and self.dir < -90):
self.vel_x = -1
self.vel_y = -1
if self.dir == 0 :
self.vel_x = 0
self.vel_y = 1
if self.dir == 90 :
self.vel_x = 1
self.vel_y = 0
if self.dir == 180:
self.vel_x = 0
self.vel_y = -1
if self.dir == 270:
self.vel_x = -1
self.vel_y = 0
self.rect.move_ip(self.vel_x, self.vel_y)
if keys[K_a]:
self.dir += 5
if self.dir > 360:
self.dir = 0
elif keys[K_d]:
self.dir -= 5
if self.dir < -360:
self.dir = 0
if not screen_rect.contains(self.rect):
self.rect.clamp_ip(screen_rect)
size = width, height = 500, 400
gsize = 25
start_x, start_y = 0, 0
bgColor = 255, 255, 255
pygame.init()
screen = pygame.display.set_mode(size)#, pygame.FULLSCREEN)
pygame.display.set_caption("Sample Sprite")
clock = pygame.time.Clock()
p = Player(width/2, height/4, (3,4))
e = Enemy(width/2, height/4)
coll_font = pygame.font.Font(None, 30)
going = True
while going:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
going = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
going = False
p.update()
e.update()
screen.fill(bgColor)
p.draw(screen)
e.draw(screen)
pygame.display.flip()
pygame.quit()
现在没有错误,但我希望不断移动而不是一遍又一遍地重复按键。有人可以向我解释一下吗?
答案 0 :(得分:0)
我认为您的问题是while going
函数update()
位于for event
循环内部,因此仅当您移动鼠标,按下/释放鼠标按钮或按下/释放键时(当密钥从not-pressed
更改为{时)它们才会执行{1}}或从pressed
到pressed
),但不是在按下按键时。
not-pressed