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()
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.
I'm new to python and I'm trying following along with a tutorial that uses PyGame to create a snake like game. For some reason my boundaries are not working. It may be something simple but I can't see any reason why it wouldn't work. I don't get any errors, the snake just goes past the boundaries and the game doesn't end.
import pygame
import time
import random
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Slither')
clock = pygame.time.Clock()
block_size = 10
FPS = 30
font = pygame.font.SysFont(None, 25)
def message_to_screen(msg,color):
screen_text = font.render(msg, True, color)
gameDisplay.blit(screen_text, [display_width/2, display_height/2])
def gameLoop():
gameExit = False
gameOver = False
lead_x = display_width/2
lead_y = display_height/2
lead_x_change = 0
lead_y_change = 0
randAppleX = random.randrange (0, display_width-block_size)
randAppleY = random.randrange (0, display_height-block_size)
while not gameExit:
while gameOver == True:
gameDisplay.fill(white)
message_to_screen("Game over, press C to play again or Q to quit", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameLoop()
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 = -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_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_DOWN:
lead_y_change = block_size
lead_X_change = 0
**if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
gameOver == True #boundaries**
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, block_size, block_size])
pygame.draw.rect(gameDisplay, black, [lead_x , lead_y, block_size, block_size])
pygame.display.update()
clock.tick(FPS)
message_to_screen("You Lose", red)
pygame.display.update()
time.sleep(2)
pygame.quit()
quit()
gameLoop()
In your exit condition, you're using the equality comparison, not the assignment operator:
if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
gameOver == True #boundaries
in the above,
gameOver == True
should be
gameOver = True
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'm using 2.7.10 on pygame and I can't understand why the right keeps going down instead of right. The left works perfect. I can't find any issues relating, so does anyone know what is wrong with this?
import pygame
pygame.init()
GameDisplay = pygame.display.set_mode((800, 600))
White = (255,255,255)
Black = (0,0,0)
Red = (255,0,0)
pygame.display.set_caption('Test')
PyQuit = False
lead_x = 300
lead_y = 300
while not PyQuit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
PyQuit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x -= 10
if event.key == pygame.K_RIGHT:
lead_y += 10
GameDisplay.fill(White)
pygame.draw.rect(GameDisplay, Black, [lead_x,lead_y,10,10])
pygame.display.update()
pygame.quit()
quit()
Instead of:
lead_y += 10
Try:
lead_x += 10
Use lead_x for horizontal and lead_y for vertical.
if event.key == pygame.K_RIGHT:
lead_y += 10'
You are making the lead_y += 10 when you should be adding 10 to the x coordinate
if event.key == pygame.K_RIGHT:
lead_x += 10'
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()