Growing function in pygame - SnakeGame [duplicate] - python

This question already has an answer here:
How do I get the snake to grow and chain the movement of the snake's body?
(1 answer)
Closed 1 year ago.
Hi guys I started learning pygames, i started playing snake and came to an obstacle. I don't know how to make a function for my snake to grow when it eats an apple, I've looked at a lot of snake codes and I'm still not sure how to do it. I dont have idea how to do that, I realy hope you can give mi some advice to improve my game.
your help would do me good, thanks
import pygame
import random
import math
# Init
pygame.init()
# Screen
screen = pygame.display.set_mode((800, 600))
# Caption and Icon
pygame.display.set_caption("Snake Game")
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
# Background
background = pygame.image.load('background.jpg')
# Snake
snakeImg = pygame.image.load('snake.png')
snakeX = 300
snakeY = 300
snakeX_change = 0
snakeY_change = 0
# Apple
appleImg = pygame.image.load('apple.png')
appleX = random.randint(32, 768)
appleY = random.randint(32, 568)
def snake(x, y):
screen.blit(snakeImg, (x, y))
def apple(x, y):
screen.blit(appleImg, (x, y))
# Collision
def isCollision(appleX, appleY, snaketX, snakeY):
distance = distance = math.sqrt(math.pow(appleX - snakeX, 2) + (math.pow(appleY - snakeY, 2)))
if distance < 27:
return True
else:
return False
# Game Loop
score = 0
running = True
while running:
# Background Image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Snake Movment
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
snakeX_change = -0.1
snakeY_change = 0
if event.key == pygame.K_UP:
snakeY_change = -0.1
snakeX_change = 0
if event.key == pygame.K_RIGHT:
snakeX_change = 0.1
snakeY_change = 0
if event.key == pygame.K_DOWN:
snakeY_change = 0.1
snakeX_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
snakeX_change = -0.1
if event.key == pygame.K_RIGHT:
snakeX_change = 0.1
if event.key == pygame.K_DOWN:
snakeY_change = 0.1
if event.key == pygame.K_UP:
snakeY_change = -0.1
snakeX += snakeX_change
snakeY += snakeY_change
if snakeX <= 0:
snakeX = 0
elif snakeX >= 770:
snakeX = 770
if snakeY <= 0:
snakeY = 0
elif snakeY >= 570:
snakeY = 570
# Collision
collision = isCollision(appleX, appleY, snakeX, snakeY)
if collision:
score += 1
print(score)
appleX = random.randint(32, 768)
appleY = random.randint(32, 568)
snake(snakeX, snakeY)
apple(appleX, appleY)
pygame.display.flip()

When snake's head collide with the apple, add 1 element inside snake's body, you can interprete snake's body as a list of elements, containing tuples perhaps.

Related

How to collide two images in Pygame module [duplicate]

