python - PyGame 运动和射击
问题描述
我目前正在尝试用 Python 制作我的第一款游戏。我刚刚完成了这个动作,但是当我的角色直接跳起来时,他就消失了。我也试图让我的角色射击子弹,但它不会出现。不知道改哪个部分。因为我还在学习,所以我自己无法解决这个问题。这是我的代码:
import pygame
from pygame.constants import K_SPACE
pygame.init()
win = pygame.display.set_mode((500, 480))
WinHeight = 480
WinWidth = 500
# player
char = pygame.image.load('standing.png')
# Player movement
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')]
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')]
# title
pygame.display.set_caption("First Game")
# background
bg = pygame.image.load('bg.jpg')
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.walkCount = 0
self.isJump = False
self.jumpCount = 10
self.standing = True
def Draw(self, win):
if not (self.standing):
if self.walkCount + 1 >= 27:
self.walkCount = 0
if self.left:
win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
else:
if self.right:
win.blit(walkRight[0], (self.x, self.y))
else:
win.blit(walkLeft[0], (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, win):
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius )
# functions
def redrawGameWindow():
win.blit(bg,(0,0))
man.Draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
clock = pygame.time.Clock()
running = True
man = Player(50, 400, 64, 64)
bullets = []
while running:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
for bullet in bullets:
if bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
if man.left:
facing = -1
else:
facing = 1
if len(bullets) > 5:
bullets.append(Projectile(round(man.x + man.width //2)), round(man.y + man.height), 30,(138,43,226), facing)
if keys[pygame.K_a] and man.x > man.vel:
man.x -= man.vel
man.left = True
man.right = False
man.standing = False
elif keys[pygame.K_d] and man.x < WinWidth - man.vel - man.width:
man.x += man.vel
man.right = True
man.left = False
man.standing = False
else:
man.walkCount = 0
if not (man.isJump):
if keys[pygame.K_w]:
man.isJump = True
man.right = False
man.left = False
man.walkCount = 0
else:
if man.jumpCount >= -10:
neg = 1
if man.jumpCount < 0:
neg = -1
man.y -= (man.jumpCount ** 2) * 0.5 * neg
man.jumpCount -= 1
else:
man.isJump = False
man.jumpCount = 10
redrawGameWindow()
解决方案
[..] 当我的角色直接跳起来时,他就消失了。
您错过了初始化man.jumpCount = 10
:
while running:
# [...]
if not (man.isJump):
if keys[pygame.K_w]:
man.isJump = True
man.right = False
man.left = False
man.walkCount = 0
man.jumpCount = 10 # <-- this is missing in your code
else:
if man.jumpCount >= -10:
neg = 1
if man.jumpCount < 0:
neg = -1
man.y -= (man.jumpCount ** 2) * 0.5 * neg
man.jumpCount -= 1
else:
man.isJump = False
man.jumpCount = 10
# [...]
我也试图让我的角色射击子弹,但它不会出现。
条件len(bullets) > 5
永远不会满足。它必须是len(bullets) < 5
。另外,创建新Projectile
对象时括号设置不正确:
while running:
# [...]
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
facing = -1 if man.left else 1
if len(bullets) < 5:
bullets.append(Projectile(round(man.x + man.width //2), round(man.y + man.height), 30,(138,43,226), facing))
如果您只想发射 1 发子弹Projectile
,请阅读如何一次停止超过 1 发子弹发射?.
完整示例:
import pygame
from pygame.constants import K_SPACE
pygame.init()
win = pygame.display.set_mode((500, 480))
WinHeight = 480
WinWidth = 500
# player
char = pygame.image.load('standing.png')
# Player movement
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')]
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')]
# title
pygame.display.set_caption("First Game")
# background
bg = pygame.image.load('bg.jpg')
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.walkCount = 0
self.isJump = False
self.jumpCount = 10
self.standing = True
def Draw(self, win):
if not (self.standing):
if self.walkCount + 1 >= 27:
self.walkCount = 0
if self.left:
win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
else:
if self.right:
win.blit(walkRight[0], (self.x, self.y))
else:
win.blit(walkLeft[0], (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, win):
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius )
# functions
def redrawGameWindow():
win.blit(bg,(0,0))
man.Draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
clock = pygame.time.Clock()
running = True
man = Player(50, 400, 64, 64)
bullets = []
while running:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
for bullet in bullets:
if bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
facing = -1 if man.left else 1
if len(bullets) < 5:
bullets.append(Projectile(round(man.x + man.width //2), round(man.y + man.height), 30,(138,43,226), facing))
if keys[pygame.K_a] and man.x > man.vel:
man.x -= man.vel
man.left = True
man.right = False
man.standing = False
elif keys[pygame.K_d] and man.x < WinWidth - man.vel - man.width:
man.x += man.vel
man.right = True
man.left = False
man.standing = False
else:
man.walkCount = 0
if not (man.isJump):
if keys[pygame.K_w]:
man.isJump = True
man.right = False
man.left = False
man.walkCount = 0
man.jumpCount = 10
else:
if man.jumpCount >= -10:
neg = 1
if man.jumpCount < 0:
neg = -1
man.y -= (man.jumpCount ** 2) * 0.5 * neg
man.jumpCount -= 1
else:
man.isJump = False
man.jumpCount = 10
redrawGameWindow()
推荐阅读
- java - 用 C++、C#、Java 等编写的桌面 GUI(图形用户界面)是否像所有 Web 或移动 GUI 一样使用 DOM 或类似概念?
- javascript - 在父元素的动画完成后显示一个元素
- json - 如何使用 spark/scala 将 json 字符串格式化为 MongoDB 文档样式?
- mysql - SUM 函数在 MySQL 存储函数中不起作用
- sql-server - 'RawQuerySet' 对象没有属性 'distinct'
- codeigniter - 谷歌搜索上显示的机密数据。如何禁用?
- python - 清空队列时遇到问题
- android - 空对象引用上的“Session.getConfig()”
- java - 如果用户需要,在运行时在 SDK 中添加与之相关的类(代码)和依赖项,如果没有,则不要添加,在 android 中是否可以?
- sql - SQL 将所有事件放入 3 个月的存储桶中