-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpygameexp.py
More file actions
64 lines (47 loc) · 1.71 KB
/
pygameexp.py
File metadata and controls
64 lines (47 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 13 17:07:53 2017
@author: ECOWIZARD
"""
#pygame stuff
import random
import pygame
from pygame.locals import *
class Square():
def __init__(self,x,y,width,height,color):
self.rectangle = Rect(x,y,width,height)
self.color = color
def draw(self,screen):
pygame.draw.rect(screen,self.color,self.rectangle)
class pygamewindow():
def __init__(self,width,height):
self.square = Square(20,20,50,50,(250,40,40))
self.openwindow(width,height)
def drawsquare(self):
self.square.draw(self.screen)
def openwindow(self,width,height):
self.screen = pygame.display.set_mode((height,width))
pygame.display.flip()
running = True
while running:
self.render()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
x = random.randint(-1,1)
y = random.randint(-1,1)
self.movesquare(x,y)
pygame.quit()
def render(self):
self.screen.fill((0,0,0))
self.drawsquare()
pygame.display.flip()
def movesquare(self,x,y):
screensize = self.screen.get_size()
self.square.rectangle.x += x
self.square.rectangle.y += y
if self.square.rectangle.x < 0 or self.square.rectangle.x > screensize[0]:
self.square.rectangle.x -= x
if self.square.rectangle.y < 0 or self.square.rectangle.y > screensize[1]:
self.square.rectangle.y -= y
window = pygamewindow(300,200)