This question already has an answer here:
How to detect collisions between two rectangular objects or images in pygame
(1 answer)
Closed 2 years ago.
I’m new to programming and know the basics of Python and wanted to ask how I can perform an action if two images overlap a little bit and then a specific button is pressed in pygame.
The game looks like following:
import pygame
import random
pygame.init()
window = pygame.display.set_mode((1000, 600))
caption = pygame.display.set_caption(
'Test your reaction speed. Shoot the target game!') # sets a caption for the window
game_running = True # this is the gameloop so the window stays open
PlayerImg = pygame.image.load('F:\PythonPortable\oscn.png')
PlayerX = 370
PlayerY = 420
PlayerX_change = 0
PlayerY_change = 0
def player():
window.blit(PlayerImg, (PlayerX, PlayerY))
aim_sight = pygame.image.load('F:\PythonPortable\ktarget.png')
aim_sightX = 460
aim_sightY = 300
aim_sight_changeX = 0
aim_sight_changeY = 0
def aim_sight_function(x, y):
window.blit(aim_sight, (x, y))
targetedImg = pygame.image.load('F:\PythonPortable\ktargetedperson.png')
targetedX = random.randint(0, 872)
targetedY = random.randint(0, 200)
def random_target():
window.blit(targetedImg, (targetedX, targetedY))
while game_running:
window.fill((255, 255, 255))
random_target()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
aim_sight_changeX = -2
PlayerX_change = -2
elif event.key == pygame.K_RIGHT:
aim_sight_changeX = 2
PlayerX_change = 2
elif event.key == pygame.K_UP:
aim_sight_changeY = -2
PlayerY_change = -2
elif event.key == pygame.K_DOWN:
aim_sight_changeY = 2
PlayerY_change = 2
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
aim_sight_changeX = 0
PlayerX_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
aim_sight_changeY = 0
PlayerY_change = 0
aim_sightX += aim_sight_changeX
if aim_sightX <= 46.5:
aim_sight_changeX = 0
elif aim_sightX >= 936:
aim_sight_changeX = 0
aim_sightY += aim_sight_changeY
if aim_sightY <= 0:
aim_sight_changeY = 0
elif aim_sightY >= 400:
aim_sight_changeY = 0
PlayerX += PlayerX_change
if PlayerX <= -50:
PlayerX_change = 0
elif PlayerX >= 850:
PlayerX_change = 0
player()
aim_sight_function(aim_sightX, aim_sightY)
pygame.display.update()
I would like to know how a do what I want. I thought maybe:
if event.type == pygame.K_SPACE and targetedX == targetedX in range(aim_sightX - 100, aim_sightY + 100) or targetedY == targetedY in range (aim_sightY - 100, aim_sightY + 100):
...
It seems as though none of the tutorials I watched cover this topic and would like to recieve recommendations for tutorials that cover this problem or a direct answer.
I recommend to use a pygame.Rect objects and colliderect() to find a collision between two Surface objects. pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. The position of the rectangle can be specified by a keyword argument:
PlayerImg_rect = PlayerImg.get_rect(topleft = (PlayerX, PlayerY))
targetedImg_rect = targetedImg .get_rect(topleft = (targetedX, targetedX))
if PlayerImg_rect.colliderect(targetedImg_rect):
print("hit")

