I'm new to python/pygame and I can't figure this out. Whenever I press and hold a key it won't loop the KEYDOWN. Also, if I hold the key on my keyboard down and move the mouse at the same time, it seems to move continuously.
Can someone tell me what I'm doing wrong?
import pygame
import random
pygame.init()
#Colors
white = 255, 255, 255
black = 0, 0, 0
back_color = 48, 255, 124
light_color = 34, 155, 78
#Display W/H
display_width = 800
display_height = 600
#X/Y
x_axis = 400
y_axis = 580
Block_size = 20
x_int = 0
y_int = 0
ON = True
Window = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Game')
#On Loop
while ON == True:
#Screen Event
for Screen in pygame.event.get():
#Quit Screen
if Screen.type == pygame.QUIT:
pygame.quit()
exit()
#Quit Full Screen
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_q:
pygame.quit()
exit()
#Full Screen !!!!!!!! EDIT THIS !!!!!!!!
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_1:
pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)
if Screen.key == pygame.K_2:
pygame.display.set_mode((display_width, display_height))
#Player Movement K DOWN
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_d:
x_int = 20
if Screen.key == pygame.K_a:
x_int = -20
#Player Movement K UP
if Screen.type == pygame.KEYUP:
if Screen.key == pygame.K_d or Screen.key == pygame.K_a:
x_int = 0
x_axis += x_int
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
quit()
I have improved your code. You have placed the screen update (drawing the screen) portion in the events loop whereas it should be in the while loop. The code I have mode is a bit complex but works as expected. Why it is complex? When the key is held down, then the events list is empty (you can print the events). I have also made the block not to go out of the screen. The speed of the block was high so I decreased it to 10.
import pygame
import random
pygame.init()
#Colors
white = 255, 255, 255
black = 0, 0, 0
back_color = 48, 255, 124
light_color = 34, 155, 78
#Display W/H
display_width = 800
display_height = 600
#X/Y
x_axis = 400
y_axis = 580
global x_int
Block_size = 20
x_int = 0
y_int = 0
ON = True
Window = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Game')
global topass_a,topass_d
x_int,topass_a,topass_d = 0,0,0
#On Loop
while ON:
#Screen Event
events = pygame.event.get()
def on_a_press():
global topass_a,x_int
x_int = -10
topass_a = 1
def on_d_press():
global topass_d,x_int
x_int = 10
topass_d = 1
if len(events) == 0:
if topass_a == 1:on_a_press()
if topass_d == 1:on_d_press()
for Screen in events:
if Screen.type == pygame.QUIT:
pygame.quit()
exit()
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_q:
pygame.quit()
exit()
if Screen.key == pygame.K_1:
pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)
if Screen.key == pygame.K_2:
pygame.display.set_mode((display_width, display_height))
if Screen.key == pygame.K_d or topass_d == 1:
on_d_press()
if Screen.key == pygame.K_a or topass_a == 1:
on_a_press()
if Screen.type == pygame.KEYUP:
if Screen.key == pygame.K_d or Screen.key == pygame.K_a:
x_int = 0
topass_a = 0
topass_d = 0
x_axis += x_int
x_int = 0
if x_axis < 0:x_axis=0
elif x_axis >= display_width-Block_size:x_axis = display_width-Block_size
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
You can further improve the code as you need.
Edit:
Why complex? Easy things come first. I have realized that there is no need to track the keys. pygame.key.get_pressed() returns the pressed key. Here is a smaller , better and improved code. I have also implemented the w and s (y_axis) keys.
import pygame
import random
pygame.init()
#Colors
white = 255, 255, 255
black = 0, 0, 0
back_color = 48, 255, 124
light_color = 34, 155, 78
#Display W/H
display_width = 800
display_height = 600
#X/Y
x_axis = 400
y_axis = 580
Block_size = 20
x_int = 0
y_int = 0
ON = True
Window = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Game')
while ON:
events = pygame.event.get()
for Screen in events:
if Screen.type == pygame.QUIT:
pygame.quit()
exit()
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_q:
pygame.quit()
exit()
if Screen.key == pygame.K_1:
pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)
if Screen.key == pygame.K_2:
pygame.display.set_mode((display_width, display_height))
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
x_int = -10
if keys[pygame.K_d]:
x_int = 10
# keys controlling y axis, you can remove these lines
if keys[pygame.K_w]:
y_int = -10
if keys[pygame.K_s]:
y_int = 10
#x_axis......
x_axis += x_int
x_int = 0
if x_axis < 0:x_axis=0
elif x_axis >= display_width-Block_size:x_axis = display_width-Block_size
#y axis
y_axis += y_int
y_int = 0
if y_axis < 0:y_axis=0
elif y_axis >= display_height-Block_size:y_axis = display_height-Block_size
#updaing screen
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
You only receive pygame.KEYDOWN when the key is first pressed - not while it is held down. The simple solution is to only draw while the key is down (ie. when x_int != 0)
#On Loop
while ON == True:
#Screen Event
for Screen in pygame.event.get():
# <Removed not relevant code for brevity>
#Player Movement K DOWN
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_d:
x_int = 20
if Screen.key == pygame.K_a:
x_int = -20
#Player Movement K UP
if Screen.type == pygame.KEYUP:
if Screen.key == pygame.K_d or Screen.key == pygame.K_a:
x_int = 0
# Render only happens if x_int is not zero
# (Need to add code to force render first time)
if x_int:
x_axis += x_int
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
As the program grows and gets more complex, you'll need better logic for when to render, but this will get you started.
Related
I'm attempting to detect a collision between two objects (both images).
I have rect's for both the objects, but cannot get the collision to be detected.
I've attempted to use rect's, comparing x and y positions.
import sys
import pygame
from pygame.locals import *
#init pygame
pygame.init()
window_width = 840
window_height = 650
size = (window_width, window_height)
screen = pygame.display.set_mode(size)
bg_img = pygame.image.load("img.png").convert_alpha()
crash = False
bot1x = 150
bot1y = 100
x = (window_width * 0.45)
y = (450)
clock = pygame.time.Clock()
fr = clock.tick(30)
x_speed = 0
car_img = pygame.image.load("car.png").convert_alpha()
car_img = pygame.transform.scale(car_img, (125, 175))
bot_img = pygame.image.load("bot.png").convert_alpha()
bot_img = pygame.transform.scale(bot_img, (175, 200))
def car(x, y):
screen.blit(car_img, (x,y))
def bot(x, y):
screen.blit(bot_img, (x,y))
car_rect = pygame.Rect(x, y, 125, 175)
bot_rect = pygame.Rect(bot1x, bot1y, 175, 200)
#game loop
while(crash==False):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == K_LEFT:
x_speed = -.9
print("Moving Left")
if event.key == K_RIGHT:
x_speed = .9
print("Moving Right")
if event.type == pygame.KEYUP:
if event.key == K_LEFT:
x_speed = 0
if event.key == K_RIGHT:
x_speed= 0
if event.type == pygame.QUIT:
crash = True
bot1y += .5
# Move car
x += x_speed
if car_rect.colliderect(bot_rect):
print("collision deceted")
#blit images to screen
screen.blit(bg_img, [0, 0])
#spawn car
car(x,y)
bot(bot1x, bot1y)
#flip game display
pygame.display.flip()
#end game
pygame.quit()
quit()
I expect the game to output "Collision detected" when the two objects touch eachother.
Thanks,
Lachlan
I'm trying to make the snake game in python, but sadly my snake and my apples aren't appearing on the screen, all i see is a gray screen with nothing on it, can you help me out? thanks!
P.S - i would like an explanation as well about why my current code isn't working so i would avoid it in the future, thanks again.
import pygame, sys, random, time
from pygame.locals import *
pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 500
windowHeight = 500
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
red = (255, 0, 0)
green = (0, 255, 0)
color = (100, 100, 100)
snakeHead = [250, 250]
snakePosition = [[250, 250],[240, 250],[230, 250]]
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
def collisionBoundarias(snakeHead):
if snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0:
return 1
else:
return 0
def collisionSelf(SnakePosition):
snakeHead = snakePosition[0]
if snakeHead in snakePosition[1:]:
return 1
else:
return 0
while True:
windowSurface.fill(color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
snakeHead[0] += 10
if event.type == pygame.K_LEFT or event.key == pygame.K_a:
snakeHead[0] -= 10
if event.type == pygame.K_UP or event.type == pygame.K_w:
snakeHead[1] += 10
if event.type == pygame.K_DOWN or event.type == pygame.K_s:
snakeHead[1] -= 10
def displaySnake(snakePosition):
for position in snakePosition:
pygame.draw.rect(windowSurface ,red,pygame.Rect(position[0],position[1],10,10))
def display_apple(windowSurface, applePosition, apple):
windowSurface.blit(apple ,(applePosition[0], applePosition[1]))
snakePosition.insert(0,list(snakeHead))
snakePosition.pop()
def displayFinalScore(displayText, finalScore):
largeText = pygame.font.Font('freesansbold.ttf',35)
TextSurf = largeText.render(displayText, True, (255, 255, 255))
TextRect = TextSurf.get_rect()
TextRect.center = ((windowWidth/2),(windowHeight/2))
windowSurface.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
def collisionApple(applePosition, score):
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
score += 1
return applePosition, score
if snakeHead == applePosition:
applePosition, score = collisionApple(applePosition, score)
snakePosition.insert(0,list(snakeHead))
pygame.display.update()
clock = pygame.time.Clock()
clock.tick(20)
As mentioned in the comments, the objects don't appear because you never draw them by calling the displaySnake and display_apple functions. It also makes no sense to define these functions in the while loop again and again.
Here's a fixed version of the code. I've changed the movement code, so that the snake moves continually each frame. (It could still be improved, but I tried to keep it simple.)
import pygame, sys, random, time
from pygame.locals import *
pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 500
windowHeight = 500
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
clock = pygame.time.Clock()
red = (255, 0, 0)
green = (0, 255, 0)
color = (100, 100, 100)
snakeHead = [250, 250]
snakePosition = [[250, 250],[240, 250],[230, 250]]
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
apple = pygame.Surface((10, 10))
apple.fill(green)
score = 0
speed = 10
# Define a velocity variable (a list or better a pygame.Vector2).
velocity = pygame.Vector2(speed, 0)
def collisionBoundarias(snakeHead):
return snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0
def collisionApple(applePosition, score):
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
score += 1
return applePosition, score
def displaySnake(snakePosition):
for position in snakePosition:
pygame.draw.rect(windowSurface, red, pygame.Rect(position[0], position[1], 10, 10))
def display_apple(windowSurface, applePosition, apple):
windowSurface.blit(apple, (applePosition[0], applePosition[1]))
while True:
# Event handling.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
# Replace `type` with `key`.
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
velocity.x = speed
velocity.y = 0
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
velocity.x = -speed
velocity.y = 0
if event.key == pygame.K_UP or event.key == pygame.K_w:
velocity.x = 0
velocity.y = -speed
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
velocity.x = 0
velocity.y = speed
# Game logic.
snakeHead[0] += velocity.x # Update the x-position every frame.
snakeHead[1] += velocity.y # Update the y-position every frame.
snakePosition.insert(0, snakeHead)
snakePosition.pop()
if snakeHead == applePosition:
applePosition, score = collisionApple(applePosition, score)
snakePosition.insert(0, snakeHead)
# Drawing.
windowSurface.fill(color)
displaySnake(snakePosition)
display_apple(windowSurface, applePosition, apple)
pygame.display.update()
clock.tick(20)
I'm working on the basics of a game right now, but for some reason I cannot get my sprite to move. I know that it's registering when I'm pressing the key down, but the sprite for some reason just stays still.
import pygame
import sys
from pygame.locals import *
import time
pygame.init()
black = (0, 0, 0)
white = (255, 255, 255)
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("RADICAL")
screen.fill(black)
imga = pygame.image.load('coina.png')
imgb = pygame.image.load('coinb.png')
sound = pygame.mixer.Sound('coin.mp3')
FPS = 80
imgx = 10
imgy = 10
pixMove = 10
steps = 0
x1 = 0
y1 = 0
keystate = pygame.key.get_pressed()
GameOver = False
while not GameOver:
screen.fill(white)
points = (steps)
font = pygame.font.SysFont(None, 30)
text = font.render('Score: '+str(points), True, black)
screen.blit(text, (0,0))
if steps % 2 == 0:
screen.blit(imga, (imgx, imgy))
else:
screen.blit(imgb, (imgx, imgy))
for event in pygame.event.get():
print event
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if keystate[pygame.K_UP]:
y1 -= pixMove
elif keystate[pygame.K_DOWN]:
y1 += pixMove
elif keystate[pygame.K_LEFT]:
x1 -= pixMove
elif keystate[pygame.K_RIGHT]:
x1 += pixMove
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x1 = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y1 = 0
steps +=1
if event.type == K_SPACE:
sound.play()
time.sleep(1)
sound.stop()
pygame.display.update()
fpsTime.tick(FPS)
Generally you blit() images on to the screen (pygame.display.set_mode()), for the changes to to reflect on the screen we call pygame.display.update(), In your case the game never comes the statement, which is out of while loop.
I have been coding a program, a survival game, on Python. I seem to have an issue with adding Images/Sprites. I am noticing the images blink. Any way to fix this?
import os
import pygame
import time
import random
from pygame.locals import *
launchLog = pygame.init()
print(launchLog)
white = (255,255,255)
black = (0,0,0)
red=(255,0,0)
blue=(0,0,255)
green=(0,255,0)
skin = (236,227,100)
size = 10
dependant = (green)
rate = 0.0018
weight = 100
bound_x = 600
bound_x2 = bound_x-size
bound_y = 600
bound_y2 = bound_y-size
screen = pygame.display.set_mode((bound_x,bound_y))
pygame.display.set_caption("Survival: EfEs Edition")
clock = pygame.time.Clock()
font = pygame.font.SysFont(None,25)
x = 300
y = 300
roundX = x
roundY = y
img=pygame.image.load("pewds.png")
def hmn(x,y,size):
clothes = pygame.draw.rect(screen,skin,[x,y,size,size-size*2])
snake = pygame.draw.rect(screen, red, [x,y,size,size])
def mesg(msg,color):
txt = font.render(msg, True, color)
screen.blit(txt, [x,y-30])
display.flip(img)
def gameLoop():
global x
global y
jump = 1
quitted = False
starved = 100
eaten = False
restart = False
lead_change = 0
lead_y = 0
randF_x = random.randrange(0,bound_x)
randF_y = random.randrange(0,bound_y)
didX2=round(randF_x/10.0)*10.0
didY2=round(randF_y/10.0)*10.0
while not quitted:
screen.blit(img,(x,y -15))
screen.blit(img,(x,y -15))
pygame.display.update()
hmn(x,y,size)
starved=starved-rate
#print(starved)
if starved<0:
screen.fill(white)
mesg("You died of hunger! Press R to restart.",red)
pygame.display.flip()
time.sleep(0.3)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
starved = 100
pygame.display.update()
rate = 0.0018
x=300
y=300
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.display.update()
quitted = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
lead_change = -.2
elif event.key == pygame.K_d:
lead_change = .2
elif event.key == pygame.K_w:
lead_y = .2
elif event.key == pygame.K_s:
lead_y = -0.2
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_a:
lead_change = 0
#print(x)
#print(y)
elif event.key == pygame.K_ESCAPE:
#print("Quitting")
quitted = True
elif event.key == pygame.K_w or event.key == pygame.K_s:
#print(y)
lead_y=0
x += lead_change
y -= lead_y
roundX = x
roundY = y
global roundX
global roundY
didX=round(roundX/10)*10
didY=round(roundY/10)*10
screen.fill(white)
s = pygame.draw.rect(screen, red, [bound_x2/15,bound_y2/20, starved * 2, 50])
pygame.draw.rect(screen, dependant, [didX2,didY2,10,10])
pygame.display.update()
#boundary script
if x>bound_x2:
x=bound_x2
elif x<-1:
x=-1
elif y<2:
y=2
elif y>590:
y=590
hmn(x,y,size)
pygame.display.flip()
if didX == didX2 and didY==didY2:
didX2 = round(random.randrange(0,bound_x)/10)*10
didY2 = round(random.randrange(0,bound_y)/10)*10
global dependant
dependant = green
global starved
starved = starved +0.01
global weight
weight = weight + 3
elif weight>150:
mesg("Weight increased! You now get hungry faster.",black)
pygame.display.update()
global rate
rate = rate+0.0008
pygame.display.update()
clock.tick(55)
mesg("Leaving game", black)
screen.blit(img,(x,y-15))
pygame.display.update()
time.sleep(2)
pygame.quit()
quit()
gameLoop()`
Please excuse the bad coding, the game is only in it's earliest state.
The correct sequence is to:
draw everything you want to show on a frame (all the blit()s, draw()s), etc.
do only one of display.update() with a list of all the changed regions OR display.flip() to update the whole screen once you've drawn everything
tl;dr - don't mix draw()s with update()s.
I am following a youtube tutorial series, and I came across this problem with my pygame game. I made a function called messagetoscreen, and it works fine in the video, but it doesn't work for me.
Here is my code:
#Imports
import pygame
pygame.init()
pygame.font.init()
import sys
import random
import cx_Freeze
import time
#Variables
playerhealth = 100
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0 ,255, 0)
windowtitle = "Climber"
sizex = 1000
sizey = 700
rect1x = 500
rect1y = 350
rect1sizex = 50
rect1sizey = 50
rect1xchange = 0
rect1ychange = 0
clock = pygame.time.Clock()
fps = 600
font = pygame.font.SysFont(None, 25)
#Functions
def messagetoscreen (msg,color):
screentext = font.render(msg, True, color)
gamedisplay.blit(screentext , [sizex / 2, sizey / 2])
#Initialization
gamedisplay = pygame.display.set_mode((sizex, sizey))
pygame.display.set_caption(windowtitle)
#Game Loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
rect1ychange -= 1
elif event.key == pygame.K_a:
rect1xchange -= 1
elif event.key == pygame.K_s:
rect1ychange += 1
elif event.key == pygame.K_d:
rect1xchange += 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
rect1ychange = 0
elif event.key == pygame.K_a:
rect1xchange = 0
elif event.key == pygame.K_s:
rect1ychange = 0
elif event.key == pygame.K_d:
rect1xchange = 0
if rect1y > 650:
rect1y = 650
if rect1x > 950:
rect1x = 950
if rect1y < 0:
rect1y = 0
if rect1x < 0:
rect1x = 0
rect1x += rect1xchange
rect1y += rect1ychange
messagetoscreen("HAPPY", red)
gamedisplay.fill(white)
pygame.draw.rect(gamedisplay, green, (rect1x, rect1y, rect1sizex, rect1sizey))
pygame.display.update()
clock.tick(fps)
I would like to know how to fix my function, and anything else that could cause an error.
The answer is that I blitted the text to the screen before I filled gamedisplay with white. Remember to blit text after drawing a background.
Here is the code that draw graphics
in the while loop:
gamedisplay.fill(white)
pygame.draw.rect(gamedisplay, green, (rect1x, rect1y, rect1sizex, rect1sizey))
messagetoscreen("HAPPY", red)