How do I make sprites move in python/pygame? - python-3.x

I am making kind of like a pong game but I can't seem to make the paddle to move.
So here is the code:
import pygame
pygame.init()
GameDisplay=pygame.display.set_mode([800,600])
pygame.display.set_caption('GAME')
FPS=pygame.time.Clock()
black=(0,0,0)
white=(255,255,255)
GameDisplay.fill(white)
StopProgram=False
class Paddle(pygame.sprite.Sprite):
def __init__(self,image=pygame.image.load('Paddle.png')):
super(Paddle,self).__init__()
self.image=pygame.image.load('Paddle.png')
self.rect=self.image.get_rect()
self.speed=(2,2)
def setPosition(self,x,y):
self.rect.x=x
self.rect.y=y
def move(self,x,y):
self.rect.centerx
self.rect.centery
PaddleGroup=pygame.sprite.Group()
PaddlePlayer1=Paddle()
PaddlePlayer1.setPosition(235,492)
PaddleGroup.add(PaddlePlayer1)
PaddleGroup.draw(GameDisplay)
while not StopProgram:
for event in pygame.event.get():
if event.type==pygame.QUIT:
StopProgram=True
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
PaddlePlayer1.rect.move_ip(-5,0)
pygame.display.flip()
FPS.tick(60)
pygame.quit()
quit()
I hope someone can help me to my solution.(Sorry for bad english.)

Related

What can I do to make my PyGame window respond?

I am trying to make a window open every time I run my code, but every time I run the code, it shows the window as unresponsive.
My code is as follows:
import os
import time
import sys
pygame.font.init()
WIDTH, HEIGHT = 500, 400
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("EscapeGame")
BG = pygame.transform.scale(pygame.image.load(os.path.join("GameMap1.png")), (WIDTH, HEIGHT))
def main():
fps = 60
clock = pygame.time.Clock()
def redraw_window():
WIN.blit(BG, (0,0))
pygame.display.update()
while True:
clock.tick(fps)
redraw_window()
for event in pygame.event.get():
if event.type == sys.exit:
sys.exit()
input()
main()
I've already tried a few thing with event.get and event.pump, I'm probably missing something very basic. Could someone tell me what else I could try?
Also, I'm new at pygame, so if anyone sees another mistake, please tell me about. I would greatly appreciate it.
The window is unresponsive because you don't handle the events by calling pygame.event.get().
While pygame.event.get() is in your code, it's never executed because
a) it's in a function you never call
b) your code blocks on the input() call
You're code should look like this:
import os
pygame.init()
WIDTH, HEIGHT = 500, 400
def main():
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("EscapeGame")
BG = pygame.transform.scale(pygame.image.load(os.path.join("GameMap1.png")).convert_alpha(), (WIDTH, HEIGHT))
fps = 60
clock = pygame.time.Clock()
while True:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
WIN.blit(BG, (0,0))
pygame.display.flip()
main()

Why won't the pipes move?