car game doesn't work properly in pygame [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
image = https://www.dropbox.com/s/nx6yzx8ddhu36m7/car.png?dl=0
when I rotate(press Left or Right Key) while I accelerate(press up key) my car moves in a strange way.
Also there is something wrong with the accelerating
speed doesn't increase the way I expect it to be. I think the speed should be increasing as the time goes on but it doesn't...
can anyone please help me by trying the code?
thank you
here's my code:
import pygame,math
pygame.init()
display_width = 1200
display_height = 800
white = (255,255,255)
black = (0,0,0)
car_image = pygame.image.load('car.png')
role_model = pygame.image.load('role_model.png')
clock = pygame.time.Clock()
FPS = 30
screen = pygame.display.set_mode([display_width,display_height])
car_width = 76
car_height = 154
def rotate(image, rect, angle):
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect(center=rect.center)
return rot_image,rot_rect
def carRotationPos(angle):
x=1*math.cos(math.radians(angle-90))
y=1*math.sin(math.radians(angle-90))
return x,y
def gameloop():
running = True
angle = 0
angle_change = 0
changeX = 0
changeY=0
x=0
y=0
change_x=0
change_y=0
speed = 1
speed_change = 1
rect = role_model.get_rect()
while running == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
angle_change = 5
#changeX=0
#changeY=0
if event.key == pygame.K_RIGHT:
angle_change = -5
#changeX=0
#changeY=0
if event.key == pygame.K_UP:
#angle_change =0
changeX=-x
changeY=y
speed_change = speed_change**2 +1
if event.key == pygame.K_DOWN:
#angle_change =0
changeX=x
changeY=-y
speed_change = -(speed_change**2 + 1)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
angle_change = 0
if event.key == pygame.K_RIGHT:
angle_change = 0
if event.key == pygame.K_UP:
changeX=0
changeY=0
speed = 1
speed_change=1
if event.key == pygame.K_DOWN:
changeX=0
changeY=0
speed = 1
speed_change=1
if angle == -360 or angle == 360:
angle = 0
angle+=angle_change
change_x+=changeX
change_y+=changeY
speed+=speed_change
if speed > 20:
speed = 20
screen.fill(white)
x,y=carRotationPos(angle)
x=round(x,5)*speed
y=round(y,5)*speed
rot_image,rot_rect=rotate(car_image,rect,angle)
rot_rect=list(rot_rect)
rot_rect1=rot_rect[0]+display_width/2-car_width/2
rot_rect2=rot_rect[1]+display_height/2-car_height/2
rot_rect1+=change_x
rot_rect2+=change_y
del rot_rect[0]
del rot_rect[1]
rot_rect.insert(0,rot_rect1)
rot_rect.insert(1,rot_rect2)
screen.blit(rot_image,rot_rect)
pygame.display.update()
clock.tick(FPS)
gameloop()
pygame.quit()
When you press UP/DOWN then you set changeX = x , changeY = y and car moves using changeX, changeY.
When you press LEFT/DOWN then you change angle and calculate new x, y but this doesn't change changeX, changeY so car still moves the same direction (using the same changeX, changeY).
EDIT: now it turns correctly when you move forward but still there is problem with backward acceleration. I'm working on it.
I use moving_up/moving_down to update changeX and changeY when car is moving - so it use current angle and x, y to change direction.
EDIT: acceleration problem solve: you have to use speed_change = speed_change**2 + 1 when you go UP and DOWN. You don't need negative value when you go DOWN because x_change = x, y_change = -y will change direction.
New code:
BTW: I add ESC to quit program and BACKSPACE to center car on screen (reset position)
import pygame
import math
# --- constants --- (UPPER_CASE)
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
DISPLAY_WIDTH = 1200
DISPLAY_HEIGHT = 800
FPS = 30
CAR_WIDTH = 76
CAR_HEIGHT = 154
# --- functions --- (lower_case)
def rotate(image, rect, angle):
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect(center=rect.center)
return rot_image, rot_rect
def rotate_car_pos(angle):
x = math.cos(math.radians(angle-90))
y = math.sin(math.radians(angle-90))
return x, y
def gameloop():
# start position - screen center - so I don't have to add center later
car_rect = role_model.get_rect(center=screen_rect.center)
# ---
angle = 0
angle_change = 0
x = 0
y = 0
x_change = 0
y_change = 0
speed = 0
speed_change = 0
# ---
pos_x = 0
pos_y = 0
#---
moving_up = False
moving_down = False
#recalculate = True
running = True
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_BACKSPACE:
# reset position [for test only]
pos_x = 0
pos_y = 0
angle = 0
elif event.key == pygame.K_LEFT:
angle_change = 5
elif event.key == pygame.K_RIGHT:
angle_change = -5
elif event.key == pygame.K_UP:
moving_up = True
x_change = -x
y_change = y
speed_change = speed_change**2 + 1
elif event.key == pygame.K_DOWN:
moving_down = True
x_change = x
y_change = -y
speed_change = speed_change**2 + 1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
angle_change = 0
elif event.key == pygame.K_RIGHT:
angle_change = 0
elif event.key == pygame.K_UP:
moving_up = False
x_change = 0
y_change = 0
speed = 0
speed_change = 0
elif event.key == pygame.K_DOWN:
moving_down = False
x_change = 0
y_change = 0
speed = 0
speed_change = 0
# --- updates ---
# - pos -
if x_change or y_change:
pos_x += x_change
pos_y += y_change
print('[DEBUG]: pos_x, pos_y: ', pos_x, pos_y)
# - angle -
# rotate olny when moving
#if moving_up or moving_down:
if angle_change:
angle += angle_change
while angle > 360:
angle -= 360
while angle < -360:
angle += 360
print('[DEBUG]: angle: ', angle)
# - speed -
if speed_change:
speed += speed_change
if speed > 20:
speed = 20
print('[DEBUG]: speed: ', speed)
# - others -
x, y = rotate_car_pos(angle)
x = round(x*speed, 5)
y = round(y*speed, 5)
print('[DEBUG]: x, y: ', x, y)
if moving_up:
x_change = -x
y_change = y
elif moving_down:
x_change = x
y_change = -y
#if recalculate:
rot_image, rot_rect = rotate(car_image, car_rect, angle)
rot_rect.centerx += pos_x
rot_rect.centery += pos_y
# --- draws ---
screen.fill(WHITE)
screen.blit(rot_image, rot_rect)
pygame.display.update()
# --- clock ---
clock.tick(FPS)
# --- main ---
pygame.init()
car_image = pygame.image.load('car.png')
role_model = pygame.image.load('car.png')
screen = pygame.display.set_mode( (DISPLAY_WIDTH, DISPLAY_HEIGHT) )
screen_rect = screen.get_rect()
clock = pygame.time.Clock()
gameloop()
pygame.quit()

