Snake problems - Movement - python

I am having some issues with my code in python, I am making a version of snake, my issue concerns the movement of the snake itself. I have gotten the directions working fine, I just need to make it so that the snake continues moving in the direction it has been told to do so via the key press, I also need to make it so that it is the one block, currently it shows all previous blocks once it has been moved.
import pygame, sys, time, random
from pygame.locals import *
pygame.init()
size = width,height = 480, 480 #16x30,16x30
grey = (128,128,128)
screen = pygame.display.set_mode(size)
icon = pygame.image.load('snakeIcon.png')
newDir = 0
prevDir = 0
FPS = 8
xCoord = 10.0
yCoord = 10.0
#Main Program:
pygame.display.set_caption('Anthony\'s Snake')
pygame.display.set_icon(icon)
screen.fill(grey)
fpsTime = pygame.time.Clock()
gameB = pygame.image.load('gameB.png')
border = {}
createLvl()
snakeH = pygame.image.load('snakeH.png')
snake = {}
refreshScreen()
keyCheck = {'LEFT':bool(0), 'RIGHT':bool(0), 'UP':bool(0), 'DOWN':bool(0), 'ESC':bool(0)}
pygame.mixer.music.load('retroMusik.mp3')
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(-1)
while bool(1):
for i in range(1):
snake[str(len(snake))]=snakeH.get_rect()
x = xCoord
y = yCoord
for i in range(len(snake)):
snake[str(i)].x = x*16
snake[str(i)].y = y*16
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_UP :
keyCheck['UP'] = bool(1)
if event.key == K_DOWN :
keyCheck['DOWN'] = bool(1)
if event.key == K_LEFT :
keyCheck['LEFT'] = bool(1)
if event.key == K_RIGHT :
keyCheck['RIGHT'] = bool(1)
if event.key == K_ESCAPE :
keyCheck['ESC'] = bool(1)
if event.type == KEYUP:
if event.key == K_UP :
keyCheck['UP'] = bool(0)
elif event.key == K_DOWN :
keyCheck['DOWN'] = bool(0)
elif event.key == K_LEFT :
keyCheck['LEFT'] = bool(0)
elif event.key == K_RIGHT :
keyCheck['RIGHT'] = bool(0)
elif event.key == K_ESCAPE :
keyCheck['ESC'] = bool(0)
if keyCheck['UP']:
if prevDir != 'DOWN':
newDir = 'UP'
print('Direction changed to UP')
if keyCheck['DOWN']:
if prevDir != 'UP':
newDir = 'DOWN'
print('Direction changed to DOWN')
if keyCheck['LEFT']:
if prevDir != 'RIGHT':
newDir = 'LEFT'
print('Direction changed to LEFT')
if keyCheck['RIGHT']:
if prevDir != 'LEFT':
newDir = 'RIGHT'
print('Direction changed to RIGHT')
prevDir = newDir
if keyCheck['UP'] == bool(1):
yCoord -= 1
if keyCheck['DOWN'] == bool(1):
yCoord += 1
if keyCheck['LEFT'] == bool(1):
xCoord -= 1
if keyCheck['RIGHT'] == bool(1):
xCoord += 1
refreshScreen()
fpsTime.tick(FPS)
Please be soft on me, I am still new to this. Thank you in advance.

I think your animation doesn't work right because you don't erase (cover with background) the screen before drawing. See this Pygame tutorial for slow and detailed explanation.
Don't use bool(1). Use True and False.
I'm not sure but I think the problem with the snake not moving is either A) you reset your control/movement variable or B) you don't check it every "round"/frame.
I'd really recommend finding some tutorial (or something) and read some Python code by other people, your code could use a lot of improvement (this isn't bad, the important thing is trying to improve).

Related

Multiplayer Following Camera in Pygame

