| | |
fileIO & pygame help
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
1
#11 27 Days Ago
When you have a project like this, it is best to test each shape you want to draw before you combine it all. Let's start out with a pygame template that you can use for each shape ...
Now let's test the rectangle shape ...
... next test the line ...
... lastly test the circle ...
Python Syntax (Toggle Plain Text)
import pygame import random pygame.init() w = 640 h = 480 screen = pygame.display.set_mode((w, h)) # set (r, g, b) color tuple color = (random.randrange(255), random.randrange(255), random.randrange(255)) # ... your shape code goes here ... # update display pygame.display.flip() # event loop ... running = True while running: for event in pygame.event.get(): # quit when window corner x is clicked if event.type == pygame.QUIT: running = False
Python Syntax (Toggle Plain Text)
import pygame import random pygame.init() w = 640 h = 480 screen = pygame.display.set_mode((w, h)) # set (r, g, b) color tuple color = (random.randrange(255), random.randrange(255), random.randrange(255)) # set rect corner coordiates to draw a rectangle # (x1, y1, x2, y2) upper left and lower right corner coordinates rect = (random.randrange(w), random.randrange(h), random.randrange(w), random.randrange(h)) # pygame.draw.rect(Surface, color, Rect, width=0) # if width=0 (or not given) the rectangle is filled with color pygame.draw.rect(screen, color, rect) # update display pygame.display.flip() # event loop ... running = True while running: for event in pygame.event.get(): # quit when window corner x is clicked if event.type == pygame.QUIT: running = False
Python Syntax (Toggle Plain Text)
import pygame import random pygame.init() w = 640 h = 480 screen = pygame.display.set_mode((w, h)) # set (r, g, b) color tuple color = (random.randrange(255), random.randrange(255), random.randrange(255)) # set start and end position of line start_pos = random.randrange(w), random.randrange(h) end_pos = random.randrange(w), random.randrange(h) # pygame.draw.line(Surface, color, start_pos, end_pos, width=1) # width is thickness of line (default is 1) pygame.draw.line(screen, color, start_pos, end_pos) # update display pygame.display.flip() # event loop ... running = True while running: for event in pygame.event.get(): # quit when window corner x is clicked if event.type == pygame.QUIT: running = False
Python Syntax (Toggle Plain Text)
import pygame import random pygame.init() w = 640 h = 480 screen = pygame.display.set_mode((w, h)) # set (r, g, b) color tuple color = (random.randrange(255), random.randrange(255), random.randrange(255)) # set center_pos for circle center_pos = random.randrange(w), random.randrange(h) # pygame.draw.circle(Surface, color, center_pos, radius, width=0) # if width=0 (or not given) the circle is filled with color radius = 50 pygame.draw.circle(screen, color, center_pos, radius) # update display pygame.display.flip() # event loop ... running = True while running: for event in pygame.event.get(): # quit when window corner x is clicked if event.type == pygame.QUIT: running = False
Last edited by vegaseat; 27 Days Ago at 11:05 am. Reason: template code
May 'the Google' be with you!
•
•
Join Date: Sep 2009
Posts: 11
Reputation:
Solved Threads: 0
0
#12 27 Days Ago
Python Syntax (Toggle Plain Text)
import random import pygame w = 640 h = 480 n = random.randrange(6) x = open("shapes.txt") z = x.readlines()[n] lines = len(z) str(lines) l = str(('The file shapes.txt has',lines,'lines.')) output = open('output.txt','w') output.write(l) output.close() screen = pygame.display.set_mode((w, h)) if "circle" in z: pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 ) elif "line" in z: pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)),(random.randrange(w), random.randrange(h)), (random.randrange(w), random.randrange(h))) elif "rect" in z: pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)),(random.randrange(w), random.randrange(h), random.randrange(w), random.randrange(h))) pygame.display.flip() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: exit()
Solved thanks to everyone for the help!
![]() |
Similar Threads
- Code Snippet: Pygame Curve Library for Anti-Aliased Circles and Curved Surface Corners. (Python)
- Professional Shared & Reseller Web Hosting (Web Hosting Deals)
- Code Snippet: Bouncing Image (Python & Pygame) (Python)
- pygame full screen help (Python)
- Pygame crashing (Python)
Other Threads in the Python Forum
- Previous Thread: python MySQL problem
- Next Thread: how do I connect to MySQL server which is firewall protected?
| Thread Tools | Search this Thread |
accessdenied advanced application argv beginner change color command convert csv cursor def dictionary digital dynamic dynamically edit editing enter event examples excel file float format frange function google gui homework i/o import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame newb number numbers numeric obexftp output parameters parsing path port prime programming projects py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session simple skinning smtp sprite stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape windows wxpython