How do I know when a cube collide with another moving cube?

I'm trying to create a little game as a training, but I'm blocked because I don't know how I can collide 2 moving cubes.
The game is simple, there is a red box that you can move and if this box touches a green cube, then you lost. (the green cubes are always moving)
I tried to read some documentations but it's not really easy to understand as a beginner.
Here is the code:
import pygame
import random
from threading import Timer
pygame.init()
screenWidth = 1100
screenHeight = 600
white = (255,255,255)
red = (255, 0, 0)
yellow = (50, 250, 20)
FPS = 60
gameDisplay = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption('Tekken')
pygame.display.update()
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 28)
class Players:
def __init__(self, playerName, playerAttribute, cubeheight, cubewidth, missilesHeight, missilesWidth):
self.playerName = playerName
self.playerAttribute = playerAttribute
self.playerLife = 100
self.droite_x = 300
self.droite_y = 600
self.cubeheight = cubeheight
self.cubewidth = cubewidth
self.missiles = True
self.missilesHeight = missilesHeight
self.missilesWidth = missilesWidth
self.missiles_droite_x = 0
self.missiles_droite_y = round(random.randrange(50, screenHeight-50))
self.missiles_droite_x_inverse = screenWidth-50
self.missiles_droite_y_inverse = round(random.randrange(50, screenHeight-50))
self.vitesse_missiles = 10
print(self.playerName, self.playerAttribute, self.playerLife)
def environment_un(self):
gameExit = False
gameOver = False
droite_x_change = 0
droite_y_change = 0
missiles_droite_x_change = 0
missiles_droite_x_change_inverse = 0
while not gameExit:
while gameOver:
gameDisplay.fill(red)
screen_text = font.render("Game Over, do you want to play again? [Q] to quit", True, white)
gameDisplay.blit(screen_text, [100, 300])
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameOver = False
gameExit = True
break
if event.type == pygame.QUIT:
gameOver = False
gameExit = True
break
for event in pygame.event.get(): #va chercher les events
if event.type == pygame.QUIT: #Si j'appuie sur X
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
droite_x_change = -3
if event.key == pygame.K_RIGHT:
droite_x_change = +3
if event.key == pygame.K_UP:
droite_y_change = -3
if event.key == pygame.K_DOWN:
droite_y_change = +3
if event.key == pygame.K_SPACE:
missiles_droite_x_change = self.vitesse_missiles
missiles_droite_x_change_inverse = -self.vitesse_missiles
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
droite_x_change = 0
if event.key == pygame.K_RIGHT:
droite_x_change = 0
if event.key == pygame.K_UP:
droite_y_change = 0
if event.key == pygame.K_DOWN:
droite_y_change = 0
self.missiles_droite_x_inverse += missiles_droite_x_change_inverse
self.missiles_droite_x += missiles_droite_x_change
self.droite_x += droite_x_change
self.droite_y += droite_y_change
if self.droite_y + self.cubeheight <= 0:
self.droite_y = 0
elif self.droite_y + self.cubeheight >= screenHeight:
self.droite_y = screenHeight-self.cubeheight
elif self.droite_x + self.cubewidth <= 0:
self.droite_x = 0
elif self.droite_x + self.cubewidth >= screenWidth:
self.droite_x = screenWidth-self.cubewidth
gameDisplay.fill(white)
gameDisplay.fill(red, rect=[self.droite_x, self.droite_y, self.cubewidth, self.cubeheight])
gameDisplay.fill(yellow, rect=[self.missiles_droite_x, self.missiles_droite_y, self.missilesWidth, self.missilesHeight])
gameDisplay.fill(yellow, rect=[self.missiles_droite_x_inverse, self.missiles_droite_y_inverse, self.missilesWidth, self.missilesHeight])
pygame.display.update()
if self.missiles_droite_x + self.missilesWidth >= screenWidth:
missiles_droite_x_change = 0
if missiles_droite_x_change == 0:
self.missiles_droite_x = 0
self.missiles_droite_y = round(random.randrange(50, screenHeight-50))
missiles_droite_x_change = self.vitesse_missiles
if self.missiles_droite_x_inverse <= 0:
missiles_droite_x_change_inverse = 0
if missiles_droite_x_change >= 0:
self.missiles_droite_x_inverse = screenWidth-50
self.missiles_droite_y_inverse = round(random.randrange(50, screenHeight-50))
missiles_droite_x_change_inverse = -12
clock.tick(FPS)
pygame.quit()
Player_1 = Players('John', 'sometext', 50, 50, 100, 100)
Player_1.environment_un()
What should do I in order to detect the collision?
I can not run your code at the moment as I dont have pygame installed. However, you can use the pygame.sprite.collide_rect() if you declare your objects to have in their class an pygame.sprite.Sprite-object or inherit from that class (as suggested below). The code below may note work as I can not test it but it should be close to a functioning code snippet. In the case you would like to test collision of a sprite against multiple other sprites - consider looking at pygame.sprite.Group(). I believe that something like this should work:
class SpriteObject(pygame.sprite.Sprite):
def __init__(self,pos_x, pos_y):
pygame.sprite.Sprite.__init__(self)
self.rect = self.original.get_rect()
self.rect.center = (pos_x, pos_y)
class Players:
def __init__(self, playerName, playerAttribute, cubeheight, cubewidth, missilesHeight, missilesWidth):
sprite1 = SpriteObject(1,2)
sprite2 = SpriteObject(1,2)
sprite1.rect.collide_rect(sprite2)
If you are looking for a conceptual answer:
Since you are considering just cubes and if they are of the same size, two cubes will occupy the same space 'if and only if' a corner of one cube is between (inclusive) two parallel planes of another. There are many ways to do this in practice.
I would check if between by evaluating an inward normal vector of cube 1 dotted with a vector to a corner (of cube 2) from any corner (of cube 1) . Do so for both parallel sides. If both are positive, its inside.
It's slightly more complicated for different shapes and varying sizes.
Use pygame.Rect() to keep cube position and size - and then you can use pygame.Rect.colliderect() to check collision between two cubes.
cube1 = pygame.Rect((x1, y1), (width, height))
cube2 = pygame.Rect((x2, y2), (width, height))
if cube1.colliderect(cube2):
print("Collision !")
PyGame has other usefull classes - pygame.sprite.Sprite and pygame.sprite.Group - which use Rect and collision detection functions.