I'm new to python and I'm making a flappy bird game, but the pipes won't move. I made several modules: flappy.py - main module, pipes.py which contains pipe objects, and a player class that has the bird object. Does anyone know solution to my problem?
main module
import pygame
import sys
from player import Player
from pipes import Pipes
BLUE = (0,191,255)
WHITE = (255,255,255)
clock = pygame.time.Clock()
def run_game():
pygame.init()
screen = pygame.display.set_mode((500,800))
flappy = Player(screen)
pipes = Pipes(screen)
while True:
#controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
flappy.moving_up=True
flappy.moving_down=False
elif event.key == pygame.K_q:
sys.exit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
flappy.moving_up=False
flappy.moving_down=True
screen.fill(WHITE)
flappy.blitme()
flappy.moving()
pipes.draw_upper_pipes()
pipes.draw_down_pipes(
pipes.move()
clock.tick(32) #settings fps to 32
pygame.display.flip()
run_game()
pipes.py module
import pygame
import random
class Pipes():
def __init__(self, screen):
self.screen = screen
self.pipe_x=300
self.color = (124,252,0) # green
self.pipe_width=100
self.pipe_height=random.randrange(100,300)
self.upperPipe=pygame.Rect(self.pipe_x, 0, self.pipe_width, self.pipe_height) #making upper pipe rect
self.down_pipe_height = random.randrange(500, 800)
self.downPipe=pygame.Rect(self.pipe_x, 400, self.pipe_width, self.down_pipe_height) #making bottom pipe rect
def move(self):
self.pipe_x-=9
def draw_upper_pipes(self):
pygame.draw.rect(self.screen, self.color, self.upperPipe) #drawing upper pipe
def draw_down_pipes(self):
pygame.draw.rect(self.screen, self.color, self.downPipe) #drawing bottom pipe
Use move_ip on your pipe rect
def move(self):
self.pipe_x-=9
upperPipe.move_ip(pipe_x,pipe_y)
https://www.pygame.org/docs/ref/rect.html#pygame.Rect.move_ip

Quit function is not working in pygame

I want the system to exit when I click the "x" on the window of pygame. I want to be able to call the quit_game function whenever I need to. So if the user does quit, it actually quits. I have tried sys exit and pygame.quit(), but I haven't been able to successfully implement those. Right now I just have the quit function built into the intro screen. I am using python 3.4.3. Here is the 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=2
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)))
quit_game()
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()
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()
Your code has no main game-loop. You may thinks this:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
quit()
Is your game-loop. While that is almost correct, it is missing a key part. A while loop. Your telling Python to loop through every single event that Pygame is tracking. After it finishes looping through the events once, it stops. You want Python to continually loop through the Pygame events, until the window is closed. To do this create a variable to control your loop. Such as running = True or not_quit = True. Next wrap a while-loop around your for-loop using the control variable as your condition. Lastly, if the user closes the window, set your control variable to False.
running = True # our variable to control the loop.
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False # break the loop
pygame.quit()
sys.exit()
quit()
This should allow you to close your window. I should note however that none of the text/shapes your blitting/drawing to the screen will be visible unless inside your game-loop. So you may need to rethink your code a bit.

Rectangle is not moving in pygame

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

Key Pressing System PyGame

I'm making a basic pong game and want to make a system where a button press makes the paddle goes up or down. I'm fairly new to PyGame and here's my code so far.
import pygame, sys
from pygame.locals import*
def Pong():
pygame.init()
DISPLAY=pygame.display.set_mode((600,400),0,32)
pygame.display.set_caption("Pong")
BLACK=(0,0,0)
WHITE=(255,255,255)
RED=(255,0,0)
DISPLAY.fill(BLACK)
while True:
def P(b):
pygame.draw.rect(DISPLAY,WHITE,(50,b,50,10))
xx=150
P(xx)
for event in pygame.event.get():
if event.type==KEYUP and event.key==K_W:
P(xx+10)
xx=xx+10
pygame.display.update()
elif event.type==QUIT:
pygame.quit()
sys.exit()
Please read some tutorials and/or docs. This is really basic and if you don't get this right it will bite you later.
Anyway, here's how it could look:
import pygame
pygame.init()
DISPLAY = pygame.display.set_mode((600,400))
paddle = pygame.Rect(0, 0, 10, 50)
paddle.midleft = DISPLAY.get_rect().midleft
speed = 10
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise SystemExit(0)
keystate = pygame.key.get_pressed()
dy = keystate[pygame.K_s] - keystate[pygame.K_w]
paddle.move_ip(0, dy * speed)
DISPLAY.fill(pygame.Color("black"))
pygame.draw.rect(DISPLAY, pygame.Color("white"), paddle)
pygame.display.update()
clock.tick(30)
That is ok for Pong but I still wouldn't write my game like this. So read some tutorials and maybe try CodeReview if you really want to improve your code.

Resources