Unable to properly refresh display in Pong game - python

import pygame, sys
pygame.init()
display_width = 800
display_height = 500
white = (255, 255, 255)
display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pong')
clock = pygame.time.Clock()
platform_y = display_height * 0.38
def platform(color, x, y):
global platform_y
pygame.draw.rect(display, color, (x, y, 25, 100))
pygame.display.update()
clock.tick(80)
def ball():
pygame.draw.circle(display, white, (400, 250), 12)
pygame.display.update()
def game():
global platform_y
y_change = 0
while True:
platform(white, 100, platform_y)
platform(white, 675, platform_y)
ball()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
sys.exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change -= 2
if event.key == pygame.K_DOWN:
y_change += 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
platform_y += y_change
pygame.display.update()
clock.tick(80)
if __name__ == '__main__':
game()
I guess it's not the best code but I've been messing around with pygame and got to the point when I have to create Pong, although I'm not sure why the rectangles (platforms) are just getting higher instead of going up and down (I know that both platforms will rise together, will fix that, this is just for test)

You had couple of issues with your code:
You have to call pygame.display.update only once, not after every pygame.draw,
clock.tick is set in main game loop, not in other places,
You have to clear the screen before drawing.
Here is what will probably work as you expect:
import pygame, sys
pygame.init()
display_width = 800
display_height = 500
white = (255, 255, 255)
display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pong')
clock = pygame.time.Clock()
platform_y = display_height * 0.38
def platform(color, x, y):
global platform_y
pygame.draw.rect(display, color, (x, y, 25, 100))
def ball():
pygame.draw.circle(display, white, (400, 250), 12)
def game():
global platform_y
y_change = 0
while True:
platform(white, 100, platform_y)
platform(white, 675, platform_y)
ball()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
sys.exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change -= 2
if event.key == pygame.K_DOWN:
y_change += 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
platform_y += y_change
pygame.display.update()
clock.tick(80)
display.fill((0, 0, 0))
if __name__ == '__main__':
game()
Couple of advices:
Don't use global! It's smells on bad code. There are certainly better ways to achieve what you wanted without using it.
Try to avoid a lot of conditions nesting. It's very hard to read, and will require much mental energy to read after day or two.
Happy coding!

Related

Snake game: snake and apples do not appear on the screen

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)

Sprite change when key is pressed

I've been following an youtube tutorial to begin with python and pygmae but started doing modifications before finishing it(https://www.youtube.com/watch?v=dX57H9qecCU&index=5&list=PLQVvvaa0QuDdLkP8MrOXLe_rKuf6r80KO). I wanted to add this same "sprite change when pressed" (how to change a sprite on the screen after pressing a key in pygame) to my code but i get nothing. the game just runs as if there was no code at all for this.
import pygame
import time
pygame.init() #as important as import pygame, always init pygame
display_width = 600
display_height = 900
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
starship_width = 60
gameDisplay = pygame.display.set_mode((display_width,display_height)) #sets window size. Put between () so it's seen as a single parameter
pygame.display.set_caption("Starship") #changes window title display
clock = pygame.time.Clock() #sets clock as frames per second
ship_image_names = ["starship", "move_right", "move_left"]
ship_sprites = dict(((img_name, pygame.image.load(img_name + ".png"))
for img_name in ship_image_names))
starshipImp = ship_sprites["starship"]
def starship(x,y): #sets image position
gameDisplay.blit(starshipImp,(x,y))
def text_objects(text,font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def kb_disable():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or pygame.K_RIGHT or pygame.K_UP or pygame.K_DOWN:
pass
def message_display(text):
largeText = pygame.font.Font("theboldfont.ttf",70)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2), (display_height/2-170))
gameDisplay.blit(TextSurf,TextRect)
def message_display1(text):
largeText = pygame.font.Font("theboldfont.ttf",70)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2), (display_height/2-100))
gameDisplay.blit(TextSurf,TextRect)
pygame.display.update()
while not kb_disable():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
game_loop()
elif event.key == pygame.K_n:
pygame.quit()
quit()
def replay():
message_display("You Crashed")
message_display1("Replay? (Y)(N)")
def game_loop():
x = (display_width * 0.5 - 45)
y = (display_height * 0.8)
x_change = 0
y_change = 0
game_Exit = False # beginning game loop
while not game_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 = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
x += x_change
y += y_change
gameDisplay.fill(white) #background
starship(x,y) #shows starship image
if x_change == 0:
starshipImp = ship_sprites["starship"]
if x_change > 0:
starshipImp = ship_sprites["move_right"]
if x_change < 0:
starshipImp = ship_sprites["move_left"]
if x > display_width - starship_width or x < 0: #setting boundaries to left and right border
replay()
pygame.display.update() #update the screen
clock.tick(120)
game_loop()
pygame.quit()
quit()
I also added some kind of replay function but it's buggy, when I press the designated key, it doesn't always work, dont know why.
Any help would be greatly appreciated
near pygame.K_DOWN or other with pygame.K_
after pygame.K_ command add you r desired contorl key name

