在 youtube 上关注 Pygame/Python 教程时出现语法错误 [英] Syntax Error When Following Pygame/Python Tutorial on youtube

查看:58
本文介绍了在 youtube 上关注 Pygame/Python 教程时出现语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚下载了 python 3.3.2 和 pygame-1.9.2a0.win32-py3.3.msi.我决定在 youtube 上尝试一些教程,看看它们是否有效.

I've just downloaded python 3.3.2 and pygame-1.9.2a0.win32-py3.3.msi. I have decided to try a few tutorials on youtube and see if they work.

我已经尝试过 thenewboston 的游戏开发教程 - 2 - 基本 Pygame 程序",看看它是否有效.它应该产生一个黑色背景和一个鼠标球(或者我认为).当我尝试运行它时出现语法错误,如果我删除它,它只会产生一个黑色的 pygame 窗口.代码如下:

I have tried thenewboston's 'Game Development Tutorial - 2 - Basic Pygame Program' to see if it works. It is supposed to produce a black background and a ball that is the mouse (or so i think). It comes up with a syntax error when i try to run it, if i delete it it just produces a black pygame window. Here is the code:

bgg="bg.jpg"
ball="ball.png"

import pygame, sys
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((540,341),0,32)

background=pygame.image.load(bgg).convert()
mouse_c=pygame.image.load(ball).convert_alpha()

while True:
    for event in pygame.event.get():
        if event.type ==QUIT:
            pygame.quit()
            sys.exit()

    screen.blit(background), (0,0))

screen.blit(bakcgorund, (0,0)) 命令是问题所在,当它出现语法错误时,它会突出显示命令最右侧的第二个括号.如果我删除它,它只会显示一个黑色的 pygame 窗口.有人可以帮我吗?

The screen.blit(bakcgorund, (0,0)) command is the problem, when it comes up with the syntax error it highlights the second bracket on the furthest right of the command. If I delete it it just shows a black pygame window. can anyone help me?

推荐答案

我更新了你的代码:

import pygame
from pygame.locals import * 
#about: pygame boilerplate

class GameMain():
    # handles intialization of game and graphics, as well as game loop
    done = False

    def __init__(self, width=800, height=600):
        """Initialize PyGame window.

        variables:
            width, height = screen width, height
            screen = main video surface, to draw on

            fps_max = framerate limit to the max fps
            limit_fps = boolean toggles capping FPS, to share cpu, or let it run free.
            now = current time in Milliseconds. ( 1000ms = 1second)
        """
        pygame.init()

        # save w, h, and screen
        self.width, self.height = width, height
        self.screen = pygame.display.set_mode(( self.width, self.height ))
        pygame.display.set_caption( "pygame tutorial code" )        

        self.sprite_bg = pygame.image.load("bg.jpg").convert()
        self.sprite_ball = pygame.image.load("ball.png").convert_alpha()


    def main_loop(self):
        """Game() main loop."""
        while not self.done:
            self.handle_events()        
            self.update()
            self.draw()

    def draw(self):
        """draw screen"""
        self.screen.fill(Color('darkgrey'))

        # draw your stuff here. sprites, gui, etc....        
        self.screen.blit(self.sprite_bg, (0,0))
        self.screen.blit(self.sprite_ball, (100,100))


        pygame.display.flip()

    def update(self):
        """physics/move guys."""
        pass

    def handle_events(self):
        """handle events: keyboard, mouse, etc."""
        events = pygame.event.get()
        kmods = pygame.key.get_mods()

        for event in events:
            if event.type == pygame.QUIT:
                self.done = True
            # event: keydown
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE: self.done = True

if __name__ == "__main__":         
    game = GameMain()
    game.main_loop()    

这篇关于在 youtube 上关注 Pygame/Python 教程时出现语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