我是python / pygame的初学者,并且是此页的新手。我有以下问题: 我的玩家等级达到了敌人等级的检测范围! 每2秒向玩家射击一次,但不是1枚子弹而是一次2枚,或者偏移0.1秒! 但是我只希望击落1个子弹而不是2个! 创建者提供的代码:Kidscancode(https://www.youtube.com/channel/UCNaPQ5uLX5iIEHUCLmfAgKg)! 不幸的是,他没有回答我一个月前发送的邮件! 您能看一下我的代码并告诉我哪里有错误吗? (来自德国,由翻译人员翻译)
仇敌-克拉斯:
import pygame as pg
from settings import *
from sprites import *
from random import randint, uniform
vec = pg.math.Vector2
class Mob(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.mobs
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.Surface((140, 20))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
self.pos = vec(x, y) * TILESIZE
self.rect.x = x * TILESIZE
self.rect.y = y * TILESIZE
self.rotate_time = pg.time.get_ticks()
self.target = game.player
self.health = MOB_HEALTH
self.last_shot = 0
def shoot(self):
Mob_bullet(self.game)
def rotate(self,angle):
now = pg.time.get_ticks()
if now - self.rotate_time > 50:
self.rotate_time = pg.time.get_ticks()
newimage = pg.transform.rotate(self.image, angle)
oldcenter = self.rect.center
self.image = newimage
self.rect = self.image.get_rect()
self.rect.center = oldcenter
def update(self):
target_dist = self.target.pos - self.pos
if target_dist.length_squared() < DETECT_RADIUS ** 2:
now = pg.time.get_ticks()
if now - self.last_shot > MOB_BULLET_RATE:
self.last_shot = now
self.shoot()
class Mob_bullet(pg.sprite.Sprite):
def __init__(self, game):
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.Surface((10, 10))
self.image.fill(ORANGE)
self.rect = self.image.get_rect()
self.pos = vec(800, 400)
self.vel = vec(MAX_SPEED, 0).rotate(uniform(0, 180))
self.acc = vec(0, 0)
self.rect.center = self.pos
"'spawntime ist wichtig für Lifetime der Bullets!'"
self.spawn_time = pg.time.get_ticks()
def seek(self, target):
self.desired = (target - self.pos).normalize() * MAX_SPEED
steer = (self.desired - self.vel)
if steer.length() > SEEK_FORCE:
steer.scale_to_length(SEEK_FORCE)
return steer
def update(self):
"'Geschwindigkeit = suche nach(Target)'"
self.acc = self.seek(self.game.player.pos)
"'Bewegungsgleichung'"
self.vel += self.acc
if self.vel.length() > MAX_SPEED:
self.vel.scale_to_length(MAX_SPEED)
self.pos += self.vel
self.rect.center = self.pos
"'Kollission mit Walls'"
if pg.sprite.spritecollideany(self, self.game.walls):
self.kill()
"'Lebenszeit von Bullets'"
if pg.time.get_ticks() - self.spawn_time > MOB_BULLET_LIFETIME:
self.kill()
主文件的一部分:
class Game:
def new(self):
self.all_sprites = pg.sprite.Group()
.......
self.Mob_bullets = pg.sprite.Group()
设置文件的一部分:
"'Bullet-Settings'"
MOB_BULLET_LIFETIME = 5000
MOB_BULLET_RATE = 2000
MOB_BULLET_DMG = 20
MAX_SPEED = 5
SEEK_FORCE = 3
由于游戏正常,我无法提供错误消息。 我已经学习python(我的第一门编程语言)仅3个月了。请不要使用太多的技术术语;)!
还有一件事,这不是显示错误,有2条子弹(Rects)出来了!
答案 0 :(得分:0)
当事件触发时,我建议将信息打印到控制台。如果您在每次发射子弹时都打印环境变量,这可能会给您一些提示。当满足某些条件时,您还可以打印诸如“范围内的玩家”之类的消息。您可能会看到与您尝试的模式不匹配的模式,这将缩小代码的哪一部分有错误。
答案 1 :(得分:0)
发现一个错误。 map.txt文件中有拼写错误(键入错误)。 我没有敌人了,而是彼此相邻了2个!所以代码很好!