好吧,所以我正在制作一个角色在屏幕上移动并射击敌人的游戏。他在他身上加了一门大炮(一条线;))但这就是我遇到问题的地方。在班级大炮(下面)我围绕玩家旋转线,这很好。我遇到的问题是这条线在某种程度上增长了:/不肯定为什么,任何帮助都是值得赞赏的。它旋转,如果你让它绕了很多次(听起来有点幼稚,但是)
P.S(很抱歉,长码,以为我会把它包括在内。所有你真正需要的都是在大炮课下。谢谢)
import pygame, sys
from pygame.locals import *
# Classes
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, targetX, targetY):
self.x = x
self.y = y
self.targetX = targetX
self.targetY = targetY
pygame.sprite.Sprite.__init__(self, bulletGroup)
def update(self):
pygame.draw.rect(screen, (251, 231, 9), (self.x, self.y, 5, 5))
class Cannon(pygame.sprite.Sprite):
def __init__(self):
self.x = 0
self.y = -30
self.xSpeed = 10
self.ySpeed = 10
self.right = False
self.left = False
def update(self):
if self.y >= 0:
self.xSpeed = self.xSpeed * -1
print self.x
if self.right:
if self.x < 0:
self.x += self.xSpeed
self.y -= self.ySpeed
else:
self.x += self.xSpeed
self.y += self.ySpeed
if self.left:
if self.x > 0:
self.x -= self.xSpeed
self.y -= self.ySpeed
else:
self.x -= self.xSpeed
self.y += self.ySpeed
self.xSpeed = 0.5
self.ySpeed = 0.5
pygame.draw.line(screen, red, player.center, ((player.x + 14) + self.x, (player.y + 14) + self.y), 2)
class Player(pygame.sprite.Sprite):
def __init__(self, config):
self.loadConfig(config)
self.x = 250
self.y = 250
self.up = False
self.down = False
self.right = False
self.left = False
self.canShoot = True
self.canShootTicks = 0
self.shooting = False
def loadConfig(self, config):
loaded = 0
for line in config:
if "health - " in line:
self.health = int(line[9:])
loaded += 1
elif "damage - " in line:
self.damage = int(line[9:])
loaded += 1
if loaded < 2:
loaded = 0
config = open("config.txt", "w")
config = open("config.txt", "r+")
self.writeToConfig(config)
def writeToConfig(self, config):
config.write("health - 100\n")
config.write("damage - 10\n")
config.seek(0)
self.loadConfig(config)
def update(self):
if self.canShootTicks > 2:
self.canShoot = True
self.shoot()
if self.up and self.y > 20:
self.y -= 5
if self.right and self.x < 550:
self.x += 5
if self.left and self.x > 20:
self.x -= 5
if self.down and self.y < 550:
self.y += 5
self.canShootTicks += 1
self.center = (self.x + 14, self.y + 15)
screen.blit(playerImg, (self.x, self.y))
def shoot(self):
if player.canShoot:
if player.shooting:
bullet = Bullet(self.x, self.y, 10, 10)
bulletGroup.add(bullet)
self.canShootTicks = 0
self.canShoot = False
# Functions
def getResources():
wallImgTop = pygame.image.load("wallTop.png")
wallImgSide = pygame.image.load("wallSide.png")
wallImgCorner1 = pygame.image.load("wallCorner1.png")
wallImgCorner2 = pygame.image.load("wallCorner2.png")
wallImgCorner3 = pygame.image.load("wallCorner3.png")
wallImgCorner4 = pygame.image.load("wallCorner4.png")
playerImg = pygame.image.load("player.png")
return (wallImgTop, wallImgSide, wallImgCorner1, wallImgCorner2, wallImgCorner3, wallImgCorner4, playerImg)
def drawWalls():
for i in range (4):
if i == 0 or i == 2:
direction = 0
else:
direction = 580
for n in range(20):
if i < 2:
screen.blit(wallImgTop, (n * 30, direction))
else:
screen.blit(wallImgSide, (direction, n * 30))
screen.blit(wallImgCorner1, (-6, -1))
screen.blit(wallImgCorner2, (567, -6))
screen.blit(wallImgCorner3, (-1, 564))
screen.blit(wallImgCorner4, (564, 567))
def loadSave():
try:
config = open("config.txt", "r+")
except IOError:
config = open("config.txt", "w")
config = open("config.txt", "r+")
player = Player(config)
return player
# Starting out
pygame.init()
screen = pygame.display.set_mode((600, 600), 0, 32)
pygame.display.set_caption("Upgrady Shooter")
clock = pygame.time.Clock()
# Variables
FPS = 50
red = (255, 0, 0)
black = (0, 0, 0)
# Get goin'
wallImgTop, wallImgSide, wallImgCorner1, wallImgCorner2, wallImgCorner3, wallImgCorner4, playerImg = getResources()
bulletGroup = pygame.sprite.Group()
player = loadSave()
cannon = Cannon()
while True:
screen.fill(black)
drawWalls()
bulletGroup.update()
player.update()
cannon.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == K_w:
player.up = True
elif event.key == K_d:
player.right = True
elif event.key == K_a:
player.left = True
elif event.key == K_s:
player.down = True
elif event.key == K_SPACE:
player.shooting = True
elif event.key == K_LEFT:
cannon.left = True
elif event.key == K_RIGHT:
cannon.right = True
elif event.type == pygame.KEYUP:
if event.key == K_w:
player.up = False
elif event.key == K_d:
player.right = False
elif event.key == K_a:
player.left = False
elif event.key == K_s:
player.down = False
elif event.key == K_SPACE:
player.shooting = False
elif event.key == K_LEFT:
cannon.left = False
elif event.key == K_RIGHT:
cannon.right = False
pygame.display.update()
答案 0 :(得分:1)
独立职位
Player
的速度和Cannon
的速度不一样,第一个基本上是5
(例如self.x += 5
),第二个是{{ 1}}开始(10
)但后设置为self.xSpeed = 10
(0.5
)。
然后,当您使用玩家的中心作为线的起点,并包含大炮的self.xSpeed = 0.5
和x
作为终点时,线条会改变长度。
更明确地说,您的大炮绘图线是
y
这样,该行的起点为pygame.draw.line(screen, red, player.center, ((player.x + 14) + self.x, (player.y + 14) + self.y), 2)
,而终点包含player.center
和self.x
......
循环动作
为了实现圆周运动,您需要使用类似
的内容self.y
或更可能将这些放入查找表中(即,生成0到2 PI的所有x和y值)。
其他等式can also be used