Python/Pygame 游戏循环问题/让代码运行

Posted

技术标签:

【中文标题】Python/Pygame 游戏循环问题/让代码运行【英文标题】:Python/Pygame Trouble with Game loop/Getting code to run 【发布时间】:2015-04-25 14:22:52 【问题描述】:

在过去的几天里,我一直在为 pong 编写这段代码,并且几乎可以运行它。一切都取决于物体的运动。当 pygame 窗口打开时,屏幕会冻结。我的 main() 似乎有问题,但我对 python/pygame 还是很陌生,不知道哪里出了问题。

import pygame, sys
pygame.init()

SCR_WID, SCR_HEI = 640, 480
class Player():
    def __init__(self):
        self.x, self.y = 16, SCR_HEI/2
        self.x1, self.y1 = SCR_WID-16, SCR_HEI/2
        self.speed = 3
        self.padWid, self.padHei = 8, 64
        self.score = 0
        self.scoreFont = pygame.font.Font("imagine_font.ttf", 64)

    def scoring(self):
            scoreBlit = self.scoreFont.render(str(self.score), 1, (0,0,0))
            screen.blit(scoreBlit, (32, 16))
            if self.score == 10:
                print ("player 1 wins!")
                exit()

    def scoring1(self):
            scoreBlit = self.scoreFont.render(str(self.score), 1, (0,0,0))
            screen.blit(scoreBlit, (SCR_HEI+92, 16))
            if self.score == 10:
                print ("Player 2 wins!")
                exit()

    def movement(self):
            keys = pygame.key.get_pressed()
            if keys[pygame.K_w]:
                self.y -= self.speed
            elif keys[pygame.K_s]:
                self.y += self.speed

            if self.y <= 0:
                self.y = 0
            elif self.y >= SCR_HEI-64:
                self.y = SCR_HEI-64

            keys1 = pygame.key.get_pressed()
            if keys1[pygame.K_UP]:
                self.y1 -= self.speed
            elif keys1[pygame.K_DOWN]:
                self.y1 += self.speed

            if self.y1 <= 0:
                self.y1 = 0
            elif self.y >= SCR_HEI-64:
                self.y1 = SCR_HEI-64

    def draw(self):
        pygame.draw.rect(screen, (0,0,0), (self.x, self.y, self.padWid, self.padHei))
        pygame.draw.rect(screen, (0,0,0), (self.x1, self.y1, self.padWid, self.padHei))



class Ball():
    def __init__(self):
        self.x, self.y = SCR_WID/2, SCR_HEI/2
        self.x1, self.y1 = SCR_WID/2, SCR_HEI/2
        self.speed_x = -3
        self.speed_y = 3
        self.size = 8

    def movement(self):
        self.x += self.speed_x
        self.y += self.speed_y

        if self.y <= 0:
            self.speed_y *= -1
        elif self.y >= SCR_HEI-self.size:
            self.speed_y *= -1

        if self.x <= 0:
            self.__init__()
            enemy.score += 1
        elif self.x >= SCR_WID-self.size:
            self.__init__()
            self.speed_x = 3
            player.score += 1
        for n in range(-self.size, player.padHei):
            if self.y == player.y + n:
                    if self.x <= player.x + player.padWid:
                        self.speed_x *= -1
                        break
            n += 1
        self.x1 += self.speed_x
        self.y1 += self.speed_y

        if self.y1 <= 0:
            self.speed_y *= -1
        elif self.y1 >= SCR_HEI-self.size:
            self.speed_y *= -1

        if self.x1 <= 0:
            self.__init__()
            enemy.score += 1
        elif self.x1 >= SCR_WID-self.size:
            self.__init__()
            self.speed_x = 3
            player.score += 1
        for n in range(-self.size, enemy.padHei):
            if self.y1 == enemy.y1 + n:
                    if self.x1 >= enemy.x1 - enemy.padWid:
                        self.speed_x *= -1
                        break
            n += 1

    def draw(self):
            pygame.draw.rect(screen, (112,138,144), (self.x, self.y, 8, 8))

SCR_WID, SCR_HEI = 640, 480
screen = pygame.display.set_mode((SCR_WID, SCR_HEI))
player = Player() 
ball = Ball()
enemy = Player()


def main():
    running = True
    while running == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print ("Game exited by user")
                exit()
            else:
                running = False
    pygame.font.init()
    clock = pygame.time.Clock()
    FPS = 60
    backgroundimage = pygame.image.load("background.png")
    pygame.display.set_caption("Justin's pong")
    ball.movement()
    player.movement()
    enemy.movement()
    position = (0,0)
    screen.fill((0, 0, 0))
    screen.blit(backgroundimage, position)
    ball.draw()
    player.draw()
    player.scoring()
    enemy.draw()
    enemy.scoring1()
    pygame.display.flip()
    clock.tick(FPS)

main()

【问题讨论】:

【参考方案1】:

问题是这段代码:

running = True
while running == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print ("Game exited by user")
            exit()
        else:
            running = False

您正确获取了事件,但如果没有任何事件(因为一开始没有),您将永远运行,检查事件。

我假设您希望其余代码也在 while 循环中运行,因此您应该缩进它。

【讨论】:

哇,这似乎几乎完全解决了我的问题!现在,当我运行代码时,游戏运行,但它运行的唯一方法是我的鼠标在 pygame 窗口上不断移动。有点奇怪。有什么想法吗? @JustinFarr 去掉运行变量,让循环为真 没关系,我只是解决了我自己的问题。非常感谢您的帮助!

以上是关于Python/Pygame 游戏循环问题/让代码运行的主要内容,如果未能解决你的问题,请参考以下文章

实战用Python+Pygame+Kivy(Buildozer)+Ubuntu开发安卓android手机端apk游戏及踩坑分享

蹲在python平台游戏中与pygame

打飞机小游戏 python+pygame

python pygame 游戏实战:Maze 迷宫生成,显示和游戏(附全部代码)

Python Pygame制作简单五子棋游戏(详细代码+解释)

推箱子游戏不会推动新的水平 - Python - Pygame