- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试用 pygame 制作一款足球游戏,几乎成功完成,但是,我有一些问题。我试图在进球后移动球。我尝试将球移动到 ball_x 和 ball_y 坐标,但它不起作用。我的代码如下,我真的希望有人可以编辑我的代码,使其正常工作。
import pygame
import pygame as pg
from pygame.math import Vector2
pygame.init()
WIDTH = 1150
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
# Images.
bgImg = pygame.image.load("Football_pitch.png")
REDGOAL = pg.Surface((50, 150), pg.SRCALPHA)
REDGOAL.fill((255, 0, 0))
redgoal_rect = REDGOAL.get_rect(topleft=(0, 340))
redgoal_mask = pg.mask.from_surface(REDGOAL)
BLUEGOAL = pg.Surface((50, 150), pg.SRCALPHA)
BLUEGOAL.fill((0, 0, 255))
bluegoal_rect = REDGOAL.get_rect(topleft=(1100, 340))
bluegoal_mask = pg.mask.from_surface(REDGOAL)
BLUECAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
BLUECAR_ORIGINAL, (0, 0, 255), [(0, 30), (50, 20), (50,10), (0, 0)])
bluecar = BLUECAR_ORIGINAL
REDCAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
REDCAR_ORIGINAL, (255, 0, 0), [(0, 0), (50, 10), (50, 20), (0, 30)])
redcar = REDCAR_ORIGINAL
score = 0
redspeed = 7
bluespeed = 7
ball_x = 575
ball_y = 400
dx = 0
dy = 0
x = 800
y = 500
BALL = pygame.Surface((30, 30), pygame.SRCALPHA)
pygame.draw.circle(BALL, [0,0,0], [15, 15], 15)
# Ball variables.
ball_pos = Vector2(ball_x, ball_y)
ballrect = BALL.get_rect(center=ball_pos)
ball_vel = Vector2(dx, dy)
ball_mask = pg.mask.from_surface(BALL)
# Car variables.
pos_red = Vector2(x,y)
vel_red = Vector2(redspeed,0)
redrect = redcar.get_rect(center=pos_red)
redangle = 180
pos_blue = Vector2(275,300)
vel_blue = Vector2(bluespeed,0)
bluerect = bluecar.get_rect(center=pos_red)
blueangle = 0
# Masks.
mask_blue = pygame.mask.from_surface(bluecar)
mask_red = pygame.mask.from_surface(redcar)
mask_ball = pygame.mask.from_surface(BALL)
ballrect = BALL.get_rect(center=ball_pos)
vel_red.rotate_ip(-180)
def redgoal():
print("Red Goal!!!")
ball_x = 300
ball_y = 300
def bluegoal():
print("Blue Goal!!!")
boostedspeedred = 10
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if y <0:
y = 10
if y > 450:
y = 440
if x > 480:
x = 470
if ballrect.top < 0 and ball_vel.y < 0:
ball_vel.y *= -1
elif ballrect.bottom > screen.get_height() and ball_vel.y > 0:
ball_vel.y *= -1
if ballrect.left < 0 and ball_vel.x < 0:
ball_vel.x *= -1
elif ballrect.right > screen.get_width() and ball_vel.x > 0:
ball_vel.x *= -1
if redrect.top < 0 and redrect.y < 0:
redrect.y *= +10
elif redrect.bottom > screen.get_height() and redrect.y > 0:
redrect.y *= -10
if redrect.left < 0 and redrect.x < 0:
redrect.x *= -10
elif redrect.right > screen.get_width() and redrect.x > 0:
redrect.x *= -10
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
redangle += 5
vel_red.rotate_ip(-5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redrect = redcar.get_rect(center=redrect.center)
# We need a new mask after the rotation.
mask_red = pygame.mask.from_surface(redcar)
elif keys[pygame.K_RIGHT]:
redangle -= 5
vel_red.rotate_ip(5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redrect = redcar.get_rect(center=redrect.center)
mask_red = pygame.mask.from_surface(redcar)
elif keys[pygame.K_UP]:
redspeed == 10
if keys[pygame.K_a]:
blueangle += 5
vel_blue.rotate_ip(-5)
bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
bluerect = bluecar.get_rect(center=bluerect.center)
mask_blue = pygame.mask.from_surface(bluecar)
elif keys[pygame.K_d]:
blueangle -= 5
vel_blue.rotate_ip(5)
bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
bluerect = bluecar.get_rect(center=bluerect.center)
mask_blue = pygame.mask.from_surface(bluecar)
# Move the cars.
pos_red += vel_red
redrect.center = pos_red
pos_blue += vel_blue
bluerect.center = pos_blue
# Move the ball.
ball_vel *= .99 # Friction.
ball_pos += ball_vel
ballrect.center = ball_pos
# Red car collision.
# We need the offset between the redrect and the ballrect.
offset_red = redrect[0] - ballrect[0], redrect[1] - ballrect[1]
# Pass the offset to the `overlap` method. If the masks collide,
# overlap will return a single point, otherwise `None`.
overlap_red = mask_ball.overlap(mask_red, offset_red)
# Blue car collision.
offset_blue = bluerect[0] - ballrect[0], bluerect[1] - ballrect[1]
overlap_blue = mask_ball.overlap(mask_blue, offset_blue)
offset = redgoal_rect[0] - ballrect[0], redgoal_rect[1] - ballrect[1]
redgoaloverlap = ball_mask.overlap(redgoal_mask, offset)
offset = bluegoal_rect[0] - ballrect[0], bluegoal_rect[1] - ballrect[1]
bluegoaloverlap = ball_mask.overlap(bluegoal_mask, offset)
if redgoaloverlap:
redgoal()
if bluegoaloverlap:
bluegoal()
if overlap_red and overlap_blue: # Both collide with the ball.
# Not sure what should happen here.
ball_vel = vel_red + vel_blue * 1.4
elif overlap_red: # Red collides with the ball.
ball_vel = Vector2(vel_red) * 1.4
elif overlap_blue: # Blue collides with the ball.
ball_vel = Vector2(vel_blue) * 1.4
# Drawing.
screen.blit(bgImg, (0, 0))
screen.blit(BALL, ballrect)
screen.blit(redcar, redrect)
screen.blit(bluecar, bluerect)
screen.blit(REDGOAL, redgoal_rect)
screen.blit(BLUEGOAL, bluegoal_rect)
pygame.display.flip()
pygame.display.update()
clock.tick(60)
pygame.quit()
最佳答案
问题是您正在设置 ball_x
和 ball_y
,但球并未在这些位置绘制。球实际上是在名为 ball_pos
的向量中的坐标处绘制的。
要解决此问题:
ball_pos
即可。ball_vel
,因此球再次移动到中心后不再移动。您可能还想添加重置汽车位置的代码。此外,只有出现“红色球门”时,球才会重置。
这是固定代码:
import pygame
import pygame as pg
from pygame.math import Vector2
pygame.init()
WIDTH = 1150
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
# Images.
bgImg = pygame.image.load("Football_pitch.png")
REDGOAL = pg.Surface((50, 150), pg.SRCALPHA)
REDGOAL.fill((255, 0, 0))
redgoal_rect = REDGOAL.get_rect(topleft=(0, 340))
redgoal_mask = pg.mask.from_surface(REDGOAL)
BLUEGOAL = pg.Surface((50, 150), pg.SRCALPHA)
BLUEGOAL.fill((0, 0, 255))
bluegoal_rect = REDGOAL.get_rect(topleft=(1100, 340))
bluegoal_mask = pg.mask.from_surface(REDGOAL)
BLUECAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
BLUECAR_ORIGINAL, (0, 0, 255), [(0, 30), (50, 20), (50,10), (0, 0)])
bluecar = BLUECAR_ORIGINAL
REDCAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
REDCAR_ORIGINAL, (255, 0, 0), [(0, 0), (50, 10), (50, 20), (0, 30)])
redcar = REDCAR_ORIGINAL
score = 0
redspeed = 7
bluespeed = 7
ball_x = 575
ball_y = 400
dx = 0
dy = 0
x = 800
y = 500
BALL = pygame.Surface((30, 30), pygame.SRCALPHA)
pygame.draw.circle(BALL, [0,0,0], [15, 15], 15)
# Ball variables.
ball_pos = Vector2(ball_x, ball_y)
ballrect = BALL.get_rect(center=ball_pos)
ball_vel = Vector2(dx, dy)
ball_mask = pg.mask.from_surface(BALL)
# Car variables.
pos_red = Vector2(x,y)
vel_red = Vector2(redspeed,0)
redrect = redcar.get_rect(center=pos_red)
redangle = 180
pos_blue = Vector2(275,300)
vel_blue = Vector2(bluespeed,0)
bluerect = bluecar.get_rect(center=pos_red)
blueangle = 0
# Masks.
mask_blue = pygame.mask.from_surface(bluecar)
mask_red = pygame.mask.from_surface(redcar)
mask_ball = pygame.mask.from_surface(BALL)
ballrect = BALL.get_rect(center=ball_pos)
vel_red.rotate_ip(-180)
def redgoal():
print("Red Goal!!!")
ball_vel.x = 0
ball_vel.y = 0
ball_pos.x = 575
ball_pos.y = 400
def bluegoal():
print("Blue Goal!!!")
boostedspeedred = 10
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if y <0:
y = 10
if y > 450:
y = 440
if x > 480:
x = 470
if ballrect.top < 0 and ball_vel.y < 0:
ball_vel.y *= -1
elif ballrect.bottom > screen.get_height() and ball_vel.y > 0:
ball_vel.y *= -1
if ballrect.left < 0 and ball_vel.x < 0:
ball_vel.x *= -1
elif ballrect.right > screen.get_width() and ball_vel.x > 0:
ball_vel.x *= -1
if redrect.top < 0 and redrect.y < 0:
redrect.y *= +10
elif redrect.bottom > screen.get_height() and redrect.y > 0:
redrect.y *= -10
if redrect.left < 0 and redrect.x < 0:
redrect.x *= -10
elif redrect.right > screen.get_width() and redrect.x > 0:
redrect.x *= -10
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
redangle += 5
vel_red.rotate_ip(-5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redrect = redcar.get_rect(center=redrect.center)
# We need a new mask after the rotation.
mask_red = pygame.mask.from_surface(redcar)
elif keys[pygame.K_RIGHT]:
redangle -= 5
vel_red.rotate_ip(5)
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redrect = redcar.get_rect(center=redrect.center)
mask_red = pygame.mask.from_surface(redcar)
elif keys[pygame.K_UP]:
redspeed == 10
if keys[pygame.K_a]:
blueangle += 5
vel_blue.rotate_ip(-5)
bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
bluerect = bluecar.get_rect(center=bluerect.center)
mask_blue = pygame.mask.from_surface(bluecar)
elif keys[pygame.K_d]:
blueangle -= 5
vel_blue.rotate_ip(5)
bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
bluerect = bluecar.get_rect(center=bluerect.center)
mask_blue = pygame.mask.from_surface(bluecar)
# Move the cars.
pos_red += vel_red
redrect.center = pos_red
pos_blue += vel_blue
bluerect.center = pos_blue
# Move the ball.
ball_vel *= .99 # Friction.
ball_pos += ball_vel
ballrect.center = ball_pos
# Red car collision.
# We need the offset between the redrect and the ballrect.
offset_red = redrect[0] - ballrect[0], redrect[1] - ballrect[1]
# Pass the offset to the `overlap` method. If the masks collide,
# overlap will return a single point, otherwise `None`.
overlap_red = mask_ball.overlap(mask_red, offset_red)
# Blue car collision.
offset_blue = bluerect[0] - ballrect[0], bluerect[1] - ballrect[1]
overlap_blue = mask_ball.overlap(mask_blue, offset_blue)
offset = redgoal_rect[0] - ballrect[0], redgoal_rect[1] - ballrect[1]
redgoaloverlap = ball_mask.overlap(redgoal_mask, offset)
offset = bluegoal_rect[0] - ballrect[0], bluegoal_rect[1] - ballrect[1]
bluegoaloverlap = ball_mask.overlap(bluegoal_mask, offset)
if redgoaloverlap:
redgoal()
if bluegoaloverlap:
bluegoal()
if overlap_red and overlap_blue: # Both collide with the ball.
# Not sure what should happen here.
ball_vel = vel_red + vel_blue * 1.4
elif overlap_red: # Red collides with the ball.
ball_vel = Vector2(vel_red) * 1.4
elif overlap_blue: # Blue collides with the ball.
ball_vel = Vector2(vel_blue) * 1.4
# Drawing.
screen.blit(bgImg, (0, 0))
screen.blit(BALL, ballrect)
screen.blit(redcar, redrect)
screen.blit(bluecar, bluerect)
screen.blit(REDGOAL, redgoal_rect)
screen.blit(BLUEGOAL, bluegoal_rect)
pygame.display.flip()
pygame.display.update()
clock.tick(60)
pygame.quit()
关于python - Pygame 在事件发生后移动 Sprite 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51810316/
任何人都可以评论是否对图像使用 Sprite 的决定?我看到以下好处/权衡(其中一些可以减轻): 单个图像上的 Sprite 优点: 需要管理的图像更少 更容易实现主题图像 图像交换 (JS/CSS)
所以,我一直在 Unity 工作,但我决定是时候换成我更了解的东西了:JavaScript。我一直在考虑切换到 Phaser.js,但我有一些关于我什至在 Unity 中遇到的问题的问题,而且我在互联
所以我有一个木偶要在 Sprite Kit 中显示。木偶是由一堆不同的 body 部位组成的,当然每个部位都是一个.png。 所以我的过程是:我有一个 Marionette 对象(SKNode 子类)
我有一个 Sprite ,我将其初始化为 SKSPriteNode,它不断地从另一个 Sprite 上弹起,也以相同的方式初始化。 我无法弄清楚两者之间的冲突,并且到目前为止堆栈上没有任何帮助。 我将
标题说明了一切。我想知道 Sprite.getcontentsize、Sprite.gettexture、Sprite.getscale 之间有什么区别。以及它们是如何使用的。在这个问题之后我找不到任
我有兴趣尝试创建一些游戏,即在新的 sprite 工具包中。但是,对于我心目中的游戏,我宁愿使用方向键而不是操纵杆。因为我将从 Cocos 搬过来,所以我的旧程序不再有效(所以那个 dpad 也不会)
我正在 LibGdx 中开发 2D 射击游戏。 我不得不提一下,我是 LibGdx 的新手,我正在努力理解它是如何工作的。我有几年的Java和Android编程经验,所以我了解游戏概念。 我感兴趣的是
我正在使用 Compass 生成 CSS Sprite 。 我找到了一种方法来定义一次 Sprite 并在不同的 .scss 文件中使用它,但我不确定这是正确的解决方案。 到目前为止,我能找到的最好方
我在游戏中遇到背景音乐问题。当我从主菜单场景切换到游戏场景时,它停止,但是当游戏场景切换到gameOver场景时,它不停止。当我选择重播时,音乐也会重叠(从gameOver场景切换回游戏场景)。 要播
我是一名使用 libgdx 引擎的新程序员,想知道 Sprite 批处理的行为。特别是如何在程序生命周期中将 Sprite 添加到批处理中以进行绘制。到目前为止, Sprite 的所有示例都使用了一些
这可能是个愚蠢的问题,但如果有人能帮助我,我将不胜感激。 我有一个由 3 个垂直堆叠的不同图像组成的 Sprite ,但我试图让中间的图像(高度为 1px 和宽度为 194)重复,只是那条 1px 的
我正在尝试为我正在构建的菜单加载 spritesheet,但它不是一次显示一个图像,而是在元素的不同位置显示整个 spritesheet。 这是我使用两张图片的 CSS 代码: #mymenu ul.
我有两个 Sprite 组,ship_list 有 20 个飞船 Sprite ,all_sprites 有这 20 个 Sprite ,加上玩家 Sprite 。在主循环中,当检测到玩家与 ship
我制作了这个我可以抓取并四处移动的 Sprite 。我的问题是我希望能够“抛出” Sprite 。意思是,当我释放 Sprite 时,我希望它继续沿着我移动它的方向前进。就像扔球一样。 我该怎么办?
我目前正在开发 HTML/CSS 模板,我将实现以下社交媒体图标: http://www.premiumpixels.com/freebies/41-social-media-icons-png/ 它
在我的游戏中,我希望能够收集硬币。我有一个该硬币 Sprite 的数组列表,这样我就可以单独绘制多个硬币。这些硬币也随着背景移动(模拟汽车驾驶),我想要它,所以当硬币撞到汽车时,它会消失并被收集。感谢
我是 cocos2D 的新手,谁能提出一个简单的解决方案? 我有一个风车,风车上有 8 个条,每个条以 45 度隔开,其中风车的一半在屏幕上,一半在屏幕外。我想旋转风车,让它永远旋转。我还想在风车杆的
我使用的是主播中心分支,刚刚发现我的游戏出现的问题大部分都是这个原因。有没有一种方法可以使用 Sprite 的左下角而不是使用 (0, 0) 作为其中心来设置 Sprite ? 谢谢! 最佳答案 发现
我是 magic-importing我的 Sprite : // Creating a concatenated sprite image out of all sprites in the "/im
这个问题在这里已经有了答案: How do I detect collision in pygame? (5 个答案) 关闭去年。 想要创建一个包含 10 张图像的组。稍后屏幕上的图像不应重叠。我尝
我是一名优秀的程序员,十分优秀!