How to create a border in Pygame

I would like to know how to create a border in Pygame to stop the user controlled object from exiting the screen. Right now, I only have it so python prints some text when the user controlled object has come near one of the 4 sides.
Here is my code so far.
import pygame
from pygame.locals import *
pygame.init()
#Display Stuff
screenx = 1000
screeny = 900
screen = pygame.display.set_mode((screenx,screeny))
pygame.display.set_caption('Block Runner')
clock = pygame.time.Clock()
image = pygame.image.load('square.png')
#Color Stuff
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
black = (0,0,0)
#Variables
x_blocky = 50
y_blocky = 750
blocky_y_move = 0
blocky_x_move = 0
#Animations
def Blocky(x_blocky, y_blocky, image):
screen.blit(image,(x_blocky,y_blocky))
#Game Loop
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
blocky_y_move = -3
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
blocky_y_move = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
blocky_y_move = 3
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
blocky_y_move = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
blocky_x_move = 3
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
blocky_x_move = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
blocky_x_move = -3
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
blocky_x_move = 0
if x_blocky > 870 or x_blocky < 0:
print(' X Border')
if y_blocky > 750 or y_blocky < 2:
print(' Y Border')
y_blocky += blocky_y_move
x_blocky += blocky_x_move
screen.fill(white)
Blocky(x_blocky, y_blocky, image)
pygame.display.update()
clock.tick(60)
Don't use integers to store your position. Use a Rect.
So instead of
x_blocky = 50
y_blocky = 750
use
blocky_pos = pygame.rect.Rect(50, 750)
Now you can simply use
blocky_pos.move_ip(blocky_x_move, blocky_y_move)
to move your object.
After moving, you can simply call clamp/clamp_ip to ensure the blocky_pos Rect is always inside the screen.
blocky_pos.clamp_ip(screen.get_rect())
Also, you don't need to define basic colors yourself, you could simply use pygame.color.Color('Red') for example.
I also suggest you use pygame.key.get_pressed() to get all pressed keys to see how to move your object instead of creating 1000 lines of event handling code.
Well, simply don't increase your move variable any further, if you detect that the user object is near or at the border. Or reverse the move direction, depending on your general intent.
if x_blocky > 870 or x_blocky < 0:
print(' X Border')
blocky_x_move = 0
if y_blocky > 750 or y_blocky < 2:
print(' Y Border')
blocky_y_move = 0
Also, you have some redundant code with your keyboard movement. Instead of writing
if event.type == KEYDOWN:
over and over again, group the KEYUP if statements and KEYDOWN if statements.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
blocky_y_move = -3
elif event.key == pygame.K_DOWN:
blocky_y_move = +3
etc, and:
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
blocky_y_move = 0
elif event.type == pygame.K_DOWN:
blocky_y_move = 0
etc
You can set the boundaries using the min and max functions.
Here is the concept:
We have a pygame object that moves in all four directions; lets say the user holds down the LEFT arrow key, so that the object reaches the top of the screen. The y-coordinate of the top of the screen will always be 0, so we want the object to come to a stop at y-coordinate 0.
This may seem as simple as:
if char.rect.y > 0:
char.rect.y -= char.speed
But this will result in a bug ig char.speed is greater than 1. Like when the object is at y-coordinate 5,
and its speed is 10; the condition still allows for one more step for the object, resulting in the object
coming 5 pixels out of the pygame window. What we want to do is more like:
if char.rect.y > 0:
char.rect.y -= char.speed
if char.rect.y < 0:
char.rect.y = 0
to push the object back into the boundaries. The above block of code can be simplified with the max function:
self.rect.y = max([self.rect.y - self.speed, 0])
For the object moving down:
if char.rect.y < HEIGHT - char.height:
char.rect.y += char.speed
if char.rect.y > HEIGHT - char.height:
char.rect.y = HEIGHT - char.height
or, the more efficient and clean method:
self.rect.y = min([self.rect.y + self.speed, HEIGHT - self.height])
For going left and right, simply replace the ys and height (and HEIGHT) from two lines above with xs and widths (and WIDTH).
All together:
import pygame
pygame.init()
WIDTH = 600
HEIGHT = 600
wn = pygame.display.set_mode((WIDTH, HEIGHT))
class Player:
def __init__(self):
self.speed = 1
self.width = 20
self.height = 20
self.color = (255, 255, 0)
self.rect = pygame.Rect((WIDTH - self.width) / 2, (HEIGHT - self.height) / 2, 20, 20)
def up(self):
self.rect.y = max([self.rect.y - self.speed, 0])
def down(self):
self.rect.y = min([self.rect.y + self.speed, HEIGHT - self.height])
def left(self):
self.rect.x = max([self.rect.x - self.speed, 0])
def right(self):
self.rect.x = min([self.rect.x + self.speed, WIDTH - self.width])
def draw(self):
pygame.draw.rect(wn, self.color, self.rect)
char = Player()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
char.up()
if keys[pygame.K_DOWN]:
char.down()
if keys[pygame.K_LEFT]:
char.left()
if keys[pygame.K_RIGHT]:
char.right()
wn.fill((0, 0, 0))
char.draw()
pygame.display.update()
Good luck!

