gpt4 book ai didi

python - Pygame 中向上转换物的故障排除

转载 作者:太空宇宙 更新时间:2023-11-03 19:57:52 24 4
gpt4 key购买 nike

我正在编写一款坦克决斗游戏,类似于“坦克麻烦”。当然,我现在还只是刚刚起步,而且我有点卡在一个特定的小错误上。目前,下面的代码使我能够显示我的 Sprite ,能够向各个方向移动,并且还能够发射射弹。然而,唯一的问题是它只能向左或向右发射射弹。有人对如何改变这一点有什么建议,以便坦克在面朝上时可以向上射击,面朝下时可以向下射击吗?即使朝上或朝下,它也只能向左和向右射击,这有点奇怪。我无法访问 y 轴并让射弹沿其行进。

如果有人能找到如何做到这一点并告诉我新代码是什么以及将其放置在哪里,那将会有很大帮助。这是我的代码:

import pygame
pygame.init()

screen = pygame.display.set_mode((500,500))

pygame.display.set_caption("Game")
tankImg = pygame.image.load("tank1.png")
downTank = tankImg
leftTank = pygame.image.load("left1.png")
rightTank = pygame.image.load("right1.png")
upTank = pygame.image.load("up1.png")
bg = pygame.image.load("background.png")


screenWidth = 500
screenHeight = 500

clock = pygame.time.Clock()

class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.left = False
self.right = False
self.up = False
self.down = False

def draw(self,screen):
screen.blit(tankImg,(self.x,self.y))

class projectile(object):
def __init__(self,x, y, radius, color, facing):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.facing = facing
self.vel = 8 * facing

def draw(self,screen):
pygame.draw.circle(screen, self.color, (self.x,self.y), self.radius)

def redraw():
screen.blit(bg, (0,0))
tank.draw(screen)

for bullet in bullets:
bullet.draw(screen)

pygame.display.update()

run = True
tank = player(300, 410, 16, 16)
bullets = []
while run:

clock.tick(30)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

for bullet in bullets:
if bullet.x < screenWidth and bullet.x > 0:
bullet.x += bullet.vel
elif bullet.y > screenHeight and bullet.y > 0:
bullet.y += bullet.vel
else:
bullets.pop(bullets.index(bullet))



keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and tank.x > tank.vel:
tank.left = True
tankImg = leftTank
tank.x -= tank.vel
facing = -1
if keys[pygame.K_RIGHT] and tank.x < 500 - tank.width:
tank.right = True
tankImg = rightTank
tank.x += tank.vel
facing = 1
if keys[pygame.K_UP] and tank.y > tank.vel:
tank.up = True
tankImg = upTank
tank.y -= tank.vel
facing = 1
if keys[pygame.K_DOWN] and tank.y < 500 - tank.height:
down = True
tankImg = downTank
tank.y += tank.vel
facing = -1
if keys[pygame.K_SPACE]:
if len(bullets) < 1:
bullets.append(projectile(round(tank.x + tank.width // 2), round(tank.y + tank.height // 2), 4, (0,0,0),facing))

redraw()

pygame.quit()

最佳答案

问题是由这部分代码引起的:

if bullet.x < screenWidth and bullet.x > 0:
bullet.x += bullet.vel
elif bullet.y > screenHeight and bullet.y > 0:
bullet.y += bullet.vel
else:
bullets.pop(bullets.index(bullet))

只要子弹位于窗口范围内,第一个条件的计算结果就是True。这导致子弹只能在 x 方向移动。

projectile 类添加一个 direction 属性,而不是 faceing 属性。此外,添加一个移动射弹的方法(move)。此方法忽略窗口的边界:

class projectile(object):
def __init__(self,x, y, radius, color, direction):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.direction = direction
self.vel = 8

def move(self):
self.x += self.direction[0] * self.vel
self.y += self.direction[1] * self.vel

def draw(self,screen):
pygame.draw.circle(screen, self.color, (self.x,self.y), self.radius)

在循环结束时移动项目符号,评估项目符号是否在窗口范围内。通过使用 pygame.Rect 可以轻松完成此操作和collidepoint() :

while run:
# [...]

for bullet in bullets[:]:
bullet.move()
window_rect = pygame.Rect(0, 0, screenWidth, screenHeight)
if not window_rect.collidepoint((bullet.x, bullet.y)):
bullets.pop(bullets.index(bullet))

根据坦克的移动方向设置子弹的方向:

direction = (-1, 0)
while run:
# [...]

if keys[pygame.K_LEFT] and tank.x > tank.vel:
tank.left = True
tankImg = leftTank
tank.x -= tank.vel
direction = (-1, 0)
if keys[pygame.K_RIGHT] and tank.x < 500 - tank.width:
tank.right = True
tankImg = rightTank
tank.x += tank.vel
direction = (1, 0)
if keys[pygame.K_UP] and tank.y > tank.vel:
tank.up = True
tankImg = upTank
tank.y -= tank.vel
direction = (0, -1)
if keys[pygame.K_DOWN] and tank.y < 500 - tank.height:
down = True
tankImg = downTank
tank.y += tank.vel
direction = (0, 1)
if keys[pygame.K_SPACE]:
if len(bullets) < 1:
px, py = round(tank.x + tank.width // 2), round(tank.y + tank.height // 2)
bullets.append(projectile(px, py, 4, (0,0,0), direction))
<小时/>

<小时/>

我建议在 pygame.KEYDOWN 事件中生成子弹,而不是评估按下的按键 (keys[pygame.K_SPACE])。当按下按钮时该事件发生一次。因此,每次按下 SPACE 时都会生成一个子弹(当然这取决于您):

direction = (-1, 0)
while run:
clock.tick(30)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event. key == pygame.K_SPACE:
px, py = round(tank.x + tank.width // 2), round(tank.y + tank.height // 2)
bullets.append(projectile(px, py, 4, (0,0,0), direction))

for bullet in bullets[:]:
bullet.move()
window_rect = pygame.Rect(0, 0, screenWidth, screenHeight)
if not window_rect.collidepoint((bullet.x, bullet.y)):
bullets.pop(bullets.index(bullet))

keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and tank.x > tank.vel:
tank.left = True
tankImg = leftTank
tank.x -= tank.vel
direction = (-1, 0)
if keys[pygame.K_RIGHT] and tank.x < 500 - tank.width:
tank.right = True
tankImg = rightTank
tank.x += tank.vel
direction = (1, 0)
if keys[pygame.K_UP] and tank.y > tank.vel:
tank.up = True
tankImg = upTank
tank.y -= tank.vel
direction = (0, -1)
if keys[pygame.K_DOWN] and tank.y < 500 - tank.height:
down = True
tankImg = downTank
tank.y += tank.vel
direction = (0, 1)

redraw()

关于python - Pygame 中向上转换物的故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59446624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com