Форум сайта python.su
0
Здраствуйте, эксперты!!!
помогите с организацией движения на pygame при помощи мыши.
Если сможете приведите пример(код) небольшой программки.
Офлайн
0
Могу поделиться своим старым курсовиком.
Сетевые отказоустойчивые крестики-нолики на pygame.
http://dl.dropbox.com/u/11931230/ouvs_cource_work_report.doc
Отредактировано (Окт. 16, 2010 12:46:56)
Офлайн
0
спасибо, Eliont!
я тут накидал код только с pygame.transform.rotate() проблемы
за плохой алгоритм просьба строго не судить, я новичек в питоне и в pygame
# -*- coding: UTF-8 -*-
background_image_filename='pole.jpg'
message="game"
import pygame
from pygame.locals import *
from sys import exit
import time
import math
#import os
pygame.init()
screen=pygame.display.set_mode((640,480))
pygame.display.set_caption(message)
background=pygame.image.load(background_image_filename).convert()
class Bricks:
def __init__(self,xpos,ypos,filename):
self.x=xpos
self.y=ypos
self.bitmap=pygame.image.load(filename)
def render(self,angle):
self.ugol=pygame.transform.rotate(self.bitmap,angle)
screen.blit(self.ugol,(self.x,self.y))
stime()
x0=320
y0=240
brick=Bricks(x0,y0,'tank.png')
quit=0
def stime():
time.sleep(0.01)
pygame.display.update()
angle=0
while quit==0:
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type==QUIT:
exit()
if pygame.mouse.get_pressed()[2]:
x1,y1=pygame.mouse.get_pos()
if x1>x0:
a=x1-x0
elif x1<x0:
a=x0-x1
else:
a=x0
if y1>y0:
b=y1-y0
elif y1<y0:
b=y0-y1
else:
b=y0
c=math.hypot(a,b)
c=round(c,1)
rad=math.acos(a/c)
angle=math.degrees(rad)
#if x1>x0:
angle=-angle
z=1
while c>=z:
dx=z*math.cos(rad)
dy=z*math.sin(rad)
if x1>x0:
brick.x=x0+dx
elif x1<x0:
brick.x=x0-dx
if y1>y0:
brick.y=y0+dy
elif y1<y0:
brick.y=y0-dy
z=z+1.1
screen.blit(background,(0,0))
brick.render(angle)
x0=brick.x
y0=brick.y
brick.render(angle)
Отредактировано (Окт. 16, 2010 19:21:22)
Офлайн
0
вот оптимизировал
# -*- coding: UTF-8 -*-
background_image_filename='pole.jpg'
message="prog"
import pygame
from pygame.locals import *
from sys import exit
import time
import math
#import os
pygame.init()
screen=pygame.display.set_mode((640,480))
pygame.display.set_caption(message)
background=pygame.image.load(background_image_filename).convert()
class Bricks:
def __init__(self,xpos,ypos,filename):
self.x=xpos
self.y=ypos
self.bitmap=pygame.image.load(filename)
def render(self,angle):
self.ugol=pygame.transform.rotate(self.bitmap,angle)
screen.blit(self.ugol,(self.x,self.y))
stime()
x0=320
y0=240
brick=Bricks(x0,y0,'tank.png')
quit=0
def stime():
time.sleep(0.01)
pygame.display.update()
angle=0
while quit==0:
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type==QUIT:
exit()
if pygame.mouse.get_pressed()[2]:
x1,y1=pygame.mouse.get_pos()
if x1>x0:
a=x1-x0
elif x1<x0:
a=x0-x1
else:
a=x0
if y1>y0:
b=y1-y0
elif y1<y0:
b=y0-y1
else:
b=y0
c=math.hypot(a,b)
c=round(c,1)
rad=math.acos(a/c)
angle=math.degrees(rad)
#if x1>x0:
#angle=-angle
if x1>x0 and y1>y0:
angle=-90-angle
if x1<x0 and y1<y0:
angle=-270-angle
if x1>x0 and y1<y0:
angle=-90+angle
if x1<x0 and y1>y0:
angle=-270+angle
if x1==x0 and y1>y0:
angle=-180
if x1==x0 and y1<y0:
angle=0
if x1>x0 and y1==y0:
angle=-90
if x1<x0 and y1==y0:
angle=-270
z=1
#print x1, y1
#print angle
while c>=z:
dx=z*math.cos(rad)
dy=z*math.sin(rad)
if x1>x0:
brick.x=x0+dx
elif x1<x0:
brick.x=x0-dx
if y1>y0:
brick.y=y0+dy
elif y1<y0:
brick.y=y0-dy
z=z+1
screen.blit(background,(0,0))
brick.render(angle)
x0=brick.x
y0=brick.y
brick.render(angle)
Офлайн
0
pole.jpg и tank.png ещё нужно для этого кода
не открывает
Офлайн