How can I make the movement smoother?

I'm developing a very basic engine, basing myself on tutorials from Pygame, and I'm having a little problem with "smoothness". How do I make my player walk "smoother"?
My event handler is pretty basic, pretty standard, nothing new, and I even figured out how to make a "boost" (run) for test. But the thing is, at the pygame.KEYUP, those lots of zeros destroy the "smoothness" of my little player, and I don't want that, but I don't want it to walk ad infinitum.
import pygame
import gfx
# Main Class
class Setup:
background = gfx.Images.background
player = gfx.Images.player
pygame.init()
# Configuration Variables:
black = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)
title = "Ericson's Game"
# Setup:
size = [700,700]
screen = pygame.display.set_mode(size)
pygame.display.set_caption(title)
done = False
clock = pygame.time.Clock()
# Logic Variables
x_speed = 0
y_speed = 0
x_speed_boost = 0
y_speed_boost = 0
x_coord = 350
y_coord = 350
screen.fill(white)
# Main Loop:
while done == False:
screen.blit(background,[0,0])
screen.blit(player,[x_coord,y_coord])
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
if event.key == pygame.K_a:
x_speed = -6
x_speed_boost = 1
if event.key == pygame.K_d:
x_speed = 6
x_speed_boost = 2
if event.key == pygame.K_w:
y_speed = -6
y_speed_boost = 1
if event.key == pygame.K_s:
y_speed = 6
y_speed_boost = 2
if event.key == pygame.K_LSHIFT:
if x_speed_boost == 1:
x_speed = -10
if x_speed_boost == 2:
x_speed = 10
if y_speed_boost == 1:
y_speed = -10
if y_speed_boost == 2:
y_speed = 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
x_speed = 0
x_speed_boost = 0
if event.key == pygame.K_d:
x_speed = 0
x_speed_boost = 0
if event.key == pygame.K_w:
y_speed = 0
y_speed_boost = 0
if event.key == pygame.K_s:
y_speed = 0
y_speed_boost = 0
x_coord = x_coord + x_speed
y_coord = y_coord + y_speed
pygame.display.flip()
pygame.display.update()
clock.tick(20)
pygame.quit()
Code will be simpler/clearer using keystate polling for your use. If other parts of the game use 'on press' logic, you can use event handling. So your movement would be:
If you are calling pygame.display.flip() then you don't use pygame.display.update(). Infact it will probably slow it down to use both.
I used your x_coord variable. But it would simplify things to use a tuple or vector for player location. You can use a float, for smoother precision for movement. Then it blits as an int to screen.
while not done:
for event in pygame.event.get():
# any other key event input
if event.type == QUIT:
done = True
elif event.type == KEYDOWN:
if event.key == K_ESC:
done = True
vel_x = 0
vel_y = 0
speed = 1
if pygame.key.get_mods() & KMOD_SHIFT
speed = 2
# get key current state
keys = pygame.key.get_pressed()
if keys[K_A]:
vel_x = -1
if keys[K_D]:
vel_x = 1
if keys[K_W]:
vel_y = -1
if keys[K_S]:
vel_y = 1
x_coord += vel_x * speed
y_coord += vel_y * speed
You are ticking the clock at a rate of 20 frames per second. This is probably causing the choppiness. Change it to something bigger like 70:
clock.tick(70)

Categories