Having some problems with Pygame - python

So heres my script:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((600,600))
pygame.display.set_caption('Doge Adventures')
gameexit = False
move_x = 300
move_y = 300
move_x_change = 0
move_y_change = 0
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:
move_x_change = -10
if event.key == pygame.K_RIGHT:
move_y_change = 10
move_x += move_x_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [move_x, move_y, 10, 10])
pygame.display.update()
pygame.quit()
quit()
The problem is that when I run it, nothing happens, no errors. Just the idle pops up, but no window. I was wondering if there was anything wrong with my code. I'm running python 2.6 with pygame 2.6

As I said you need to indentate everything, but I found many problems in your code, here's a version I did out of it:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((600,600))
pygame.display.set_caption('Doge Adventures')
gameexit = False
move_x = 300
move_y = 300
white = (255, 255, 255)
black = (0, 0, 0)
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:
move_x -= 10
elif event.key == pygame.K_RIGHT:
move_x += 10
elif event.key == pygame.K_UP:
move_y -= 10
elif event.key == pygame.K_DOWN:
move_y += 10
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [move_x, move_y, 10, 10])
pygame.display.update()
pygame.quit()
quit()
I recommend you the Al Sweigart book of Python/Pygame. It's such a great book.

Related

pygame chromebook key names

I am currently following a pygame tutorial on a chromebook on which i have installed and linux to use IDLE. i am writing a block of code which assigns and x-axis increase or decrase to the arrow keys:
import pygame
pygame.init()
displayWidth = 800
displayHeight = 600
black = (0, 0, 0)
white = (255, 255, 255)
clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('racecar.png')
def car(x,y):
gameDisplay.blit(carImg, (x,y))
x = (displayWidth * 0.45)
y = (displayHeight * 0.8)
x_change = 0
car_speed = 0
gameDisplay = pygame.display.set_mode((displayHeight, displayWidth))
pygame.display.set_caption('Zoomer')
clock = pygame.time.Clock()
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
car(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
when i try to run the code, the car sprite won't budge. is this due to the fact that i am on chromebook and key names are different or is it an other reason? thanks.
It doesn't work because you have wrong indentations.
You check pygame.KEYDOWN inside if ... pygame.QUIT: which is executed only when you close window.
You need all if event.type start in the same column
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0

Why is the "Game Over" text not displayed?

I have this code, and when I click ESC, I don't see "Game Over". The program waits for two seconds and closes without displaying text.
Pygame 1.9.6
What am I doing wrong?
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_UP:
lead_x_change = 0
lead_y_change = -block_size
elif event.key == pygame.K_DOWN:
lead_x_change = 0
lead_y_change = block_size
elif event.key == pygame.K_ESCAPE:
run = False
game_display.fill(white)
lead_x += lead_x_change
lead_y += lead_y_change
pygame.draw.rect(game_display, black, [lead_x, lead_y, width, height])
pygame.display.update()
clock.tick()
draw_text_middle("Game Over", 40, (0, 0, 0, 255), game_display)
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()
You've to remove the pygame.quit() call from the event loop. pygame.quit() uninitialize all pygame modules. After calling this function, nothing can be drawn any further calls to any pygame instructions will cause an exception.
I recommend to process the pygame events by pygame.event.pump() before .delay the application. This allows pygame to handle internal actions:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# pygame.quit() <---------- delete
# [...]
draw_text_middle("Game Over", 40, (0, 0, 0, 255), game_display)
pygame.display.update()
pygame.event.pump()
pygame.time.delay(2000)
pygame.quit()

Unable to properly refresh display in Pong game

import pygame, sys
pygame.init()
display_width = 800
display_height = 500
white = (255, 255, 255)
display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pong')
clock = pygame.time.Clock()
platform_y = display_height * 0.38
def platform(color, x, y):
global platform_y
pygame.draw.rect(display, color, (x, y, 25, 100))
pygame.display.update()
clock.tick(80)
def ball():
pygame.draw.circle(display, white, (400, 250), 12)
pygame.display.update()
def game():
global platform_y
y_change = 0
while True:
platform(white, 100, platform_y)
platform(white, 675, platform_y)
ball()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
sys.exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change -= 2
if event.key == pygame.K_DOWN:
y_change += 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
platform_y += y_change
pygame.display.update()
clock.tick(80)
if __name__ == '__main__':
game()
I guess it's not the best code but I've been messing around with pygame and got to the point when I have to create Pong, although I'm not sure why the rectangles (platforms) are just getting higher instead of going up and down (I know that both platforms will rise together, will fix that, this is just for test)
You had couple of issues with your code:
You have to call pygame.display.update only once, not after every pygame.draw,
clock.tick is set in main game loop, not in other places,
You have to clear the screen before drawing.
Here is what will probably work as you expect:
import pygame, sys
pygame.init()
display_width = 800
display_height = 500
white = (255, 255, 255)
display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pong')
clock = pygame.time.Clock()
platform_y = display_height * 0.38
def platform(color, x, y):
global platform_y
pygame.draw.rect(display, color, (x, y, 25, 100))
def ball():
pygame.draw.circle(display, white, (400, 250), 12)
def game():
global platform_y
y_change = 0
while True:
platform(white, 100, platform_y)
platform(white, 675, platform_y)
ball()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
sys.exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change -= 2
if event.key == pygame.K_DOWN:
y_change += 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
platform_y += y_change
pygame.display.update()
clock.tick(80)
display.fill((0, 0, 0))
if __name__ == '__main__':
game()
Couple of advices:
Don't use global! It's smells on bad code. There are certainly better ways to achieve what you wanted without using it.
Try to avoid a lot of conditions nesting. It's very hard to read, and will require much mental energy to read after day or two.
Happy coding!

Moving an image in pygame?

So, I'm making my first game in pygame, and have done OK up to this point. I just can't move the image. Can I please get some help?
mc_x = 20
mc_y = 20
spider_x = 690
spider_y = 500
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
mc_x -= 5
elif event.key == pygame.K_RIGHT:
mc_x += 5
elif event.key == pygame.K_UP:
mc_y += 5
elif event.key == pygame.K_DOWN:
mc_y -= 5
screen.blit(background,(0,0))#fixed
screen.blit(spider_small,(spider_x,spider_y))#FIXED
screen.blit(mc,(mc_x,mc_y))
pygame.display.update()
Based on your code:
screen.blit(mc,(mc_x,mc_y))
pygame.display.update()
should be inside the loop so that it would update/refresh your game for every keystroke.
You forgot to update the screen. Set the update function inside the main game loop. This is going to work fine!
Here's my sample code
import pygame
pygame.init()
WHITE = (255, 255, 255)
RED = (255, 0, 0)
canvas = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Example')
gameExit = False
lead_x, lead_y = 300, 300
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:
lead_x -= 10
elif event.key == pygame.K_RIGHT:
lead_x += 10
elif event.key == pygame.K_DOWN:
lead_y += 10
elif event.key == pygame.K_UP:
lead_y -= 10
canvas.fill(WHITE)
pygame.draw.rect(canvas, RED, [lead_x, lead_y, 30, 30])
pygame.display.update()
pygame.quit()
quit()

Pygame display's a black screen

I did a bit of research to see if i could solve the issue that way, but didn't seem to find anything to solve my problem. I found both of these : Why isn't my pygame display displaying anything? and Confused at why PyGame display's a black screen. I tried to solve my problem with what was adviced in the comments but it didn't work, or the reason for the problem was different than mine.
When i run the code the pygame window shows, but is just completely black, but no errors are called.
so here is the code (might be a bit long).
import pygame
import time
import random
pygame.init()
display_width = 700
display_height = 900
player_width = 140
player_height = 100
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Baby shower')
clock = pygame.time.Clock()
greenkidimg = pygame.image.load('kid green.png')
bluekidimg = pygame.image.load('kid blue.png')
redkidimg = pygame.image.load('kid red.png')
catcherimg = pygame.image.load('catcher.png')
background = pygame.image.load('background.png')
#image loads
def player(x,y):
gameDisplay.blit(catcherimg,(x,y))
def bluekid(bluex, bluey, bluew, blueh):
gameDisplay.blit(bluekidimg, (bluex, bluey))
def redkid(redx, redy, redw, redh):
gameDisplay.blit(redkidimg, (redx,redy))
def greenkid(greenx, greeny, greenw, greenh):
gameDisplay.blit(greenkidimg(greenx, greeny))
def game_loop():
x = (display_width*0.45)
y = (display_height*0.8)
x_change = 0
blue_width = 66
bluex_start = random.randrange (0, display_width-blue_width)
bluey_start = -600
blue_speed = 15
blue_height = 78
green_width = 66
greenx_start = random.randrange (0, display_width-green_width)
greeny_start = -600
green_speed = 15
green_height = 78
red_width = 66
redx_start = random.randrange (0, display_width-red_width)
redy_start = -600
red_speed = 15
red_heigth = 78
#greenkid,redkid,bluekid-start,speed,h/w
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
x_change = -20
elif event.type == pygame.K_RIGHT:
x_change = 20
if event.type == pygame.KEYUP:
if event.type == pygame.K_LEFT or pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.blit(background(0,0))
player(x,y)
bluekid(bluex, bluey, bluew, blueh)
redkid(redx, redy, redw, redh)
greenkid(greenx, greeny, greenw, greenh)
#display
pygame.display.update()
clock.tick(30)
game_loop()
pygame.quit()
quit()
The rendering logic in the game loop just needs to be indented to it is a part of the while loop inside it, like this:
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
x_change = -20
elif event.type == pygame.K_RIGHT:
x_change = 20
if event.type == pygame.KEYUP:
if event.type == pygame.K_LEFT or pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.blit(background(0,0))
player(x,y)
bluekid(bluex, bluey, bluew, blueh)
redkid(redx, redy, redw, redh)
greenkid(greenx, greeny, greenw, greenh)
pygame.display.update()
clock.tick(30)

Categories

Resources