矩形不会在pygame中移动

时间:2016-08-23 04:59:58

标签: python-3.x pygame

我已经尝试了其他网站所说的移动东西,但我的工作不应该像它应该的那样。我正在尝试创建一个 Stacker 游戏。

一旦用户按下左箭头键或右箭头键,我希望所有3个块都移动。我正在使用can't assign to operator.-syntax error。一旦我先把它移动,我会尝试在以后获得界限。当我运行它时,它会显示import math import random import time import pygame import sys import glob pygame.init() move=0 fps=60 blue=(0,0,255) white=(255,255,255) black=(0,0,0) green=(0,155,0) display_width=800 display_height=600 gamedisplay=pygame.display.set_mode((display_width,display_height)) pygame.display.set_caption('Stacker') clock=pygame.time.Clock() speed=0 smallfont=pygame.font.SysFont("Arial",int(display_width/32)) mediumfont=pygame.font.SysFont("Arial",int(display_width/16)) largefont=pygame.font.SysFont("Arial",int(display_width/10)) gamedisplay.fill(green) block=display_height/12 pygame.display.update() def quit_game(): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() quit() def intro_screen(): welcome_message = mediumfont.render(str("Welcome to Stacker!!!"), True,black) gamedisplay.blit(welcome_message,(display_width/4,display_height/40)) how_to_play_1=smallfont.render(str("Your goal is to get to the major prize bar"),True,black) gamedisplay.blit(how_to_play_1,(display_width/3.619909502,display_height/2)) how_to_play=smallfont.render(str("Press the space bar to stop the shape"),True,black) gamedisplay.blit(how_to_play,(display_width/3.48583878,display_height/(12/7))) pygame.display.update() def middle_block(): pygame.draw.rect(gamedisplay, blue,(display_width/(32/15),display_height-block,block,block)) pygame.display.update() def left_block(): pygame.draw.rect(gamedisplay, blue,(display_width/(32/13),display_height-block,block,block)) pygame.display.update() def right_block(): pygame.draw.rect(gamedisplay, blue,(display_width/(32/17),display_height-block,block,block)) pygame.display.update() def major_screen(): major_message = mediumfont.render(str("Major Prize Here!"), True,black) gamedisplay.blit(major_message,(display_width/(10/3),display_height/40)) pygame.display.update() def main_loop(): leadx=300 leady=300 clock.tick(fps) intro_screen() pygame.time.delay(8000) gamedisplay.fill(green) major_screen() pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/8,display_width,display_height/60)) pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/2.4,display_width,display_height/60)) middle_block() left_block() right_block() pygame.display.update() gameexit=False while not gameexit: for event in pygame.event.get(): if event.type==pygame.QUIT: gameexit=True if event.type==pygame.KEYDOWN: if event.key==pygame.K_LEFT: middle_block() and right_block() and left_block() -= 10 if event.key==pygame.K_RIGHT: middle_block() and right_block() and left_block() += 10 if event.type==pygame.KEYUP: if event.key==pygame.K_LEFT: middle_block() and right_block() and left_block() -= 10 if event.key==pygame.K_RIGHT: middle_block() and right_block() and left_block() += 10 pygame.display.update() pygame.display.update() pygame.display.update() pygame.quit() quit() main_loop() 。这是我的代码。

add_definitions(-D_XOPEN_SOURCE=700)

1 个答案:

答案 0 :(得分:0)

您遇到语法错误。它指的是哪一行? 我也不明白为什么你的代码会起作用。如何从绘制矩形的块函数中减去10? block_left()+ = 10。

我会为每个块定义矩形

left_block = pygame.Rect(display_width/(32/15),display_height-block,block,block)
while not gameexit:
    for event in pygame.event.get():
        if event.type==pygame.KEYDOWN:
           if event.key==pygame.K_LEFT:
              left_block.top -= 10
    pygame.draw.rect(gamedisplay, blue, left_block)
    pygame.display.update()

我编辑了你的代码以使用一个块。绘制所有内容,然后只更新一次屏幕。

import math
import random
import time
import pygame
import sys
import glob
pygame.init()
move=0
fps=60
blue=(0,0,255)
white=(255,255,255)
black=(0,0,0)
green=(0,155,0)
display_width=800
display_height=600
gamedisplay=pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Stacker')
clock=pygame.time.Clock()
speed=0
smallfont=pygame.font.SysFont("Arial",int(display_width/32))
mediumfont=pygame.font.SysFont("Arial",int(display_width/16))
largefont=pygame.font.SysFont("Arial",int(display_width/10))
gamedisplay.fill(green)
block=display_height/12
pygame.display.update()

def intro_screen():
    welcome_message = mediumfont.render(str("Welcome to Stacker!!!"), True,black)
    gamedisplay.blit(welcome_message,(display_width/4,display_height/40))
    how_to_play_1=smallfont.render(str("Your goal is to get to the major prize bar"),True,black)
    gamedisplay.blit(how_to_play_1,(display_width/3.619909502,display_height/2))
    how_to_play=smallfont.render(str("Press the space bar to stop the shape"),True,black)
    gamedisplay.blit(how_to_play,(display_width/3.48583878,display_height/(12/7)))
    pygame.display.update()

left_block = pygame.Rect(display_width/(32/13),display_height-block,block,block)

def major_screen():
    major_message = mediumfont.render(str("Major Prize Here!"), True,black)
    gamedisplay.blit(major_message,(display_width/(10/3),display_height/40))

def main_loop():
    leadx=300
    leady=300
    clock.tick(fps)
    intro_screen()
    pygame.time.delay(8000)
    gamedisplay.fill(green)
    major_screen()
    pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/8,display_width,display_height/60))
    pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/2.4,display_width,display_height/60))
    pygame.display.update()
    gameexit=False
    while not gameexit:
        gamedisplay.fill(green)
        major_screen()
        pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/8,display_width,display_height/60))
        pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/2.4,display_width,display_height/60))
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                sys.exit()
                quit()
            elif event.type==pygame.KEYDOWN:
                if event.key==pygame.K_LEFT:
                    left_block.top -= 10
        pygame.draw.rect(gamedisplay, blue, left_block)
        pygame.display.update()
main_loop()