gpt4 book ai didi

python - Pygame--pygame不能运行乒乓球碰撞游戏

转载 作者:行者123 更新时间:2023-11-28 18:28:54 25 4
gpt4 key购买 nike

#!/usr/bin/python
# -*- coding: utf-8 -*-
import pygame
import sys


class MyBallClass(pygame.sprite.Sprite):
def __init__(self, image_file, speed, location):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
self.speed = speed

def move(self):
global points, score_text
self.rect = self.rect.move(self.speed)
if self.rect.left < 0 or self.rect.right > screen.get_width():
self.speed[0] = -self.speed[0]

if self.rect.top <= 0:
self.speed[1] = -self.speed[1]
points += 1
score_text = font.render(str(points), 1, (0, 0, 0))


class MyPaddleClass(pygame.sprite.Sprite):
def __init__(self, location=[0, 0]):
pygame.sprite.Sprite.__init__(self)
image_surface = pygame.surface.Surface([100, 20])
image_surface.fill([0, 0, 0])
self.image = image_surface.convert()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location


pygame.init()
screen = pygame.display.set_mode([640, 480])
clock = pygame.time.Clock()
ball_speed = [3, 4]
myball = MyBallClass("E:\\python file\\blackball.jpg", ball_speed, [50, 50])
ballgroup = pygame.sprite.Group(myball)
paddle = MyPaddleClass([270, 400])
lives = 3
points = 0

font = pygame.font.Font(None, 50)
score_text = font.render(str(points), 1, (0, 0, 0))
textpos = [10, 10]
done = False

while 1:
clock.tick(30)
screen.fill([255, 255, 255])
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEMOTION:
paddle.rect.centerx = event.pos[0]

if pygame.sprite.spritecollide(paddle, ballgroup, False):
myball.speed[1] = -myball.speed[1]
myball.move()
if not done:
screen.blit(myball.image, myball.rect)
screen.blit(paddle.image, paddle.rect)
screen.blit(score_text, textpos)
for i in range(lives):
width = screen.get_width()
screen.blit(myball.image, [width - 40 * i, 20])
pygame.display.flip()
if myball.rect.top <= screen.get_rect().bottom:
# In get_rect(), you cannot leave out brackets
lives -= 1
if lives == 0:
final_text1 = "Game over!"
final_text2 = 'your final score is' + str(points)
ft1_font = pygame.font.Font(None, 70)
ft2_font = pygame.font.Font(None, 50)
ft1_surface = font.render(final_text1, 1, (0, 0, 0))
ft2_surface = font.render(final_text2, 1, (0, 0, 0))
screen.blit(ft1_surface, [screen.get_width() / 2, 100])
screen.blit(ft2_surface, [screen.get_width() / 2, 200])
pygame.display.flip()
done = True
else:
pygame.time.delay(1000)
myball.rect.topleft = [50, 50]
frame_rate = clock.get_fps()
print(frame_rate)

这是我的代码的pygame窗口图片:( http://i.stack.imgur.com/SFyBJ.jpg )

每次运行时,我都来不及控制 Racket ,然后就显示游戏结束了。我一直在寻找很长时间,但找不到原因。黑球似乎移动得如此之快,以至于游戏在大约一秒钟内就结束了。但是我已经将速度设置在合理范围内,我很困惑。谁能帮忙?

最佳答案

我可以重建您的游戏,并解决问题。

在这里试试逆条件

if myball.rect.top >= screen.get_rect().bottom:

而且效果很好:我可以用球棒击球,让它反弹,...

我不知道为什么我花了这么长时间才弄明白:如果球从底部飞出屏幕,你就输了比赛。为此,顶部 y 必须大于窗口底部(计算机屏幕坐标是从左上角开始的 0,0)

关于python - Pygame--pygame不能运行乒乓球碰撞游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39051973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com