Pygame KEYUP, KEYDOWN causing ghost movement

I took a stab at learning KEYUP and KEYDOWN. Bascily, after the user crashes into any side of the window the game resets like intended. What is not intended is for the square to begin moving again even though no key has been pressed or released.
Ideal series of events:
- User Crashes
- Game resets with square in the original starting potion (stationary)
- press a movement key and square moves
Any insight into why this is happening would be great.
Thanks
import pygame
import time
pygame.init()
display_width = 800
display_height = 600
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
car_width = 75
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display('You Crashed')
def game_loop():
x = 200
y = 200
x_change = 0
y_change = 0
gameExit = False
while not gameExit:
# Movement logic
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x_change += 5
if event.key == pygame.K_LEFT:
x_change += -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
x_change += -5
if event.key == pygame.K_LEFT:
x_change += 5
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change += -5
if event.key == pygame.K_DOWN:
y_change += 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
y_change += 5
if event.key == pygame.K_DOWN:
y_change += -5
x += x_change
y += y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, red, [x, y, 75, 75])
# Check if border hit
if x > display_width - car_width or x < 0:
crash()
if y > display_height - car_width or y < 0:
crash()
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
Screw global variables, events and lots of flags.
Just use pygame.key.get_pressed to get the current state of the keyboard. Assign each arrow key a movement vector, add them up, normalize it, and there you go.
Also, if you want to do something with something rectangular, just use the Rect class. It will make your live a lot easier.
Here's a running example:
import pygame
import time
pygame.init()
display_width = 800
display_height = 600
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
car_width = 75
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display('You Crashed')
keymap = {
pygame.K_RIGHT: pygame.math.Vector2(1, 0),
pygame.K_LEFT: pygame.math.Vector2(-1, 0),
pygame.K_UP: pygame.math.Vector2(0, -1),
pygame.K_DOWN: pygame.math.Vector2(0, 1)
}
def game_loop():
# we want to draw a rect, so we simple use Rect
rect = pygame.rect.Rect(200, 200, 75, 75)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# get the state of the keyboard
pressed = pygame.key.get_pressed()
# get all the direction vectors from the keymap of all keys that are pressed
vectors = (keymap[key] for key in keymap if pressed[key])
# add them up to we get a single vector of the final direction
direction = pygame.math.Vector2(0, 0)
for v in vectors:
direction += v
# if we have to move, we normalize the direction vector first
# this ensures we're always moving at the correct speed, even diagonally
if direction.length() > 0:
rect.move_ip(*direction.normalize()*5)
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, red, rect)
# Check if border hit
# See how easy the check is
if not gameDisplay.get_rect().contains(rect):
crash()
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
There are some other issues with your code:
For example, if you use time.sleep(2), your entire program will freeze. This means you can't close the window while waiting and the window will not be redrawn by the window manager etc.
Also, your code contains an endless loop. If you hit the wall often enough, you'll run into a stack overflow, because game_loop calls crash which in turn calls game_loop again. This is probably not a big issue at first, but something to keep in mind.
What you need is to set your game variables (x, y, x_change, y_change) back to defaults, most probably in crash() function after crash. pygame won't do that for you.
Either make your variables global, or use some mutable object (like dictionary) to access them from other methods.
As Daniel Kukiela says, you can make "x_change" and "y_change" into global variables, along with giving them the value of 0 at the start, here is the working project as far as I understand.
import pygame
import time
pygame.init()
display_width = 800
display_height = 600
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
car_width = 75
x_change = 0
y_change = 0
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display('You Crashed')
def game_loop():
x = 200
y = 200
global x_change
x_change == 0
global y_change
y_change == 0
gameExit = False
while not gameExit:
# Movement logic
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x_change += 5
if event.key == pygame.K_LEFT:
x_change += -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
x_change += -5
if event.key == pygame.K_LEFT:
x_change += 5
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change += -5
if event.key == pygame.K_DOWN:
y_change += 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
y_change += 5
if event.key == pygame.K_DOWN:
y_change += -5
x += x_change
y += y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, red, [x, y, 75, 75])
# Check if border hit
if x > display_width - car_width or x < 0:
crash()
if y > display_height - car_width or y < 0:
crash()
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()