For a school project I am building a recreation of Among Us in python with Pygame. I have already set up all the server and client side code and that's all working fine. I'm now in the process of making the camera follow the player. Only I can't get it to work.
My idea was: when a player moves, everything in his surroundings has to move in the opposite direction. But when you have a multiplayer game this doesn't work. Because then the other player moves as well which breaks the system.
If anyone has any idea how to make such a code, please let me know.
Thank you in advance
You don't have to move the background and object rects around the player, you can just move the player around and have a scroll offset value that keeps track of how much the blitted objects have to be offset. You don't apply the scroll value to the rect position because the rect position isn't relative to the window like the blit objects are. Here is an example of how you could achieve this.
import pygame, sys
clock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption("Scrolling example")
WINDOW_SIZE = (600, 400)
screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32)
scroll = [0, 0]
player = pygame.Rect(100, 100, 10, 10)
up = False
down = False
left = False
right = False
blocks = [pygame.Rect(250,250,50,50)]
while True:
screen.fill((0, 0, 0))
scroll[0] += (player.x - scroll[0] - (WINDOW_SIZE[0]/2)) // 20
scroll[1] += (player.y - scroll[1] - (WINDOW_SIZE[1]/2)) // 20
player_movement = [0, 0]
if right == True:
player_movement[0] += 2
if left == True:
player_movement[0] -= 2
if up == True:
player_movement[1] -= 2
if down == True:
player_movement[1] += 2
player.x += player_movement[0]
player.y += player_movement[1]
player_scroll_rect = player.copy()
player_scroll_rect.x -= scroll[0]
player_scroll_rect.y -= scroll[1]
pygame.draw.rect(screen, (255,255,255), player_scroll_rect)
for block in blocks:
scroll_block = block.copy()
scroll_block.x = scroll_block.x - scroll[0]
scroll_block.y = scroll_block.y - scroll[1]
pygame.draw.rect(screen, (0,0,255), scroll_block)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
right = True
if event.key == K_LEFT:
left = True
if event.key == K_UP:
up = True
if event.key == K_DOWN:
down = True
if event.type == KEYUP:
if event.key == K_RIGHT:
right = False
if event.key == K_LEFT:
left = False
if event.key == K_UP:
up = False
if event.key == K_DOWN:
down = False
pygame.display.update()
clock.tick(60)
If you want a solution for using images, ask me.
Also, you can find more about scrolling here: https://www.youtube.com/watch?v=5q7tmIlXROg

My rect isn't updating

