0

I have tried what other sites said to move things but mine is not working like it should. I am trying to create a Stacker game.

I want all 3 blocks to move once the user presses either the left or right arrow key. I am using python 3.4.3. I will try to get the boundaries in later once I get it to move first. When I run this it says can't assign to operator.-syntax error. Here is my code.

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()
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Bob G.
  • 143
  • 3
  • 14
  • Why do people just edit my question, but then do not even try to help?!?!?!? Come on, is there any site where I can get some help on? – Bob G. Aug 23 '16 at 06:09
  • Some people don't know how to answer certain questions, and others are just moderators and keep the site in check. Just be patience and wait for an answer. And besides, Stack Overflow does not _owe_ you an answer. – Christian Dean Aug 23 '16 at 20:01
  • @BobG. Questions that are nicely formatted, use proper grammar, uses the right tags and are all around pleasant to eye are more likely to be understood and therefore answered. I looked at your question, ready to help, but the unformatted code where you've just pressed everything in so tight was so hard and unpleasant to read I simply didn't have the time or energy to look for errors. – Ted Klein Bergman Aug 28 '16 at 16:06

1 Answers1

0

You have a syntax error. Which line does it refer to? I also don't see why your code would work. How can you subtract 10 from the block function which draws a rectangle? block_left() +=10.

I would define rectangles for each block

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()

I edited your code to work with one block. Draw everything then update the screen just once.

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()
B Bulfin
  • 79
  • 10
  • The problem with that is I want to be able to call the function, but this way it draws it and does not let me choose. So if I move it, there will be another one there. That is why I made a function for each of them. I went ahead and tried what you said though. I made left, right, and middle blocks. I then tried to remove it but got the same error. Then I just have the middle left. I made that change in the for loop also. I tried to run it and it showed the middle block, but when I pressed the arrow key, it gave me this message. unsupported operand type(s) for -=: 'pygame.Rect' and 'int' – Bob G. Aug 23 '16 at 14:59
  • He can subtract ten from the _return value_ of the function `block_left` if it returns a integer – Christian Dean Aug 23 '16 at 19:57
  • Right, but it doesn't return an integer does it? You need to say Rect.top -= 10 or Rect.right -= 10. You can't subtract an integer from Rect, but you can from it's top, bottom left or right attributes. To avoid having two objects you can redraw the background before drawing the new rectangle, gamedisplay.fill(green) or just fill the area where the old rectangle used to be. – B Bulfin Aug 24 '16 at 07:51
  • Ok, it is still not moving and I tried what goosberry said and what b bulfin said. I assigned new variables for the boxes but this time it is with rect.top. I then just called those variables. Still gave me same error. What now? – Bob G. Aug 25 '16 at 00:39
  • We can't fix bugs in your code without seeing it. I simplified your code in the answer above to work with one block anyway as I explained. – B Bulfin Aug 25 '16 at 09:19
  • How can I post my code because I can't do it in the comment section here? Should I post it in like an answer even though it is not an answer? – Bob G. Aug 25 '16 at 15:53
  • How do you keep the block moving once the user pressed the left or right key? How can I post my code because I can't do it in the comment section? – Bob G. Aug 25 '16 at 22:44
  • I think you need to learn how to use pygame, try some of the tutorials http://www.pygame.org/wiki/tutorials . I learned to code by writing this game http://brendanbulfin.weebly.com/first-reality.html , there are tips at the bottom of the page about learning to code and game development. Source is here https://bitbucket.org/bulfinb/first-reality-android/src – B Bulfin Aug 26 '16 at 10:42
  • That is what I have been doing Bulfin. All the sources just make the object move like a certain increment and not actually keep it moving or even moving smoothly. Can you please tell me how I can do this or post my code on a comment section? What about future people who have the same problem as me who don't want to just move it like 10 pixels or something like that? – Bob G. Aug 26 '16 at 15:09
  • @BobG. You don't post code in the comments; you edit your question with the new code. – Ted Klein Bergman Aug 28 '16 at 16:08
  • I answered your original question, sand took the time to edit your code tidying it up and making the block move. This is what you asked so you should mark it as correct. See the answer to this question for moving sprites continuously. http://stackoverflow.com/questions/16183265/how-to-move-sprite-in-pygame if you want the block to stay moving after the key is pressed you could make a class and give the block a self.moving = False attribute. Then after it is pressed set it to true and use this to control the movement – B Bulfin Aug 29 '16 at 09:28
  • I'm sure many people have faced and solved the same or similar problems and there are many examples in the tutorials of moving sprites. I don't have time to write the code for your specific case and remember people take the time to answer your questions. Time that can be spent doing something else so it is good to be courteous and thankful when people try to help you. – B Bulfin Aug 29 '16 at 09:37
  • Ok, normally I would leave a "thanks in advance" message after posting a question on here. This time I did not for some reason. So I am sorry about that and thanks for everything you have done so far. You're right about you answering my original question. So I will mark it as correct. However, I might have to post another question and make sure I ask it right this time with the thanks in advance message. Also, I have not used classes much before so I have no idea how to include that in my code. I will try my best to make a class but no garentee. – Bob G. Aug 29 '16 at 15:39