Moving a drawn rectangle

I have the game come up and the rectangle rendered.
When I press my KEYDOWN it doesn't move the rectangle, it just makes it longer.
I have tried tons of stuff. I am new to Pygame.
Any help would be amazing.
Here is the code:
import pygame
import time
import random
import math
import sys
pygame.init()
display_width = 1200
display_height = 800
white = (255,255,255)
black = (0,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Vertical Pong')
clock = pygame.time.Clock()
def pongBoard(x,y,):
pygame.draw.rect(gameDisplay,white,(x,y,250,25))
def gameLoop():
x = 325
y = 750
xChange = 0
inGame = True
while inGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
xChange = -5
print("Left")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
xChange = 5
print("Right")
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
xChange = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
xChange = 0
pongBoard(x,y)
x += xChange
pygame.display.update()
clock.tick(60)
gameLoop()
pygame.quit()
quit()
So the problem is this:
The rectangle is being constantly redrawn at a different coord, but the screen is not being drawn over the rectangle to cover up the part that is not supposed to be there. In simpler terms, we need to constantly draw the background.
So now the code in the main game loop:
while inGame:
#This code below draws the background
pygame.draw.rect(gameDisplay, black, (0, 0, display_width, display_height))
That is it! The background will constantly cover up the pong ball, and the pong ball will be constantly blitted to a new position!
P.S, there is a better way to do arrow key movement here: How to get keyboard input in pygame?
it actualy does move it, but the old one just stays there, making it look like it does not move but just grows. one way to change that would be to change the old ones color to the background color
try this code it works :-)
import pygame
import time
import random
import math
import sys
pygame.init()
display_width = 1200
display_height = 800
white = (255,255,255)
black = (0,0,0)
red = (123,11,45)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Vertical Pong')
clock = pygame.time.Clock()
def pongBoard(x,y,xold):
pygame.draw.rect(gameDisplay,white,[x,y,250,25])
pygame.draw.rect(gameDisplay,red,[xold,y,250,25])
def gameLoop():
x = 325
y = 750
xChange = 0
inGame = True
while inGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
xChange = -50
pongBoard(x,y,xold)
print("Left")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
xChange = 50
pongBoard(x,y,xold)
print("Right")
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
xChange = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
xChange = 0
xold = x
x += xChange
xold = x-xChange
pygame.display.update()
clock.tick(60)
gameLoop()
pygame.quit()
quit()

Having some problems with Pygame

So heres my script:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((600,600))
pygame.display.set_caption('Doge Adventures')
gameexit = False
move_x = 300
move_y = 300
move_x_change = 0
move_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:
move_x_change = -10
if event.key == pygame.K_RIGHT:
move_y_change = 10
move_x += move_x_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [move_x, move_y, 10, 10])
pygame.display.update()
pygame.quit()
quit()
The problem is that when I run it, nothing happens, no errors. Just the idle pops up, but no window. I was wondering if there was anything wrong with my code. I'm running python 2.6 with pygame 2.6
As I said you need to indentate everything, but I found many problems in your code, here's a version I did out of it:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((600,600))
pygame.display.set_caption('Doge Adventures')
gameexit = False
move_x = 300
move_y = 300
white = (255, 255, 255)
black = (0, 0, 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:
move_x -= 10
elif event.key == pygame.K_RIGHT:
move_x += 10
elif event.key == pygame.K_UP:
move_y -= 10
elif event.key == pygame.K_DOWN:
move_y += 10
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [move_x, move_y, 10, 10])
pygame.display.update()
pygame.quit()
quit()
I recommend you the Al Sweigart book of Python/Pygame. It's such a great book.

Categories

Resources