[pygame] Fullscreen Display

2007-12-04 Thread Matt Smith

Hi,

I have seen the following line used to initialise a full screen display in a 
number of books and tutorials:


screen = pygame.display.set_mode((xsize, ysize), FULLSCREEN, 32)

When I use it in my program then I get the following error:

Traceback (most recent call last):
  File bouncing_ball_OOP.py, line 63, in module
screen = pygame.display.set_mode((xsize, ysize), FULLSCREEN, 32)
NameError: name 'FULLSCREEN' is not defined

I expected the set_mode method to treat the FULLSCREEN as an argument and not as 
a variable/ object. I can use the following line without a problem to initialise 
a windowed display for my program:


screen = pygame.display.set_mode((xsize, ysize), 0, 32)

Can anyone tell me where I'm going wrong?

Thanks,

Matt.



Re: [pygame] Why does my ball vibrate?

2007-12-01 Thread Matt Smith

Douglas Bagnall wrote:

FLOOR = 384

if ypos = FLOOR and velocity  0:
overstep = ypos - FLOOR
ypos = FLOOR - overstep * bounce
velocity = -velocity * bounce


I tried using this code and I go back to the situation where the ball continues 
to bounce when it should have come to rest. I would imagine this is because each 
time the ball passes below the floor we are giving it a little bit of upwards 
velocity.





[pygame] Bouncing ball - separating the physics from the frame rate

2007-12-01 Thread Matt Smith

Hi,

I know have the following code for my bouncing ball program:

#! /usr/bin/python

import sys, pygame, math

pygame.init()

xpos = 92
ypos = 0
gravity = 9.8
velocity = 0
# How much of the velocity of the ball is retained on a bounce
bounce = 0.8

screen = pygame.display.set_mode((200, 400), 0, 32)
# The ball is a 16px sprite
ball = pygame.image.load('ball.png')
clock = pygame.time.Clock()


# The main loop
while True:

# Test for exit
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()

# The physics
# Reverse velocity taking into account bounciness if we hit the ground
if ypos = 384 and velocity  0:
# Avoid the ball sinking into the ground
ypos = 384
velocity = -velocity * bounce

time_passed = clock.tick(60) / 1000.0
newvelocity = velocity + (gravity * time_passed)
# Use the average velocity over the period of the frame to change position
ypos = ypos + int(((velocity + newvelocity) / 2) * time_passed * 160)
velocity = newvelocity

# Update the screen
screen.fill((0, 0, 0))
screen.blit(ball, (xpos, ypos))
pygame.display.update()

The bounce is pretty realistic and I can live with the slight error caused by 
returning the ball to the floor exactly each time it bounces. Next I am going to 
have a look at adding in some sideways movement and bouncing off walls as well. 
Before I do that I would like to separate the physics calculations from the 
frame rate calculations and drawing loop. I really don't have a clue how to do 
this. I have never programmed anything in real time before. Having had a think 
about this I can see two possible ways of doing it.


1. Have the physics and graphics running in separate threads. I don't have a 
clue how to implement this!


2. Call a function from the main loop every time it passes which does the 
physics calculations based on a clock independent of the frame rate but which 
pauses when the simulation is paused (window not in focus?) Is there a function 
in the sys module that I can use to test for a pause?


Can anyone give me some more pointers on the best and most pythonic way of doing 
this.


Thanks.

Matt



Re: [pygame] Why does my ball vibrate?

2007-11-29 Thread Matt Smith

Thanks for all the help.

I have modified the code for determining the position of the ball so 
that the pixel co-ordinates are always integers:


ypos = ypos + int(((velocity + newvelocity) / 2) * time_passed * 160)

This has cured the vibrating problem and the ball comes to rest quite 
realistically. I tried consolidating the code checking for the floor but 
found that the ball sinks into the floor unless I always correct an over 
shoot of the floor to the co-ordinates of the floor.


# Reverse velocity taking into account bounciness if we hit the ground
if ypos = 384 and velocity  0:
# Avoid the ball sinking into the ground
ypos = 384
velocity = -velocity * bounce

I can't quite get my head around WHY the ball sinks into the ground if I 
don't set ypos back to 384 but it certainly works.


I am interested by the idea of separating the physics calculations from 
the frame rate calculations but I really don't know where to start.


Thanks again,

Matt



[pygame] Why does my ball vibrate?

2007-11-28 Thread Matt Smith

Hi,

I am beginning to learn Pygame and I have written a short program to 
simulate a bouncing ball. The motion of the ball is pretty realistic 
until it has almost come to rest. The ball continues to vibrate long 
after you would have expected it to come to a complete rest (or be 
moving less than 1 pile each time as it will never stop moving 100%). 
Also, the ball can start to bounce higher again if I click somewhere 
else on the desktop. I can't work out why this happens so can anyone 
shed some light on it and suggest how I can prevent it.Here's my code:


#! /usr/bin/python

import sys, pygame
pygame.init()

xpos = 92
ypos = 0
gravity = 9.8
velocity = 0
# How much of the velocity of the ball is retained on a bounce
bounce = 0.8

screen = pygame.display.set_mode((200, 400), 0, 32)
# The ball is a 16px sprite
ball = pygame.image.load('ball.png')
clock = pygame.time.Clock()

# The main loop
while True:

# Test for exit
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()

# The physics
# Reverse velocity taking into account bounciness if we hit the ground
if ypos == 384 and velocity  0:
velocity = -velocity * bounce
time_passed = clock.tick(60) / 1000.0
newvelocity = velocity + (gravity * time_passed)
# Use the average velocity over the period of the frame to change 
position

ypos = ypos + (((velocity + newvelocity) / 2) * time_passed * 160)
# Prevent the ball from sinking into the ground
if ypos = 384:
ypos = 384
velocity = newvelocity

# Update the screen
screen.fill((0, 0, 0))
screen.blit(ball, (xpos, ypos))
pygame.display.update()

Thanks for looking.

Matt