Jump to content
  • Advertisement
Sign in to follow this  
daedalus316

Pygame Help

This topic is 2532 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am attempting to create a random cloud of rectangular shapes and enable the user to move around using WASD.My original approach was to create the large rectangles and then modify thier coordinates to make it seem like they were moving. For some reason the program would ru fine but the rectangles would not move at all and nothing would happen. CAn anybody see why this is happening and/or know a better way to do this. Eventually i want to be able to have a player run on these rectngles, and be able to walk sideways or anyway on them and jump from one to another...eventually for now i justwant to get simple movement done.
Code:

import pygame
import random
import sys
from pygame.locals import *
width, height= (600, 600)
num=10
pygame.init()
screen=pygame.display.set_mode((width,height))

background= pygame.Surface(screen.get_size())
background=background.convert()
stars = [
[random.randint(0, width),random.randint(0,height), random.randint(50,100), random.randint(5,200)]
for x in range(num)
]
clock = pygame.time.Clock()
while True:
clock.tick(22)
background.fill((0,0,0))
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
background.fill((0,0,0))
for star in stars:
pygame.draw.rect(background,(255,255,255),(star[0], star[1], star[2], star[3]), 0)
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type ==pygame.KEYDOWN:
if event.key ==pygame.K_w:
print "GOGTO"
for star in stars:
star[1] = star[1] + 300
screen.blit(background, (0,0))
pygame.display.flip()

Share this post


Link to post
Share on other sites
Advertisement
Hey there, you had the event.get() in twice. I removed it, and clarified the code a bit with comments and spacing that I prefer in my editor.


import pygame
import random
import sys
from pygame.locals import *
#size constants
width, height = (600, 600)
#number of stars
num = 10
#pygame initilization
pygame.init()
screen=pygame.display.set_mode((width,height))
#set the background
background= pygame.Surface(screen.get_size())
background=background.convert()
#list comprehension for Rects (x, y, w, h) for range(num)
stars = [
[random.randint(0, width),
random.randint(0,height),
random.randint(50,100),
random.randint(5,200)]

for x in range(num)]
#used for FPS
clock = pygame.time.Clock()
while True:

#max 22 fps
clock.tick(22)

#fill BG with Black
background.fill((0,0,0))

#for every star in list of stars: draw it
for star in stars:
pygame.draw.rect(background,(255,255,255),(star[0], star[1], star[2], star[3]), 0)

#event handler
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type ==pygame.KEYDOWN:
print 'key down'
if event.key ==pygame.K_w:
print "caught key 'w'"
for star in stars:
#add 300px to y
star[1] = star[1] + 300

#blit and flip screen
screen.blit(background, (0,0))
pygame.display.flip()

Share this post


Link to post
Share on other sites
thanks that worked great. Just a question. Why does that not allow pygame to run it properly? does it only look at the first one. In my version had a strange thing where one time out of 50 it would work and the squares would move.

Share this post


Link to post
Share on other sites
Hey man, no problem.

What I think happened was you hit a button fast enough that between the two get() calls, you made a new event that the second call caught, if that makes any sense. I could be wrong too.

It looks at both, but for the second call, the event queue is empty.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!