For some reason I was working on a basic pygame project and my background appears to be black, however I specified it as white. Please assist if possible.
Here is the code (by the way, this is PYGAME in python):
import pygame
pygame.init()
white = (255,255,255)
black=(0,0,0)
red=(255,0,0)
gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('Slither')
gameExit=False
lead_x = 300
lead_y = 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
if event.key == pygame.K_RIGHT:
lead_x +=10
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black,[lead_x,lead_y,10,100])
pygame.display.update()
pygame.quit()
quit()
The thing is that your drawing code is outside of the while loop. Indent it by one tab and it should work, so, like this:
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
if event.key == pygame.K_RIGHT:
lead_x +=10
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black,[lead_x,lead_y,10,100])
pygame.display.update()
Related
I can't seem to the error in this code, the game simply quits right after launching it. If anybody could read through my code and answer me what I'm doing wrong, and I'd be very grateful. I've already read through all of it and couldn't figure it out. I don't know why the game quits right after launching it. (Meaning I can't even move my block).
import pygame
import time
pygame.init()
width = 800
height = 600
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("First snake game.")
clock = pygame.time.Clock()
block_size = 1
FPS = 100
def gameloop():
gameExit = False
lead_x = width/2
lead_y = height/2
lead_x_change = 0
lead_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_w:
lead_y_change = -block_size
lead_x_change = 0
if event.key == pygame.K_s:
lead_y_change = block_size
lead_x_change = 0
if event.key == pygame.K_d:
lead_x_change = block_size
lead_y_change = 0
if event.key == pygame.K_a:
lead_x_change = -block_size
lead_y_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
lead_x_change = 0
if event.key == pygame.K_w or event.key == pygame.K_s:
lead_y_change = 0
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])
pygame.display.update()
time.sleep(2)
pygame.quit()
quit()
clock.tick(FPS)
gameloop()
`
Not sure if I'm understanding the code correctly or not, but at the end of the gameloop() function you have pygame.quit() and quit() so comment that out and see what happens.
Im in the process in making a snake game and my problem is understanding why using the code bellow keeps continually adding -10 or +10 to the lead_x value when the lead_x_change is incremented with an event to only increase lead_x by +10 or -10 1 event at a time? Thanks.
import pygame
pygame.init()
#Colors
white= (255,255,255)
black=(0,0,0)
red= (255,0,0)
gameDisplay= pygame.display.set_mode((800,600))
pygame.display.set_caption('ALIEN')
gameExit= False
lead_x=300
lead_y=300
lead_x_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:
lead_x_change =-1
if event.key==pygame.K_RIGHT:
lead_x_change = 1
lead_x += lead_x_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay,black,[lead_x,lead_y,10,10])
pygame.display.update()
pygame.quit()
quit()
Try something like this
import pygame
pygame.init()
#Colors
white= (255,255,255)
black=(0,0,0)
red= (255,0,0)
gameDisplay= pygame.display.set_mode((800,600))
pygame.display.set_caption('ALIEN')
gameExit= False
lead_x=300
lead_y=300
lead_x_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:
lead_x_change =-1
if event.key==pygame.K_RIGHT:
lead_x_change = 1
if event.type==pygame.KEYUP:
if event.key== pygame.K_LEFT:
lead_x_change = 0
if event.key== pygame.K_RIGHT:
lead_x_change = 0
lead_x += lead_x_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay,black,[lead_x,lead_y,10,10])
pygame.display.update()
pygame.quit()
quit()
I started watching a youtube video series on learning pygame by sentdex.
I have been following all his steps and i got to the point where i had a car(the image) and it was on a white screen. But, when i put in the functions to be able to move the car. Next time i ran it I could not see my car. I noticed that I could see it for a split second when i closed the screen.I have tried searching this question every way that i could think of. It is probably a small stupid mistake on my part. Thank you in advance
import pygame
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
carImg = pygame.image.load('racecar.png')
def car(x, y):
gameDisplay.blit(carImg,(x,y))
x = (display_width * 0.45)
y = (display_height * 0.6)
x_change = 0
crashed = False
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_LEFT:
x_change = -5
if 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()
Your indentation is messed up. Everything from for event in pygame.event.get(): to clock.tick(60) should be indented to show that it fits inside the while not crashed: loop. Also, crashed = True should be indented to show that it is inside the if event.type == pygame.QUIT: statement. In addition, the pygame.KEYDOWN and pygame.KEYUP comparisons should be inside your for event in pygame.event.get(): loop. Finally, you can improve speed by changing some of the ifs to elifs. This is the corrected version of the last section of your code:
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif 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()
I'm stuck trying to change the sprite blitted to the screen (when moving upwards) to a back view of the sprite.
Currently the sprite is just blitted to the white background on each key press at the most recent x and y coordinates and not fluidly moving along the screen (i want to move the sprite continuously on set trajectories, based on arrow key press, like in snake).
This is my first stab at programming anything on python so Leyman's terms if possible!
lead_x_change = 0
lead_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:
lead_x_change = -5
if event.key == pygame.K_RIGHT:
lead_x_change = 5
#above are two snips of code to show the method i'm using to move the sprite
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_DOWN:
gameDisplay.fill(white)
gameDisplay.blit(spriteX,(lead_x,lead_y))
pygame.display.update()
if event.key == pygame.K_UP:
gameDisplay.fill(white)
gameDisplay.blit(spriteY,(lead_x,lead_y))
pygame.display.update()
lead_y += lead_y_change
lead_x += lead_x_change
clock.tick(60)
pygame.quit()
quit()
I think there are two plausible ways you can change your sprite depending on the direction you're moving.
First off, lets fix your current drawing logic, so that you draw the sprite regardless of keypresses:
while not gameExit:
for event in pygame.event.get(): # event loop handles setting player speed
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -5
if event.key == pygame.K_RIGHT:
lead_x_change = 5
lead_y += lead_y_change # update logic moves the position
lead_x += lead_x_change
gameDisplay.fill(white) # draw logic draws the background and the sprite
gameDisplay.blit(spriteX,(lead_x,lead_y))
pygame.display.update()
clock.tick(60)
The approach to the sprite is to change it at the same place you change your speed (in the event code), then use the changed sprite later when you actually draw the scene.
player_sprite = spriteX
while not gameExit:
for event in pygame.event.get(): # event loop handles setting speed and the sprite
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -5
player_sprite = spriteX
if event.key == pygame.K_UP:
lead_y_change = -5
player_sprite = spriteY
lead_y += lead_y_change # update logic moves the position
lead_x += lead_x_change
gameDisplay.fill(white) # draw logic draws the background and the sprite
gameDisplay.blit(player_sprite,(lead_x,lead_y))
pygame.display.update()
clock.tick(60)
The second approach is to limit the event code to just setting the speed, and then later examining the speed that has been set to pick a sprite to draw.
player_sprite = spriteX
while not gameExit:
for event in pygame.event.get(): # event loop handles setting speed only
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -5
if event.key == pygame.K_UP:
lead_y_change = -5
lead_y += lead_y_change # update logic moves the position
lead_x += lead_x_change
if lead_y_change < 0: # set sprite based on speed
player_sprite = spriteY
else:
player_sprite = spriteX
gameDisplay.fill(white) # draw logic draws the background and the sprite
gameDisplay.blit(player_sprite,(lead_x,lead_y))
pygame.display.update()
clock.tick(60)
For very simple logic with only a few different sprites, the event driven sprite changes may be easiest. If you want animations or other more complicated sprite changing behavior, the second approach may be better, since you will be able to handle all the sprite logic in one place.
I am trying to make a fun program with a guy who opens his mouth whenever a press space. The problem is he only opens it for .1 seconds and then it closes again. I want to make it so that the mouth is open whenever i hold space.
Code:
import pygame
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Open The Mouth")
clock = pygame.time.Clock()
faceImg = pygame.image.load("face_shut.png")
faceOpenImg = pygame.image.load("face_open.png")
def face(x,y):
gameDisplay.blit(faceImg,(face_x,face_y))
def faceOpen(x,y):
gameDisplay.blit(faceOpenImg,(faceOpen_x,faceOpen_y))
faceOpen_x = (1)
faceOpen_y = (1)
face_x = (1)
face_y = (1)
programRunning = True
while programRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
programRunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
faceOpen(faceOpen_x,faceOpen_y)
pygame.display.update()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
face(face_x,face_y)
pygame.display.update()
face(face_x,face_y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
The issue appears to be here:
while programRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
programRunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
faceOpen(faceOpen_x,faceOpen_y)
pygame.display.update()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
face(face_x,face_y)
pygame.display.update()
# Here, always drawn closed at the end of the while
face(face_x,face_y)
pygame.display.update()
clock.tick(60)
Regardless of what's being set in the while, you are redrawing the face as closed at the end.
How about this instead:
# Drawn before the loop starts
face(face_x,face_y)
pygame.display.update()
while programRunning:
for event in pygame.event.get():
event_occurred = True
if event.type == pygame.QUIT:
programRunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
faceOpen(faceOpen_x,faceOpen_y)
pygame.display.update()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
face(face_x,face_y)
pygame.display.update()
clock.tick(60)