作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让我的敌人射击我的玩家,但由于某种原因,子弹没有显示,也没有向玩家射击我什至不知道为什么,我什至在我的 window 上画了子弹 VIDEO
bulls = []
runninggame = True
while runninggame:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
for bull in bulls:
if bull.x < 500 and bull.x > 0:
bull.x += bull.vel # Moves the bullet by its vel
else:
for enemys in enemying:
if playerman.x - 100 <= enemys.x <= playerman.x + 100 and playerman.y - 100 <= enemys.y <= playerman.y + 100:
bulls.pop(bulls.index(bull))
bulls.append(enemybullets(round(enemys.x+enemys.width//2), round(enemys.y + enemys.height//2), 6, (0,0,0), facing))
然后我在下面调用它
for bull in bulls:
bull.draw(window)
我的敌人射弹类
# enemys bullets
class enemybullets(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,win):
pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)
最佳答案
这是我前一段时间在教程的帮助下制作的一个非常相似的游戏的代码。
我的第一个建议是尝试降低子弹的速度。这样您就可以确保不会遗漏屏幕上的任何内容。
接下来确保您正确更新屏幕内容。
如果不查看您的完整代码,很难给出全面的答案。我已将我的代码包含在此处,以便您可以引用并解决问题。
希望这对您有所帮助。
import pygame
pygame.init()
win = pygame.display.set_mode((800, 480))
clock = pygame.time.Clock()
char = pygame.image.load('standing.png')
bulletSound = pygame.mixer.Sound('gshot.wav')
shotHit = pygame.mixer.Sound('bounce.wav')
backSound = pygame.mixer.music.load('music.mp3')
pygame.mixer.music.play(-1)
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'),
pygame.image.load('L4.png'), pygame.image.load('L5.png'),
pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'),
pygame.image.load('L9.png')]
walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'),
pygame.image.load('R4.png'), pygame.image.load('R5.png'),
pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'),
pygame.image.load('R9.png')]
b_walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'),
pygame.image.load('L4E.png'), pygame.image.load('L5E.png'),
pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'),
pygame.image.load('L9E.png'), pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
b_walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'),
pygame.image.load('R4E.png'), pygame.image.load('R5E.png'),
pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'),
pygame.image.load('R9E.png'), pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
pygame.display.set_caption("My Samurai(v1.0)")
x, y = 50, 50
bx, by = 250, 250
p_width, p_height = 40, 50
b_width, b_height = 40, 50
jumpCount = 5
walkCount = 0
class bullets:
def __init__(self, x, y, surface, mov_speed):
self.x = x
self.y = y
self.surface = surface
self.mov_speed = mov_speed
def draw(self):
pygame.draw.circle(self.surface, (200, 20, 20), (self.x, self.y), 3, 1)
self.x += self.mov_speed
class figure:
def __init__(self, x, y, width, height, surface):
self.x = x
self.y = y
self.width = width
self.height = height
self.surface = surface
self.jumpCount = 5
self.jump = False
self.walkCount = 0
self.move = False
self.maxCount = 0
self.hitboxWidth = 55
self.hitboxHeight = 55
self.hitbox = [self.x, self.y, self.hitboxWidth, self.hitboxHeight]
def p_jump(self):
if self.jump:
print("i came here")
if self.jumpCount > 0:
self.y -= int((self.jumpCount ** 2) / 2) * 6
self.jumpCount -= 1
print("im here 1", self.jumpCount, "y pos", self.y)
else:
self.y += int((self.jumpCount ** 2) / 2) * 6
self.jumpCount -= 1
print("i came here 2", self.jumpCount)
if self.jumpCount == -6:
self.jumpCount = 5
self.jump = False
print(jump)
def draw(self, dirL, dirR, surface, arrayL, arrayR):
#pygame.draw.rect(self.surface, (255, 0, 0), (self.hitbox[0], self.hitbox[1], self.hitboxWidth, self.hitboxHeight), 1)
if dirL:
if self.move:
surface.blit(arrayL[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
else:
surface.blit(arrayL[self.walkCount // 3], (self.x, self.y))
if dirR:
if self.move:
surface.blit(arrayR[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
else:
surface.blit(arrayR[self.walkCount // 3], (self.x, self.y))
if self.walkCount > self.maxCount:
self.walkCount = 0
speed = 5
jump = False
run = True
left = False
right = True
player = figure(x, y, p_width, p_height, win)
beast = figure(bx, by, b_width, b_height, win)
player.maxCount = 24
beast.maxCount = 30
beast.move = True
player.hitboxWidth = 42
beast.hitboxWidth = 42
body = []
i = 0
bSpeed = 8
score = 0
score_font = pygame.font.SysFont('arial', 20, 1, 1)
hit_font = pygame.font.SysFont('calibri', 80, 1, 1)
while run:
pygame.time.delay(50)
pygame.display.update()
clock.tick(27)
bg = pygame.image.load('bg.jpg')
win.blit(bg, (0, 0))
score_display = score_font.render("Score: {}".format(score), 1, (0, 0, 0))
win.blit(score_display, (700, 10))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player.x > 0:
player.x -= speed
left = True
right = False
player.move = True
bSpeed = -8
elif keys[pygame.K_RIGHT] and player.x + p_width + speed + 5 < 800:
player.x += speed
left = False
right = True
player.move = True
bSpeed = 8
else:
player.move = False
player.walkCount = 0
if keys[pygame.K_UP] and player.y > 0 and not jump:
player.y -= speed
if keys[pygame.K_DOWN] and player.y + p_height + speed + 5 < 480 and not jump:
player.y += speed
if keys[pygame.K_SPACE]:
player.jump = True
if keys[pygame.K_s] and i <= 0:
body.append(bullets(player.x + 32, player.y + 40, win, bSpeed))
i = 4
bulletSound.play()
for bullet in body:
bullet.draw()
if beast.x + 25 < bullet.x < beast.x + beast.hitboxWidth:
if beast.y < bullet.y < beast.y + beast.hitboxHeight:
body.pop(body.index(bullet))
score += 1
shotHit.play()
# beast position cal
if beast.x - player.x > 0:
b_right = False
b_left = True
beast.x -= int((beast.x - player.x) * 0.025)
else:
b_right = True
b_left = False
beast.x -= int((beast.x - player.x) * 0.025)
if beast.y - player.y > 0:
beast.y -= int((beast.y - player.y) * 0.025)
else:
beast.y -= int((beast.y - player.y) * 0.025)
if i > 0:
i -= 1
player.p_jump()
player.draw(left, right, win, walkLeft, walkRight)
beast.draw(b_left, b_right, win, b_walkLeft, b_walkRight)
player.hitbox = [player.x + 10, player.y + 8, player.hitboxWidth, player.hitboxHeight]
beast.hitbox = [beast.x + 10, beast.y + 5, beast.hitboxWidth, beast.hitboxHeight]
if (player.hitbox[0] < beast.hitbox[0] < player.hitbox[0] + player.hitbox[2] - 20) or\
(player.hitbox[0] + 15 < beast.hitbox[0] + beast.hitbox[2] <= player.hitbox[0] + player.hitbox[2]):
if (player.hitbox[1] < beast.hitbox[1] < player.hitbox[1] + player.hitbox[3] - 10) or\
(player.hitbox[1] + 10 < beast.hitbox[1] + beast.hitbox[3] < player.hitbox[1] + player.hitbox[3]):
score -= 5
hit_display = hit_font.render("-5", 1, (255, 0, 0))
win.blit(hit_display, (400, 200))
pygame.display.update()
pygame.time.delay(1000)
player.x, player.y = 100, 100
beast.x, beast.y = 600, 300
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
关于python - 有敌人射击玩家问题吗?游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62107874/
我需要实现射击机制。我需要射弹的连续运动,但我无法对其进行编码。此时,当我按下空格键时,程序就会卡住。我们将不胜感激。 from tkinter import * # creates window w
我遇到了一个问题。如果有任何帮助,我将不胜感激。 我正在尝试从玩家位置射击到鼠标点击位置。代码没有给我任何错误,根据我的逻辑,它应该可以工作,但它没有 它创建了项目符号对象,仅此而已。 //Bulle
我正在用 Java 开发一款 Android 游戏,其中我将有一个 Sprite 跟随用户的手指,并且应该每秒发射一颗子弹。换句话说,我试图附加一个每秒向上移动的位图。位图从主要角色 Sprite 的
我需要在 JQuery 中使用 focusin 和 focusout 触发器。我将它与 Bootstrap Popovers 一起使用。 目前,由于我认为某些奇怪的原因,它不起作用。 这有什么问题吗?
我是一名优秀的程序员,十分优秀!