I have used pygame for a while now, but now the player rect isn't updating or noticing collisions. It is still moving though. This just happened after I modified a bit of code. I have looked at the .draw functions and the walls and the border is being drawn, but the player isn't, and the screen is being updated. (The code snippet is in javascript because I still don't know how to use a code snippet in python.)
import pygame, sys, random
from pygame.locals import *
from time import sleep
pygame.init()
def render():
windowSurface.fill(black)
pygame.draw.rect(windowSurface,white,player1)
if level == 1:
pygame.draw.rect(windowSurface,white,wall_lvl_1_1)
pygame.draw.rect(windowSurface,white,wall_lvl_1_2)
pygame.draw.rect(windowSurface,white,border1_lvl_1)
elif level == 2:
filler = 'done'
pygame.display.update()
black = (0,0,0)
white = (255,255,255)
windowSurface = pygame.display.set_mode((500, 500),0,32)
windowSurface.fill(black)
level = 1
xmod = 0
ymod = 0
direction = 'none'
player1 = pygame.Rect(225 + xmod,450 - ymod,30,30)
wall_lvl_1_1 = pygame.Rect(0,225,250,50)
wall_lvl_1_2 = pygame.Rect(300,250,200,50)
border1_lvl_1 = pygame.Rect(0,0,25,500)
border2_lvl_1 = pygame.Rect(0,0,500,25)
border3_lvl_1 = pygame.Rect(0,0,1,1)
pygame.draw.rect(windowSurface,white,wall_lvl_1_1)
render()
while True:
render()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_LEFT:
direction = 'left'
if event.key == K_RIGHT:
direction = 'right'
if event.key == K_UP:
direction = 'up'
if event.key == K_DOWN:
direction = 'down'
if event.type == QUIT:
pygame.quit()
sys.exit()
if player1.colliderect(wall_lvl_1_1) or player1.colliderect(wall_lvl_1_2):
xmod = 0
ymod = 0
player1 = pygame.Rect(225 + xmod,450 - ymod,30,30)
print('again')
if direction == 'left':
xmod -= 1
sleep(0.004)
if direction == 'right':
xmod += 1
sleep(0.004)
if direction == 'up':
ymod += 1
sleep(0.004)
if direction == 'down':
ymod -= 1
sleep(0.004)
if ymod == 450:
level = 2
render()
You need to update the position of the player1 rect each frame.
player1.topleft = (xmod, ymod)

How do I make a certain sprite move in pygame?

I need to make the player move continuously when I press a certain key.The problem that I have is that the image of the player is moving once (when I press one of the defined keys) and then it stops.
enter code here
import pygame
import sys
import os
import random
import time
from pygame.locals import *
pygame.init()
white = ( 255, 255, 255 )
black = ( 0, 0, 0 )
screenw = 800
screenh = 600
screen = pygame.display.set_mode( ( screenw, screenh ) )
pygame.display.set_caption( "Game" ) # Here I create a display.
clock = pygame.time.Clock()
class Car(pygame.sprite.Sprite): # Here I create a class.
def __init__( self, color = black, width = 100, height = 100 ):
pygame.sprite.Sprite.__init__( self )
self.image = pygame.Surface( ( width, height ) )
self.image.fill( color )
self.rect = self.image.get_rect()
def set_pos(self, x, y):
self.rect.x = x
self.rect.y = y
def set_img( self, filename = None):
if filename != None:
self.image = pygame.image.load( filename )
self.rect = self.image.get_rect()
def main():*I create a game loop
x_change = 0
y_change = 0
x = 0
y = 0
car_group = pygame.sprite.Group() # Make a group
player = Car()
player.set_img( "images.jpg" )
car_group.add( player )
exit = False
FPS = 60
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
elif event.key == pygame.K_RIGHT:
x_change = 10
elif event.key == pygame.K_UP:
y_change = -10
elif event.key == pygame.K_DOWN:
y_change = 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.type == pygame.K_DOWN:
x_change = 0
y_change = 0
x += x_change
y += y_change
screen.fill( white )
player.set_pos( x, y ) # Blit the player to the screen
car_group.draw( screen )
clock.tick( FPS )
pygame.display.update()
main()
pygame.quit()
quit()
Well the problem is that you are only allowing the sprite to move once. For example, take this piece of your code:
if event.key == pygame.K_LEFT:
x_change = -10
This will allow the x position to be smaller by 10. Then it stops. You maybe want to make a update function. A variable will also be made to allow the function to be used or not to be used. Another one will be used to control the direction it goes. Here is the update function. Feel free to change it to be able to fit your needs:
def update():
global direction
if direction == 'LEFT':
x -= 10
elif direction == 'RIGHT':
x += 10
elif direction == 'UP'
y -= 10
elif direction == 'DOWN':
y += 10
Now we will need the variable to control whether the function will run or not. Add these two lines for our new two variables:
run = 0
direction = 'NONE'
They should be before the code for the class. You absolutely change the lines from your event.key lines, it should be (in proper indention):
if event.key == pygame.K_LEFT:
direction = 'LEFT'
run = 1
elif event.key == pygame.K_RIGHT:
direction = 'RIGHT'
run = 1
elif event.key == pygame.K_UP:
direction = 'UP'
run = 1
elif event.key == pygame.K_DOWN:
direction = 'DOWN'
run = 1
Of course, there must be something to stop the car from repeatedly moving infinitely right? Add these lines with the lines above:
elif event.key == pygame.K_SPACE:
direction = 'NONE'
run = 0
Now put these two lines in the while loop but before you for loop:
if run == 1:
Car.update()
else:
pass
This should work because as long run is equal to 1, the car will continue its movement in the given direction until stopped by pressing the spacebar. I hope this helps you!

My sprite wont move right pygame

earlier my sprite wouldn't move at all so i posted the code and i got it fixed for the most part but now my up/down arrows work fine but my right key doesn't work. (Also, when you press two keys, and then let go of one, the walk animation doesn't work, but I'm not desperate right now to get that fixed.) Also i would prefer not to use user made classes. thanks in advance.
Here is the code:
from pygame.locals import *
import pygame._view
pygame.init()
clock = pygame.time.Clock()
height = 500
width = 500
screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption('placeholder text')
photo = 'grassbackground.png'
background = pygame.image.load(photo).convert()
rectexist = False
photo1 = 1
user = pygame.sprite.Sprite()
change = False
up = False
down = False
left = False
right = False
speed = 5
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_UP:
up = True
change = True
if event.key == K_DOWN:
down = True
change = True
if event.key == K_LEFT:
left = True
change = True
if event.type == K_RIGHT:
right = True
change = True
if event.type == KEYUP:
if event.key == K_UP:
up = False
change = False
if event.key == K_DOWN:
down = False
change = False
if event.key == K_LEFT:
left = False
change = False
if event.key == K_RIGHT:
right = False
change = False
if down and user.rect.bottom < height:
user.rect.top += speed
if up and user.rect.top > 0:
user.rect.top -= speed
if left and user.rect.left > 0:
user.rect.left -= speed
if right and user.rect.right < width:
user.rect.right += speed
if change == True:
pygame.time.wait(110)
photo1 += 1
if change == False:
photo1 = 1
if photo1 == 1:
user.image = pygame.image.load("1.png").convert()
if rectexist == False:
user.rect = user.image.get_rect()
rectexist = True
screen.blit(user.image, user.rect)
if photo1 == 2:
user.image = pygame.image.load("2.png").convert()
screen.blit(user.image, user.rect)
if photo1 == 3:
user.image = pygame.image.load("3.png").convert()
screen.blit(user.image, user.rect)
if photo1 >= 4:
photo1 = 1
thesprites = pygame.sprite.RenderPlain((user))
thesprites.update()
screen.blit(background, (0, 0))
thesprites.draw(screen)
pygame.display.update()
clock.tick(60)
In your code it says:
if event.type == K_RIGHT:
it should be:
if event.key == K_RIGHT:
To keep animating you'd need to change the code a little more, add a:
key_pressed = []
in the beginning. Then for each key-press block do:
key_pressed.append(event.key)
and key-release do:
key_pressed = [k for k in key_pressed if k != event.key]
instead of the change=True and change=False respectively. Then after the two segments of checking what was pressed and released have add these lines:
if len(key_pressed) > 0:
change = True
else:
change = False
That should fix most of your issues...
Use pressed = pygame.key.get_pressed() to get a dictionary (a tuple, actually, but the way things are structured you can think of it as a dictionary) of all keys that are currently pressed. You'd use it like this:
pressed = pygame.key.get_pressed()
if pressed[K_LEFT]:
# move left
elif pressed[K_RIGHT]:
# move right
# etc

Python-Pygame always crashing and Not Responding when running a module in IDLE

I'm quite new to Pygame or even Python, but i know that when something in the isn't right, it displays some text in the Python Shell telling you that there was some error. I've actually encountered many of them and this time, it finally runs and displays the window, but it does not respond. I know there might be some mistakes in my whole code so please feel free to correct me (and please, kindly explain since I'm still new to this stuff).
The code is below, but if it can help, if you'd ask for it, i'll see if i could post the file as well. Anyway, here's the codes:
#import Modules
import os, sys
import pygame
from pygame.locals import *
background_img="C:/Users/JM/Documents/Python/Pygame_Alpha/background_img.jpg"
cursor_img="C:/Users/JM/Documents/Python/Pygame_Alpha/pygameCursor.png"
def load_image(img_file, colorkey=None):
file_pathname = os.path.join("\Users\JM\Documents\Python\Pygame_Alpha",img_file)
try:
image = pygame.image.load(file_pathname).convert_alpha()
except pygame.error, message:
print "Can't load image:", file_pathname
raise SystemExit, message
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
#Main character's position and movements
char_x,char_y = 0,0
char_go_x,char_go_y = 0,0
#Main char class
class char(pygame.sprite.Sprite):
"""Main Character"""
def __init__(self):
pygame.sprite.Sprite.__init__(self)#call Sprite initializer
self.image, self.rect = load_image("char_img.png", -1)
self.jumping = 0
def update(self):
self.rect.midtop = char_x,char_y
if self.jumping == 1:
self.rect.move_ip(-35,-3)
def char_no_jump(self):
self.jumping = 0
pygame.init()
pygame.display.set_caption("pygame_Alpha")
screen = pygame.display.set_mode((800,480),0,32)
background = pygame.image.load(background_img).convert()
cursor = pygame.image.load(cursor_img).convert_alpha()
char = char()
clock = pygame.time.Clock()
millisec = clock.tick()
sec = millisec/1000.0
char_fall = sec*25
jump = sec*50
#blit the background
screen.blit(background,(0,0))
#Main Loop
while 1:
#Tell pygame not to exceed 60 FPS
clock.tick(60)
#Events
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#Events triggered when a key/s is/are pressed
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key == K_UP or event.key == K_w:
char.jumping = 1
elif event.key == K_DOWN or event.key == K_s:
char_go_y += 1
elif event.key == K_LEFT or event.key == K_a:
char_go_x -= 0.5
elif event.key == K_RIGHT or event.key == K_d:
char_go_x += 0.75
if char_x > 800:
char_x = 0
#Events triggered when a key/s is/are released
if event.type == KEYUP:
if event.key == K_UP or event.key == K_w:
char_go_y += 1
elif event.key == K_DOWN or event.key == K_s:
char_go_y = 0
elif event.key == K_LEFT or event.key == K_a:
char_go_x = 0
if char_x < 0:
char_x = 0
elif event.key == K_RIGHT or event.key == K_d:
char_go_x = 0
if char_x > 700:
char_x = 0
char.update()
while char_y < 200:
char_go_y += char_fall
if char_y > 200:
char_y = 200
#Update values of position of Main Char
char_x += char_go_x
char_y += char_go_y
#Position Variables of Cursor Image, setting its values equal to cursor pos, and blit it to screen
cursor_x,cursor_y = pygame.mouse.get_pos()
cursor_x -= cursor.get_width()/2
cursor_y -= cursor.get_height()/2
screen.blit(cursor,(cursor_x,cursor_y))
pygame.display.update()
Hmm...
while char_y < 200:
char_go_y += char_fall
Unless you have some interesting aliasing I'm not seeing, if char_y < 200 (which it should be at start, it will always be since you're updating char_go_y.
If that's not the issue, would still suggest adding some prints to figure out if it's getting through the loop or not.
is there any error message in idle when you run it? whenever the screen freezes you have something wrong, but its hard to pinpoint what without knowing the error messages. maybe it is trouble opening the picture, you should try putting .convert() at the end of your picture file name, but that is just a guess.
Calling pygame.quit and sys.exit are probably causing issues. Normally you'd never need them in pygame.
Instead of:
#Main Loop
while 1:
#Tell pygame not to exceed 60 FPS
clock.tick(60)
#Events
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Do this
#Main Loop
done = False
while not done:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
done = True
if event.type == KEYDOWN:
if event.key == K_ESC:
done = True

Categories