我正在制作一个2人游戏,看起来应该是这样的:
在游戏中,射手(绿色和蓝色,由玩家控制)可以互相射击子弹
如果子弹
1.与墙壁碰撞(灰色),它被摧毁
2.击中射手,失去健康(射手)
游戏(假设是)基于回合制,并在玩家达到0
健康状态时结束。
我的问题
我枪手的镜筒没有更新/旋转
2.有没有更好的方法来检测是否按下了(ny)键
*射手,子弹,墙是班级
我的代码
(如果没有提到任何有助于回答的功能,请发表评论)
import math,random,pygame
def event_handle(event,turn):
if turn == 1:
c_s = p1
elif turn == 2:
c_s = p2
else:
return None
if event.type == pygame.KEYDOWN:
key = pygame.key.get_pressed()
# next_pos
if key[pygame.K_q]:
c_s.next_x -= 1
if key[pygame.K_e]:
c_s.next_x += 1
# angle
if key[pygame.K_w]:
c_s.angle += radians(1)
if key[pygame.K_s]:
c_s.angle -= radians(1)
# power (speed)
if key[pygame.K_d]:
c_s.speed += 0.1
if key[pygame.K_a]:
c_s.speed -= 0.1
def draw_all(bullist,shooters,wall,surface):
# draw shooters
for shooter in shooters:
shooter.move()
shooter.draw(surface)
# draw bullets
for bullet in bullist:
bullet.gravity()
bullet.move()
bullet.collides(shooters,wall,bullist)
bullet.out(surface,bullist)
bullet.draw(surface)
# wall
wall.update()
wall.draw(surface)
pygame.draw.aaline(surface,(255,255,255),(0,400),(640,400))
def angle(A,B,BC,theta):
C = [0,0]
alpha = math.atan2(A[1]-B[1] , A[0] -B[0] ) - theta
C[0] = int(round(B[0] + BC * math.cos(alpha),0))
C[1] = int(round(B[1] + BC * math.sin(alpha),0))
return C
class Shooter:
def __init__(self,pos,size,color,xmax,xmin):
self.pos = pos
self.size = size
self.rect = pygame.Rect(pos,size)
self.health = 100
self.color = color
self.angle = 0
self.speed = 0
self.max = xmax
self.min = xmin
self.next_x = pos[0]
self.color2 = []
for i in color:
i = i - 100
if i < 0:
i = 0
self.color2.append(i)
def draw(self,surface):
global C
pygame.draw.rect(surface,self.color,self.rect)
c = angle(self.rect.midleft,self.rect.center,
20,radians(self.angle))
if c != C and c != [95,392]:
print c
C = c
pygame.draw.line(surface,self.color2,self.rect.center,c,3)
## the other funcs or classes not needed
# globals
turn = 1
C = []
# pygame
(width, height) = (640, 480)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Shooter')
clock = pygame.time.Clock()
# game actors
shooters = []
bullets = []
p1 = Shooter((400,400),(30,-15),(255,0,0),0,0)
p2 = Shooter((100,400),(30,-15),(0,255,0),0,0)
shooters.extend([p1, p2])
wall = Wall(100)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
break
else: event_handle(event,turn)
if not running:
break
screen.fill((0,0,0))
# Game draw logic + Game logic
draw_all(bullets,shooters,wall,screen)
pygame.display.flip()
clock.tick(40)
答案 0 :(得分:1)
您的代码确实有效,但存在一些问题。桶实际上是旋转的,但每次按键时只有很小的数量。
尝试将event_handle
功能更改为:
def event_handle(turn):
if turn == 1:
c_s = p1
elif turn == 2:
c_s = p2
else:
return None
key = pygame.key.get_pressed()
# next_pos
if key[pygame.K_q]:
c_s.next_x -= 1
if key[pygame.K_e]:
c_s.next_x += 1
# angle
if key[pygame.K_w]:
c_s.angle += radians(10)
if key[pygame.K_s]:
c_s.angle -= radians(10)
# power (speed)
if key[pygame.K_d]:
c_s.speed += 0.1
if key[pygame.K_a]:
c_s.speed -= 0.1
由于此时你根本没有介入事件类型,我删除了event
参数和if event.type == pygame.KEYDOWN:
检查。这样,您可以按住按键,而不是被迫多次按键来旋转枪管。
我还将桶的值从radians(1)
增加到radians(10)
。否则,更改太小而无法显示(在合理的时间内)。
此外,您必须将主循环调整为
...
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
break
if not running:
break
event_handle(turn)
...
所以在主循环的每次迭代中调用event_handle
。
答案 1 :(得分:0)
如果您使用的是key.get_pressed,则根本不需要检测key_down事件。只需删除if event.type == pygame.KEYDOWN:
语句,然后检查pygame.key.get_pressed
返回的内容是否包含您的密钥。
Alos,您应该在每次循环运行时在代码中的某处调用pygame.event.pump
。把它放在你的while循环中。
(和你的问题无关的提示:将一个主循环移动到一个函数中 - 将它挂在程序体中就像这样可怕了)